countrylanguagenames_test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.locale.countryLanguageNamesTest');
  15. goog.setTestOnly('goog.locale.countryLanguageNamesTest');
  16. goog.require('goog.locale');
  17. goog.require('goog.testing.jsunit');
  18. var LocaleNameConstants_en;
  19. function setUpPage() {
  20. // Test data from //googledata/i18n/js_locale_data/LocaleNameConstants__de.js
  21. var LocaleNameConstants_de = {
  22. LANGUAGE: {
  23. 'cad': 'Caddo',
  24. 'fr': 'Franz\u00f6sisch',
  25. 'fr_CA': 'Canadian French',
  26. 'fr_CH': 'Swiss French',
  27. 'zh': 'Chinesisch',
  28. 'zh_Hans': 'Chinesisch (vereinfacht)',
  29. 'zh_Hant': 'Chinesisch (traditionell)'
  30. },
  31. COUNTRY: {'CN': 'China', 'ES': 'Spanien', 'FR': 'Frankreich'}
  32. };
  33. registerLocalNameConstants(LocaleNameConstants_de, 'de');
  34. // Test data from //googledata/i18n/js_locale_data/LocaleNameConstants__en.js
  35. LocaleNameConstants_en = {
  36. LANGUAGE: {
  37. 'cad': 'Caddo',
  38. 'fr': 'French',
  39. 'fr_CA': 'Canadian French',
  40. 'fr_CH': 'Swiss French',
  41. 'zh': 'Chinese',
  42. 'zh_Hans': 'Simplified Chinese',
  43. 'zh_Hant': 'Traditional Chinese'
  44. },
  45. COUNTRY: {'CN': 'China', 'ES': 'Spain', 'FR': 'France'}
  46. };
  47. registerLocalNameConstants(LocaleNameConstants_en, 'en');
  48. goog.locale.setLocale('de');
  49. }
  50. function testLoadLoacleSymbols() {
  51. var result = goog.locale.getLocalizedCountryName('fr-FR');
  52. assertEquals('Frankreich', result);
  53. }
  54. function testGetNativeCountryName() {
  55. var result = goog.locale.getNativeCountryName('de-DE');
  56. assertEquals('Deutschland', result);
  57. result = goog.locale.getNativeCountryName('de_DE');
  58. assertEquals('Deutschland', result);
  59. result = goog.locale.getNativeCountryName('und');
  60. assertEquals('und', result);
  61. result = goog.locale.getNativeCountryName('de-CH');
  62. assertEquals('Schweiz', result);
  63. result = goog.locale.getNativeCountryName('fr-CH');
  64. assertEquals('Suisse', result);
  65. result = goog.locale.getNativeCountryName('it-CH');
  66. assertEquals('Svizzera', result);
  67. }
  68. function testGetLocalizedCountryName() {
  69. var result = goog.locale.getLocalizedCountryName('es-ES');
  70. assertEquals('Spanien', result);
  71. result = goog.locale.getLocalizedCountryName('es-ES', LocaleNameConstants_en);
  72. assertEquals('Spain', result);
  73. result = goog.locale.getLocalizedCountryName('zh-CN-cmn');
  74. assertEquals('China', result);
  75. result = goog.locale.getLocalizedCountryName('zh_CN_cmn');
  76. assertEquals('China', result);
  77. // 'und' is a non-existing locale, default behavior is to
  78. // return the locale name itself if no mapping is found.
  79. result = goog.locale.getLocalizedCountryName('und');
  80. assertEquals('und', result);
  81. }
  82. function testGetLocalizedRegionNameFromRegionCode() {
  83. var result = goog.locale.getLocalizedRegionNameFromRegionCode('ES');
  84. assertEquals('Spanien', result);
  85. result = goog.locale.getLocalizedRegionNameFromRegionCode(
  86. 'ES', LocaleNameConstants_en);
  87. assertEquals('Spain', result);
  88. result = goog.locale.getLocalizedRegionNameFromRegionCode('CN');
  89. assertEquals('China', result);
  90. // 'XX' is a non-existing country code, default behavior is to
  91. // return the code itself if no mapping is found.
  92. result = goog.locale.getLocalizedRegionNameFromRegionCode('XX');
  93. assertEquals('XX', result);
  94. }
  95. function testGetNativeLanguageName() {
  96. var result = goog.locale.getNativeLanguageName('fr');
  97. assertEquals('fran\u00E7ais', result);
  98. result = goog.locale.getNativeLanguageName('fr-latn-FR');
  99. assertEquals('fran\u00E7ais', result);
  100. result = goog.locale.getNativeLanguageName('fr_FR');
  101. assertEquals('fran\u00E7ais', result);
  102. result = goog.locale.getNativeLanguageName('error');
  103. assertEquals('error', result);
  104. }
  105. function testGetLocalizedLanguageName() {
  106. var result = goog.locale.getLocalizedLanguageName('fr');
  107. assertEquals('Franz\u00F6sisch', result);
  108. result = goog.locale.getLocalizedLanguageName('fr', LocaleNameConstants_en);
  109. assertEquals('French', result);
  110. result = goog.locale.getLocalizedLanguageName('fr-latn-FR');
  111. assertEquals('Franz\u00F6sisch', result);
  112. result = goog.locale.getLocalizedLanguageName('fr_FR');
  113. assertEquals('Franz\u00F6sisch', result);
  114. result = goog.locale.getLocalizedLanguageName('cad');
  115. assertEquals('Caddo', result);
  116. result = goog.locale.getLocalizedLanguageName('error');
  117. assertEquals('error', result);
  118. result =
  119. goog.locale.getLocalizedLanguageName('zh_Hans', LocaleNameConstants_en);
  120. assertEquals('Simplified Chinese', result);
  121. }
  122. function testGetLocalizedLanguageNameForGivenSymbolset() {
  123. var result = goog.locale.getLocalizedCountryName('fr-FR');
  124. assertEquals('Frankreich', result);
  125. result = goog.locale.getLocalizedCountryName('fr-FR', LocaleNameConstants_en);
  126. assertEquals('France', result);
  127. result = goog.locale.getLocalizedCountryName('fr-FR');
  128. assertEquals('Frankreich', result);
  129. }
  130. /**
  131. * Valid combination of sub tags:
  132. * 1) LanguageSubtag'-'RegionSubtag
  133. * 2) LanguageSubtag'-'ScriptSubtag'-'RegionSubtag
  134. * 3) LanguageSubtag'-'RegionSubtag'-'VariantSubtag
  135. * 4) LanguageSubtag'-'ScriptSubTag'-'RegionSubtag'-'VariantSubtag
  136. */
  137. function testGetRegionSubTag() {
  138. var result = goog.locale.getRegionSubTag('de-CH');
  139. assertEquals('CH', result);
  140. result = goog.locale.getRegionSubTag('de-latn-CH');
  141. assertEquals('CH', result);
  142. result = goog.locale.getRegionSubTag('de_latn_CH');
  143. assertEquals('CH', result);
  144. result = goog.locale.getRegionSubTag('de-CH-xxx');
  145. assertEquals('CH', result);
  146. result = goog.locale.getRegionSubTag('de-latn-CH-xxx');
  147. assertEquals('CH', result);
  148. result = goog.locale.getRegionSubTag('es-latn-419-xxx');
  149. assertEquals('419', result);
  150. result = goog.locale.getRegionSubTag('es_latn_419_xxx');
  151. assertEquals('419', result);
  152. // No region sub tag present
  153. result = goog.locale.getRegionSubTag('de');
  154. assertEquals('', result);
  155. }
  156. function testGetLanguageSubTag() {
  157. var result = goog.locale.getLanguageSubTag('de');
  158. assertEquals('de', result);
  159. result = goog.locale.getLanguageSubTag('de-DE');
  160. assertEquals('de', result);
  161. result = goog.locale.getLanguageSubTag('de-latn-DE-xxx');
  162. assertEquals('de', result);
  163. result = goog.locale.getLanguageSubTag('nds');
  164. assertEquals('nds', result);
  165. result = goog.locale.getLanguageSubTag('nds-DE');
  166. assertEquals('nds', result);
  167. }
  168. function testGetScriptSubTag() {
  169. var result = goog.locale.getScriptSubTag('fr');
  170. assertEquals('', result);
  171. result = goog.locale.getScriptSubTag('fr-Latn');
  172. assertEquals('Latn', result);
  173. result = goog.locale.getScriptSubTag('fr-Arab-AA');
  174. assertEquals('Arab', result);
  175. result = goog.locale.getScriptSubTag('de-Latin-DE');
  176. assertEquals('', result);
  177. result = goog.locale.getScriptSubTag('srn-Ar-DE');
  178. assertEquals('', result);
  179. }