/** * Ensure that all current locale ids parse. Use DateFormat as a proxy * for all current locale ids. */ public void testCurrentLocales() { Locale[] locales = java.text.DateFormat.getAvailableLocales(); Builder builder = new Builder(); for (Locale target : locales) { String tag = target.toLanguageTag(); // the tag recreates the original locale, // except no_NO_NY Locale tagResult = Locale.forLanguageTag(tag); if (!target.getVariant().equals("NY")) { assertEquals("tagResult", target, tagResult); } // the builder also recreates the original locale, // except ja_JP_JP, th_TH_TH and no_NO_NY Locale builderResult = builder.setLocale(target).build(); if (target.getVariant().length() != 2) { assertEquals("builderResult", target, builderResult); } } }
public void testGetExtension() { // forLanguageTag does NOT normalize to hyphen Locale locale = Locale.forLanguageTag("und-a-some_ex-tension"); assertEquals("some_ex-tension", null, locale.getExtension('a')); // regular extension locale = new Builder().setExtension('a', "some-ex-tension").build(); assertEquals("builder", "some-ex-tension", locale.getExtension('a')); // returns null if extension is not present assertEquals("empty b", null, locale.getExtension('b')); // throws exception if extension tag is illegal new ExpectIAE() { public void call() { Locale.forLanguageTag("").getExtension('\uD800'); }}; // 'x' is not an extension, it's a private use tag, but it's accessed through this API locale = Locale.forLanguageTag("x-y-z-blork"); assertEquals("x", "y-z-blork", locale.getExtension('x')); }
public void testBuilderSetLanguageTag() { String source = "eN-LaTn-Us-NewYork-A-Xx-B-Yy-X-1-2-3"; String target = "en-Latn-US-NewYork-a-xx-b-yy-x-1-2-3"; Builder builder = new Builder(); String result = builder .setLanguageTag(source) .build() .toLanguageTag(); assertEquals("language", target, result); // redundant extensions cause a failure new BuilderILE() { public void call() { b.setLanguageTag("und-a-xx-yy-b-ww-A-00-11-c-vv"); }}; // redundant Unicode locale extension keys within an Unicode locale extension cause a failure new BuilderILE() { public void call() { b.setLanguageTag("und-u-nu-thai-NU-chinese-xx-1234"); }}; }
/** * Returns an instance of Locale used for service look up. * The result Locale has no extensions except for ja_JP_JP * and th_TH_TH * * @param locale the locale * @return the locale used for service look up */ static Locale getLookupLocale(Locale locale) { Locale lookupLocale = locale; if (locale.hasExtensions() && !locale.equals(JRELocaleConstants.JA_JP_JP) && !locale.equals(JRELocaleConstants.TH_TH_TH)) { // remove extensions Builder locbld = new Builder(); try { locbld.setLocale(locale); locbld.clearExtensions(); lookupLocale = locbld.build(); } catch (IllformedLocaleException e) { // A Locale with non-empty extensions // should have well-formed fields except // for ja_JP_JP and th_TH_TH. Therefore, // it should never enter in this catch clause. config(LocaleServiceProviderPool.class, "A locale(" + locale + ") has non-empty extensions, but has illformed fields."); // Fallback - script field will be lost. lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant()); } } return lookupLocale; }
public static Set<Locale> get(String language, String country) { if(country == null) return language(language); Map<String, Set<Locale>> map = byLanguageCountry.get(language); if(map == null) { map = new HashMap<String, Set<Locale>>(); byLanguageCountry.put(language, map); } Set<Locale> locales = map.get(country); if(locales != null) return locales; if(builder == null) builder = new Locale.Builder(); synchronized(builder) { builder.clear(); builder.setLanguage(language); builder.setRegion(country); put(builder.build()); } return map.get(country); }
private static LangTag asLangTag(Locale.Builder locBuild) { Locale lc = locBuild.build(); Set<Character> extkeys = lc.getExtensionKeys(); StringBuilder sb = new StringBuilder(); extkeys.forEach(k->{ String ext = lc.getExtension(k); if ( sb.length() != 0 ) sb.append('-'); sb.append(k); sb.append('-'); sb.append(ext); }); String extension = sb.toString(); return new LangTag(lc.getLanguage(), lc.getScript(), lc.getCountry(), lc.getVariant(), extension); }
/** * Returns an instance of Locale used for service look up. * The result Locale has no extensions except for ja_JP_JP * and th_TH_TH * * @param locale the locale * @return the locale used for service look up */ private static Locale getLookupLocale(Locale locale) { Locale lookupLocale = locale; Set<Character> extensions = locale.getExtensionKeys(); if (!extensions.isEmpty() && !locale.equals(locale_ja_JP_JP) && !locale.equals(locale_th_TH_TH)) { // remove extensions Builder locbld = new Builder(); try { locbld.setLocale(locale); locbld.clearExtensions(); lookupLocale = locbld.build(); } catch (IllformedLocaleException e) { // A Locale with non-empty extensions // should have well-formed fields except // for ja_JP_JP and th_TH_TH. Therefore, // it should never enter in this catch clause. config("A locale(" + locale + ") has non-empty extensions, but has illformed fields."); // Fallback - script field will be lost. lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant()); } } return lookupLocale; }