1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2009-2016, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.text; 11 12 import java.lang.reflect.InvocationTargetException; 13 import java.lang.reflect.Method; 14 import java.util.Collections; 15 import java.util.Comparator; 16 import java.util.List; 17 import java.util.Locale; 18 import java.util.Set; 19 20 import android.icu.impl.ICUConfig; 21 import android.icu.lang.UScript; 22 import android.icu.text.DisplayContext.Type; 23 import android.icu.util.IllformedLocaleException; 24 import android.icu.util.ULocale; 25 26 /** 27 * Returns display names of ULocales and components of ULocales. For 28 * more information on language, script, region, variant, key, and 29 * values, see {@link android.icu.util.ULocale}. 30 */ 31 public abstract class LocaleDisplayNames { 32 /** 33 * Enum used in {@link #getInstance(ULocale, DialectHandling)}. 34 */ 35 public enum DialectHandling { 36 /** 37 * Use standard names when generating a locale name, 38 * e.g. en_GB displays as 'English (United Kingdom)'. 39 */ 40 STANDARD_NAMES, 41 /** 42 * Use dialect names when generating a locale name, 43 * e.g. en_GB displays as 'British English'. 44 */ 45 DIALECT_NAMES 46 } 47 48 // factory methods 49 /** 50 * Convenience overload of {@link #getInstance(ULocale, DialectHandling)} that specifies 51 * STANDARD dialect handling. 52 * @param locale the display locale 53 * @return a LocaleDisplayNames instance 54 */ 55 public static LocaleDisplayNames getInstance(ULocale locale) { 56 return getInstance(locale, DialectHandling.STANDARD_NAMES); 57 }; 58 59 /** 60 * Convenience overload of {@link #getInstance(Locale, DisplayContext...)} that specifies 61 * {@link DisplayContext#STANDARD_NAMES}. 62 * @param locale the display {@link java.util.Locale} 63 * @return a LocaleDisplayNames instance 64 */ 65 public static LocaleDisplayNames getInstance(Locale locale) { 66 return getInstance(ULocale.forLocale(locale)); 67 }; 68 69 /** 70 * Returns an instance of LocaleDisplayNames that returns names formatted for the provided locale, 71 * using the provided dialectHandling. 72 * @param locale the display locale 73 * @param dialectHandling how to select names for locales 74 * @return a LocaleDisplayNames instance 75 */ 76 public static LocaleDisplayNames getInstance(ULocale locale, DialectHandling dialectHandling) { 77 LocaleDisplayNames result = null; 78 if (FACTORY_DIALECTHANDLING != null) { 79 try { 80 result = (LocaleDisplayNames) FACTORY_DIALECTHANDLING.invoke(null, 81 locale, dialectHandling); 82 } catch (InvocationTargetException e) { 83 // fall through 84 } catch (IllegalAccessException e) { 85 // fall through 86 } 87 } 88 if (result == null) { 89 result = new LastResortLocaleDisplayNames(locale, dialectHandling); 90 } 91 return result; 92 } 93 94 /** 95 * Returns an instance of LocaleDisplayNames that returns names formatted for the provided locale, 96 * using the provided DisplayContext settings 97 * @param locale the display locale 98 * @param contexts one or more context settings (e.g. for dialect 99 * handling, capitalization, etc. 100 * @return a LocaleDisplayNames instance 101 */ 102 public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) { 103 LocaleDisplayNames result = null; 104 if (FACTORY_DISPLAYCONTEXT != null) { 105 try { 106 result = (LocaleDisplayNames) FACTORY_DISPLAYCONTEXT.invoke(null, 107 locale, contexts); 108 } catch (InvocationTargetException e) { 109 // fall through 110 } catch (IllegalAccessException e) { 111 // fall through 112 } 113 } 114 if (result == null) { 115 result = new LastResortLocaleDisplayNames(locale, contexts); 116 } 117 return result; 118 } 119 120 /** 121 * Returns an instance of LocaleDisplayNames that returns names formatted for the provided 122 * {@link java.util.Locale}, using the provided DisplayContext settings 123 * @param locale the display {@link java.util.Locale} 124 * @param contexts one or more context settings (e.g. for dialect 125 * handling, capitalization, etc. 126 * @return a LocaleDisplayNames instance 127 */ 128 public static LocaleDisplayNames getInstance(Locale locale, DisplayContext... contexts) { 129 return getInstance(ULocale.forLocale(locale), contexts); 130 } 131 132 // getters for state 133 /** 134 * Returns the locale used to determine the display names. This is not necessarily the same 135 * locale passed to {@link #getInstance}. 136 * @return the display locale 137 */ 138 public abstract ULocale getLocale(); 139 140 /** 141 * Returns the dialect handling used in the display names. 142 * @return the dialect handling enum 143 */ 144 public abstract DialectHandling getDialectHandling(); 145 146 /** 147 * Returns the current value for a specified DisplayContext.Type. 148 * @param type the DisplayContext.Type whose value to return 149 * @return the current DisplayContext setting for the specified type 150 */ 151 public abstract DisplayContext getContext(DisplayContext.Type type); 152 153 // names for entire locales 154 /** 155 * Returns the display name of the provided ulocale. 156 * When no display names are available for all or portions 157 * of the original locale ID, those portions may be 158 * used directly (possibly in a more canonical form) as 159 * part of the returned display name. 160 * @param locale the locale whose display name to return 161 * @return the display name of the provided locale 162 */ 163 public abstract String localeDisplayName(ULocale locale); 164 165 /** 166 * Returns the display name of the provided locale. 167 * When no display names are available for all or portions 168 * of the original locale ID, those portions may be 169 * used directly (possibly in a more canonical form) as 170 * part of the returned display name. 171 * @param locale the locale whose display name to return 172 * @return the display name of the provided locale 173 */ 174 public abstract String localeDisplayName(Locale locale); 175 176 /** 177 * Returns the display name of the provided locale id. 178 * When no display names are available for all or portions 179 * of the original locale ID, those portions may be 180 * used directly (possibly in a more canonical form) as 181 * part of the returned display name. 182 * @param localeId the id of the locale whose display name to return 183 * @return the display name of the provided locale 184 */ 185 public abstract String localeDisplayName(String localeId); 186 187 // names for components of a locale id 188 /** 189 * Returns the display name of the provided language code. 190 * @param lang the language code 191 * @return the display name of the provided language code 192 */ 193 public abstract String languageDisplayName(String lang); 194 195 /** 196 * Returns the display name of the provided script code. 197 * @param script the script code 198 * @return the display name of the provided script code 199 */ 200 public abstract String scriptDisplayName(String script); 201 202 /** 203 * Returns the display name of the provided script code 204 * when used in the context of a full locale name. 205 * @param script the script code 206 * @return the display name of the provided script code 207 * @deprecated This API is ICU internal only. 208 * @hide original deprecated declaration 209 * @hide draft / provisional / internal are hidden on Android 210 */ 211 @Deprecated 212 public String scriptDisplayNameInContext(String script) { 213 return scriptDisplayName(script); 214 } 215 216 /** 217 * Returns the display name of the provided script code. See 218 * {@link android.icu.lang.UScript} for recognized script codes. 219 * @param scriptCode the script code number 220 * @return the display name of the provided script code 221 */ 222 public abstract String scriptDisplayName(int scriptCode); 223 224 /** 225 * Returns the display name of the provided region code. 226 * @param region the region code 227 * @return the display name of the provided region code 228 */ 229 public abstract String regionDisplayName(String region); 230 231 /** 232 * Returns the display name of the provided variant. 233 * @param variant the variant string 234 * @return the display name of the provided variant 235 */ 236 public abstract String variantDisplayName(String variant); 237 238 /** 239 * Returns the display name of the provided locale key. 240 * @param key the locale key name 241 * @return the display name of the provided locale key 242 */ 243 public abstract String keyDisplayName(String key); 244 245 /** 246 * Returns the display name of the provided value (used with the provided key). 247 * @param key the locale key name 248 * @param value the locale key's value 249 * @return the display name of the provided value 250 */ 251 public abstract String keyValueDisplayName(String key, String value); 252 253 254 /** 255 * Return a list of information used to construct a UI list of locale names. 256 * @param collator how to collateshould normally be Collator.getInstance(getDisplayLocale()) 257 * @param inSelf if true, compares the nameInSelf, otherwise the nameInDisplayLocale. 258 * Set depending on which field (displayLocale vs self) is to show up in the UI. 259 * If both are to show up in the UI, then it should be the one used for the primary sort order. 260 * @param localeSet a list of locales to present in a UI list. The casing uses the settings in the LocaleDisplayNames instance. 261 * @return an ordered list of UiListItems. 262 * @throws IllformedLocaleException if any of the locales in localeSet are malformed. 263 */ 264 public List<UiListItem> getUiList(Set<ULocale> localeSet, boolean inSelf, Comparator<Object> collator) { 265 return getUiListCompareWholeItems(localeSet, UiListItem.getComparator(collator, inSelf)); 266 } 267 268 /** 269 * Return a list of information used to construct a UI list of locale names, providing more access to control the sorting. 270 * Normally use getUiList instead. 271 * @param comparator how to sort the UiListItems in the result. 272 * @param localeSet a list of locales to present in a UI list. The casing uses the settings in the LocaleDisplayNames instance. 273 * @return an ordered list of UiListItems. 274 * @throws IllformedLocaleException if any of the locales in localeSet are malformed. 275 */ 276 public abstract List<UiListItem> getUiListCompareWholeItems(Set<ULocale> localeSet, Comparator<UiListItem> comparator); 277 278 /** 279 * Struct-like class used to return information for constructing a UI list, each corresponding to a locale. 280 */ 281 public static class UiListItem { 282 /** 283 * Returns the minimized locale for an input locale, such as sr-Cyrl sr 284 */ 285 public final ULocale minimized; 286 /** 287 * Returns the modified locale for an input locale, such as sr sr-Cyrl, where there is also an sr-Latn in the list 288 */ 289 public final ULocale modified; 290 /** 291 * Returns the name of the modified locale in the display locale, such as "Englisch (VS)" (for 'en-US', where the display locale is 'de'). 292 */ 293 public final String nameInDisplayLocale; 294 /** 295 * Returns the name of the modified locale in itself, such as "English (US)" (for 'en-US'). 296 */ 297 public final String nameInSelf; 298 299 /** 300 * Constructor, normally only called internally. 301 * @param minimized locale for an input locale 302 * @param modified modified for an input locale 303 * @param nameInDisplayLocale name of the modified locale in the display locale 304 * @param nameInSelf name of the modified locale in itself 305 */ 306 public UiListItem(ULocale minimized, ULocale modified, String nameInDisplayLocale, String nameInSelf) { 307 this.minimized = minimized; 308 this.modified = modified; 309 this.nameInDisplayLocale = nameInDisplayLocale; 310 this.nameInSelf = nameInSelf; 311 } 312 313 /** 314 * {@inheritDoc} 315 */ 316 @Override 317 public boolean equals(Object obj) { 318 if (this == obj) { 319 return true; 320 } 321 if (obj == null || !(obj instanceof UiListItem)) { 322 return false; 323 } 324 UiListItem other = (UiListItem)obj; 325 return nameInDisplayLocale.equals(other.nameInDisplayLocale) 326 && nameInSelf.equals(other.nameInSelf) 327 && minimized.equals(other.minimized) 328 && modified.equals(other.modified); 329 } 330 331 /** 332 * {@inheritDoc} 333 */ 334 @Override 335 public int hashCode() { 336 return modified.hashCode() ^ nameInDisplayLocale.hashCode(); 337 } 338 339 /** 340 * {@inheritDoc} 341 */ 342 @Override 343 public String toString() { 344 return "{" + minimized + ", " + modified + ", " + nameInDisplayLocale + ", " + nameInSelf + "}"; 345 } 346 347 /** 348 * Return a comparator that compares the locale names for the display locale or the in-self names, 349 * depending on an input parameter. 350 * @param inSelf if true, compares the nameInSelf, otherwise the nameInDisplayLocale 351 * @param comparator (meant for strings, but because Java Collator doesn't have <String>...) 352 * @return UiListItem comparator 353 */ 354 public static Comparator<UiListItem> getComparator(Comparator<Object> comparator, boolean inSelf) { 355 return new UiListItemComparator(comparator, inSelf); 356 } 357 358 private static class UiListItemComparator implements Comparator<UiListItem> { 359 private final Comparator<Object> collator; 360 private final boolean useSelf; 361 UiListItemComparator(Comparator<Object> collator, boolean useSelf) { 362 this.collator = collator; 363 this.useSelf = useSelf; 364 } 365 @Override 366 public int compare(UiListItem o1, UiListItem o2) { 367 int result = useSelf ? collator.compare(o1.nameInSelf, o2.nameInSelf) 368 : collator.compare(o1.nameInDisplayLocale, o2.nameInDisplayLocale); 369 return result != 0 ? result : o1.modified.compareTo(o2.modified); // just in case 370 } 371 } 372 } 373 /** 374 * Sole constructor. (For invocation by subclass constructors, 375 * typically implicit.) 376 * @deprecated This API is ICU internal only. 377 * @hide original deprecated declaration 378 * @hide draft / provisional / internal are hidden on Android 379 */ 380 @Deprecated 381 protected LocaleDisplayNames() { 382 } 383 384 private static final Method FACTORY_DIALECTHANDLING; 385 private static final Method FACTORY_DISPLAYCONTEXT; 386 387 static { 388 String implClassName = ICUConfig.get("android.icu.text.LocaleDisplayNames.impl", "android.icu.impl.LocaleDisplayNamesImpl"); 389 390 Method factoryDialectHandling = null; 391 Method factoryDisplayContext = null; 392 393 try { 394 Class<?> implClass = Class.forName(implClassName); 395 try { 396 factoryDialectHandling = implClass.getMethod("getInstance", 397 ULocale.class, DialectHandling.class); 398 } catch (NoSuchMethodException e) { 399 } 400 try { 401 factoryDisplayContext = implClass.getMethod("getInstance", 402 ULocale.class, DisplayContext[].class); 403 } catch (NoSuchMethodException e) { 404 } 405 406 } catch (ClassNotFoundException e) { 407 // fallback to last resort impl 408 } 409 410 FACTORY_DIALECTHANDLING = factoryDialectHandling; 411 FACTORY_DISPLAYCONTEXT = factoryDisplayContext; 412 } 413 414 /** 415 * Minimum implementation of LocaleDisplayNames 416 */ 417 private static class LastResortLocaleDisplayNames extends LocaleDisplayNames { 418 419 private ULocale locale; 420 private DisplayContext[] contexts; 421 422 private LastResortLocaleDisplayNames(ULocale locale, DialectHandling dialectHandling) { 423 this.locale = locale; 424 DisplayContext context = (dialectHandling == DialectHandling.DIALECT_NAMES) ? 425 DisplayContext.DIALECT_NAMES : DisplayContext.STANDARD_NAMES; 426 this.contexts = new DisplayContext[] {context}; 427 } 428 429 private LastResortLocaleDisplayNames(ULocale locale, DisplayContext... contexts) { 430 this.locale = locale; 431 this.contexts = new DisplayContext[contexts.length]; 432 System.arraycopy(contexts, 0, this.contexts, 0, contexts.length); 433 } 434 435 @Override 436 public ULocale getLocale() { 437 return locale; 438 } 439 440 @Override 441 public DialectHandling getDialectHandling() { 442 DialectHandling result = DialectHandling.STANDARD_NAMES; 443 for (DisplayContext context : contexts) { 444 if (context.type() == DisplayContext.Type.DIALECT_HANDLING) { 445 if (context.value() == DisplayContext.DIALECT_NAMES.ordinal()) { 446 result = DialectHandling.DIALECT_NAMES; 447 break; 448 } 449 } 450 } 451 return result; 452 } 453 454 @Override 455 public DisplayContext getContext(Type type) { 456 DisplayContext result = DisplayContext.STANDARD_NAMES; // final fallback 457 for (DisplayContext context : contexts) { 458 if (context.type() == type) { 459 result = context; 460 break; 461 } 462 } 463 return result; 464 } 465 466 @Override 467 public String localeDisplayName(ULocale locale) { 468 return locale.getName(); 469 } 470 471 @Override 472 public String localeDisplayName(Locale locale) { 473 return ULocale.forLocale(locale).getName(); 474 } 475 476 @Override 477 public String localeDisplayName(String localeId) { 478 return new ULocale(localeId).getName(); 479 } 480 481 @Override 482 public String languageDisplayName(String lang) { 483 return lang; 484 } 485 486 @Override 487 public String scriptDisplayName(String script) { 488 return script; 489 } 490 491 @Override 492 public String scriptDisplayName(int scriptCode) { 493 return UScript.getShortName(scriptCode); 494 } 495 496 @Override 497 public String regionDisplayName(String region) { 498 return region; 499 } 500 501 @Override 502 public String variantDisplayName(String variant) { 503 return variant; 504 } 505 506 @Override 507 public String keyDisplayName(String key) { 508 return key; 509 } 510 511 @Override 512 public String keyValueDisplayName(String key, String value) { 513 return value; 514 } 515 516 @Override 517 public List<UiListItem> getUiListCompareWholeItems(Set<ULocale> localeSet, Comparator<UiListItem> comparator) { 518 return Collections.emptyList(); 519 } 520 } 521 } 522