1 /* 2 ******************************************************************************* 3 * Copyright (C) 2009-2014, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 package com.ibm.icu.dev.test; 8 9 import static com.ibm.icu.impl.LocaleDisplayNamesImpl.DataTableType.LANG; 10 import static com.ibm.icu.impl.LocaleDisplayNamesImpl.DataTableType.REGION; 11 12 import java.util.Locale; 13 14 import com.ibm.icu.impl.LocaleDisplayNamesImpl; 15 import com.ibm.icu.text.LocaleDisplayNames; 16 import com.ibm.icu.text.LocaleDisplayNames.DialectHandling; 17 import com.ibm.icu.util.ULocale; 18 19 public class TestLocaleNamePackaging extends TestFmwk { 20 21 public static void main(String[] args) { 22 new TestLocaleNamePackaging().run(args); 23 } 24 25 public TestLocaleNamePackaging() { 26 } 27 28 public boolean validate() { 29 logln("language data: " + LocaleDisplayNamesImpl.haveData(LANG)); 30 logln(" region data: " + LocaleDisplayNamesImpl.haveData(REGION)); 31 return true; 32 } 33 34 private static ULocale[] locales = { 35 ULocale.ROOT, ULocale.US, new ULocale("es_ES"), ULocale.GERMANY, 36 new ULocale("und_TH") 37 }; 38 39 // Java Locales equivalent to above 40 private static Locale[] javaLocales = { 41 new Locale(""), Locale.US, new Locale("es", "ES"), Locale.GERMANY, 42 new Locale("und", "TH") 43 }; 44 45 public void testRegionDisplayNames() { 46 String[] expectedWithRegionData = { 47 "", 48 "US", 49 "ES", 50 "DE", 51 "TH", 52 "", 53 "United States", 54 "Spain", 55 "Germany", 56 "Thailand", 57 "", 58 "Estados Unidos", 59 "Espa\u00f1a", 60 "Alemania", 61 "Tailandia", 62 "", 63 "Vereinigte Staaten", 64 "Spanien", 65 "Deutschland", 66 "Thailand", 67 "", 68 "United States", 69 "Spain", 70 "Germany", 71 "Thailand", 72 }; 73 String[] expectedWithoutRegionData = { 74 "", 75 "US", 76 "ES", 77 "DE", 78 "TH", 79 }; 80 String[] expected = LocaleDisplayNamesImpl.haveData(REGION) ? 81 expectedWithRegionData : expectedWithoutRegionData; 82 83 int n = 0; 84 for (ULocale displayLocale : locales) { 85 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale); 86 for (ULocale targetLocale : locales) { 87 String result = dn.regionDisplayName(targetLocale.getCountry()); 88 assertEquals(targetLocale + " in " + displayLocale, expected[n++], result); 89 if (n == expected.length) { 90 n = 0; 91 } 92 } 93 } 94 95 // Same test with Java Locale 96 n = 0; 97 for (Locale displayJavaLocale : javaLocales) { 98 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayJavaLocale); 99 for (Locale targetLocale : javaLocales) { 100 String result = dn.regionDisplayName(targetLocale.getCountry()); 101 assertEquals(targetLocale + " in " + displayJavaLocale, expected[n++], result); 102 if (n == expected.length) { 103 n = 0; 104 } 105 } 106 } 107 108 } 109 110 public void testLanguageDisplayNames() { 111 String[] expectedWithLanguageData = { 112 "", 113 "en", 114 "es", 115 "de", 116 "und", 117 "", 118 "English", 119 "Spanish", 120 "German", 121 "Unknown Language", 122 "", 123 "ingl\u00E9s", 124 "espa\u00F1ol", 125 "alem\u00E1n", 126 "lengua desconocida", 127 "", 128 "Englisch", 129 "Spanisch", 130 "Deutsch", 131 "Unbestimmte Sprache", 132 "", 133 "English", 134 "Spanish", 135 "German", 136 "Unknown Language", 137 }; 138 String[] expectedWithoutLanguageData = { 139 "", 140 "en", 141 "es", 142 "de", 143 "und" 144 }; 145 String[] expected = LocaleDisplayNamesImpl.haveData(LANG) ? 146 expectedWithLanguageData : expectedWithoutLanguageData; 147 148 int n = 0; 149 for (ULocale displayLocale : locales) { 150 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale); 151 for (ULocale targetLocale : locales) { 152 String result = dn.languageDisplayName(targetLocale.getLanguage()); 153 assertEquals(targetLocale + " in " + displayLocale, expected[n++], result); 154 if (n == expected.length) { 155 n = 0; 156 } 157 } 158 } 159 160 // Same test with Java Locale 161 n = 0; 162 for (Locale displayJavaLocale : javaLocales) { 163 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayJavaLocale); 164 for (Locale targetLocale : javaLocales) { 165 String result = dn.languageDisplayName(targetLocale.getLanguage()); 166 assertEquals(targetLocale + " in " + displayJavaLocale, expected[n++], result); 167 if (n == expected.length) { 168 n = 0; 169 } 170 } 171 } 172 173 } 174 175 // test a 'root' locale, with keywords 176 public void testLocaleDisplayNameWithKeywords() { 177 String[] expectedWithLanguageData = { 178 "root (collation=phonebook)", 179 "Root (Phonebook Sort Order)", 180 "ra\u00EDz (orden de list\u00EDn telef\u00F3nico)", 181 "Root (Telefonbuch-Sortierung)", 182 "Root (Phonebook Sort Order)", 183 }; 184 String[] expectedWithoutLanguageData = { 185 "root (collation=phonebook)", 186 }; 187 String[] expected = LocaleDisplayNamesImpl.haveData(LANG) ? 188 expectedWithLanguageData : expectedWithoutLanguageData; 189 190 ULocale kl = new ULocale("@collation=phonebook"); 191 192 int n = 0; 193 for (ULocale displayLocale : locales) { 194 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale); 195 String result = dn.localeDisplayName(kl); 196 assertEquals(kl + " in " + displayLocale, expected[n++], result); 197 if (n == expected.length) { 198 n = 0; 199 } 200 } 201 } 202 203 public void testLanguageDisplayNameDoesNotTranslateRoot() { 204 // "root" is not a language code-- the fact that we have our data organized this 205 // way is immaterial. "root" remains untranslated whether we have data or not. 206 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US); 207 assertEquals("root", "root", dn.languageDisplayName("root")); 208 } 209 210 public void testLanguageDisplayNameDoesNotTranslateDialects() { 211 // Dialect ids are also not language codes. 212 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US, 213 DialectHandling.DIALECT_NAMES); 214 assertEquals("dialect", "en_GB", dn.languageDisplayName("en_GB")); 215 216 String target = LocaleDisplayNamesImpl.haveData(LANG) 217 ? "British English" 218 : (LocaleDisplayNamesImpl.haveData(REGION) 219 ? "en (United Kingdom)" 220 : "en (GB)"); 221 assertEquals("dialect 2", target, dn.localeDisplayName("en_GB")); 222 } 223 224 public void testLocaleKeywords() { 225 LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US, 226 DialectHandling.DIALECT_NAMES); 227 String name = dn.localeDisplayName("de@collation=phonebook"); 228 String target = LocaleDisplayNamesImpl.haveData(LANG) ? 229 "German (Phonebook Sort Order)" : "de (collation=phonebook)"; 230 assertEquals("collation", target, name); 231 232 } 233 } 234