public List<String> getExternalsWithResolutionOrder(Permutation permutation) { if (permutation == null) { return null; } List<String> result = new ArrayList<String>(4); result.add(permutation.toExternal()); Locale locale = permutation.getLocale(); if (locale != null) { String localeStr = locale.toString(); if (!result.contains(localeStr)) { result.add(localeStr); } if (locale.getVariant() != null && locale.getVariant().length() != 0) { Locale localeWithVar = new Locale(locale.getLanguage(), locale.getCountry()); localeStr = localeWithVar.toString(); if (!result.contains(localeStr)) { result.add(localeStr); } } if (locale.getCountry() != null && locale.getCountry().length() != 0) { Locale localeWithCountry = new Locale(locale.getLanguage()); localeStr = localeWithCountry.toString(); if (!result.contains(localeStr)) { result.add(localeStr); } } } result.add(""); return result; }
/** * a utility method to answer the name of a resource bundle according to the given base name and * locale * * @param baseName the given base name * @param locale the locale to use * @return the name of a resource bundle according to the given base name and locale */ public String toBundleName(String baseName, Locale locale) { final String emptyString = EMPTY_STRING; final String preString = UNDER_SCORE; final String underline = UNDER_SCORE; if (null == baseName) { throw new NullPointerException(); } StringBuilder ret = new StringBuilder(); StringBuilder prefix = new StringBuilder(); ret.append(baseName); if (!locale.getLanguage().equals(emptyString)) { ret.append(underline); ret.append(locale.getLanguage()); } else { prefix.append(preString); } if (!locale.getCountry().equals(emptyString)) { ret.append((CharSequence) prefix); ret.append(underline); ret.append(locale.getCountry()); prefix = new StringBuilder(); } else { prefix.append(preString); } if (!locale.getVariant().equals(emptyString)) { ret.append((CharSequence) prefix); ret.append(underline); ret.append(locale.getVariant()); } return ret.toString(); }
/** * Get the appropriate localized version of a file according to language settings e. g. help files * in jsp/help/ * * @param locale Locale to get the file for * @param fileName String fileName, to get the localized file for * @param fileType String file extension * @return localizedFileName String - localized filename */ private static String getFilename(Locale locale, String fileName, String fileType) { String localizedFileName = null; boolean fileFound = false; // with Language, Country, Variant String fileNameLCV = null; // with Language, Country String fileNameLC = null; // with Language String fileNameL = null; fileNameL = fileName + "_" + locale.getLanguage(); if (fileType == null) { fileType = ""; } if (!("".equals(locale.getCountry()))) { fileNameLC = fileName + "_" + locale.getLanguage() + "_" + locale.getCountry(); if (!("".equals(locale.getVariant()))) { fileNameLCV = fileName + "_" + locale.getLanguage() + "_" + locale.getCountry() + "_" + locale.getVariant(); } } if (fileNameLCV != null && !fileFound) { File fileTmp = new File(fileNameLCV + fileType); if (fileTmp.exists()) { fileFound = true; localizedFileName = fileNameLCV + fileType; } } if (fileNameLC != null && !fileFound) { File fileTmp = new File(fileNameLC + fileType); if (fileTmp.exists()) { fileFound = true; localizedFileName = fileNameLC + fileType; } } if (fileNameL != null && !fileFound) { File fileTmp = new File(fileNameL + fileType); if (fileTmp.exists()) { fileFound = true; localizedFileName = fileNameL + fileType; } } if (!fileFound) { localizedFileName = fileName + fileType; } return localizedFileName; }
public String toString(final Locale locale) { if (locale == null) { return null; } final String variant = locale.getVariant(); if (StringUtils.isEmpty(variant)) { return locale.getLanguage() + "_" + locale.getCountry(); } else { return locale.getLanguage() + "_" + locale.getCountry() + "_" + locale.getVariant(); } }
/** * this method imitates the orginal <code>ResourceBundle.getBundle(String className,Locale * locale)</code> which causes problems when the locale is changed to the base locale (english). * For a full description see ResourceBundle.getBundle(String className) in the java-api. */ public ResourceBundle loadResourceBundle(String className, Locale locale) throws MissingResourceException { String tries[] = new String[7]; StringBuffer buf = new StringBuffer(); tries[6] = className; buf.append(className); if (locale.getLanguage().length() > 0) { buf.append('_'); buf.append(locale.getLanguage()); tries[2] = buf.toString(); } if (locale.getCountry().length() > 0) { buf.append('_'); buf.append(locale.getCountry()); tries[1] = buf.toString(); } if (locale.getVariant().length() > 0) { buf.append('_'); buf.append(locale.getVariant()); tries[0] = buf.toString(); } buf.delete(className.length(), buf.length() - 1); Locale defaultLocale = Locale.getDefault(); if (defaultLocale.getLanguage().length() > 0) { buf.append(defaultLocale.getLanguage()); tries[5] = buf.toString(); } if (defaultLocale.getCountry().length() > 0) { buf.append('_'); buf.append(defaultLocale.getCountry()); tries[4] = buf.toString(); } if (defaultLocale.getVariant().length() > 0) { buf.append('_'); buf.append(defaultLocale.getVariant()); tries[3] = buf.toString(); } ResourceBundle bundle = null; for (int i = 0; i < tries.length; i++) { if (tries[i] == null) continue; bundle = loadBundle(tries[i]); if (bundle != null) { loadParent(tries, i, bundle); return bundle; } } throw new MissingResourceException( "'" + className + "' not found. The resource-file is missing.", className, ""); }
/** * Returns any occurrence of the specified Locale or any its fallback in the value collection, or * null if not found. By fallback, we mean will try without variant and country. Example, if * locale is zh_TW, it will try zh_TW and then zh. */ public static Locale getByFallback(Collection<Locale> values, Locale locale) { if (values.contains(locale)) return locale; final String lang = locale.getLanguage(); final String cnty = locale.getCountry(); final String var = locale.getVariant(); if (var != null && var.length() > 0) { locale = new Locale(lang, cnty); if (values.contains(locale)) return locale; } if (cnty != null && cnty.length() > 0) { locale = new Locale(lang, ""); if (values.contains(locale)) return locale; } // search the first one that matches partially Locale rtn = null; for (Locale l : values) { if (l.getLanguage().equals(lang)) { // case 1: it matches all but the last element -> done if (var == null || var.length() == 0 || Objects.equals(l.getCountry(), cnty)) // country might null return l; // case 2: it matches only language, we seeek for any case 1 if (rtn == null) rtn = l; } } return rtn; }
private static Locale _getSuperLocale(Locale locale) { String variant = locale.getVariant(); if (variant.length() > 0) { return new Locale(locale.getLanguage(), locale.getCountry()); } String country = locale.getCountry(); if (country.length() > 0) { Locale priorityLocale = LanguageUtil.getLocale(locale.getLanguage()); if ((priorityLocale != null) && !locale.equals(priorityLocale)) { return new Locale(priorityLocale.getLanguage(), priorityLocale.getCountry()); } return LocaleUtil.fromLanguageId(locale.getLanguage(), false, true); } String language = locale.getLanguage(); if (language.length() > 0) { return _blankLocale; } return null; }
@Override public void prepareModule() throws Exception { Context ctx = (Context) super.getServletRequest().getSession().getAttribute(ProfileConstants.context); Integer merchantid = ctx.getMerchantid(); ReferenceService rservice = (ReferenceService) ServiceFactory.getService(ServiceFactory.ReferenceService); Locale locale = getLocale(); String country = locale.getCountry(); if (locale.getVariant().equals("EUR")) { country = "X1"; } Map packages = ShippingUtil.buildPackageMap(moduleid, locale); if (packages != null) { setPackageMap(packages); } // get merchant configs MerchantService mservice = (MerchantService) ServiceFactory.getService(ServiceFactory.MerchantService); ConfigurationResponse config = mservice.getConfigurationByModule(moduleid, merchantid); this.setConfigurations(config); }
private MessageBundle createMessageBundle( final ApplicationKey applicationKey, final Locale locale) { final Properties props = new Properties(); if (locale == null) { props.putAll(loadBundle(applicationKey, "")); return new MessageBundleImpl(props); } String lang = locale.getLanguage(); String country = locale.getCountry(); String variant = locale.getVariant(); props.putAll(loadBundle(applicationKey, "")); if (StringUtils.isNotEmpty(lang)) { lang = lang.toLowerCase(); props.putAll(loadBundle(applicationKey, DELIMITER + lang)); } if (StringUtils.isNotEmpty(country)) { props.putAll(loadBundle(applicationKey, DELIMITER + lang + DELIMITER + country)); } if (StringUtils.isNotEmpty(variant)) { variant = variant.toLowerCase(); props.putAll( loadBundle(applicationKey, DELIMITER + lang + DELIMITER + country + DELIMITER + variant)); } return new MessageBundleImpl(props); }
private void assertLocale(String code, String language, String country, String variant) { Locale locale = Language.getLocale(code); assertEquals(code.replaceAll("-", "_"), locale.toString()); assertEquals(language, locale.getLanguage()); assertEquals(country, locale.getCountry()); assertEquals(variant, locale.getVariant()); }
private InputStream getHelpStream(Class c, String suffix) { Locale locale = Stapler.getCurrentRequest().getLocale(); String base = c.getName().replace('.', '/').replace('$', '/') + "/help" + suffix; ClassLoader cl = c.getClassLoader(); if (cl == null) return null; InputStream in; in = cl.getResourceAsStream( base + '_' + locale.getLanguage() + '_' + locale.getCountry() + '_' + locale.getVariant() + ".html"); if (in != null) return in; in = cl.getResourceAsStream( base + '_' + locale.getLanguage() + '_' + locale.getCountry() + ".html"); if (in != null) return in; in = cl.getResourceAsStream(base + '_' + locale.getLanguage() + ".html"); if (in != null) return in; // default return cl.getResourceAsStream(base + ".html"); }
/** * @bug 4122371 Confirm that Euro support works. This test is pretty rudimentary; all it does is * check that any locales with the EURO variant format a number using the Euro currency * symbol. * <p>ASSUME: All locales encode the Euro character "\u20AC". If this is changed to use the * single-character Euro symbol, this test must be updated. * <p>DON'T ASSUME: Any specific countries support the Euro. Instead, iterate through all * locales. */ public void TestEuroSupport() { final String EURO_VARIANT = "EURO"; final String EURO_CURRENCY = "\u20AC"; // Look for this string in formatted Euro currency Locale[] locales = NumberFormat.getAvailableLocales(); for (int i = 0; i < locales.length; ++i) { Locale loc = locales[i]; if (loc.getVariant().indexOf(EURO_VARIANT) >= 0) { NumberFormat nf = NumberFormat.getCurrencyInstance(loc); String pos = nf.format(271828.182845); String neg = nf.format(-271828.182845); if (pos.indexOf(EURO_CURRENCY) >= 0 && neg.indexOf(EURO_CURRENCY) >= 0) { logln("Ok: " + loc.toString() + ": " + pos + " / " + neg); } else { errln( "Fail: " + loc.toString() + " formats without " + EURO_CURRENCY + ": " + pos + " / " + neg + "\n*** THIS FAILURE MAY ONLY MEAN THAT LOCALE DATA HAS CHANGED ***"); } } } }
public static void buildResourceBundle(Locale locale) { ArrayList<String> fileList = new ArrayList<String>(); StringBuffer buffer = new StringBuffer(FILES_PREFIX); fileList.add(buffer.toString()); String language = locale.getLanguage(); if (language.length() > 0) { buffer.append('_'); buffer.append(language); fileList.add(buffer.toString()); String country = locale.getCountry(); if (country.length() > 0) { buffer.append('_'); buffer.append(country); fileList.add(buffer.toString()); String variant = locale.getVariant(); if (variant.length() > 0) { buffer.append('_'); buffer.append(variant); fileList.add(buffer.toString()); } } } langKeyMap = loadProperties(buffer.toString()); defaultLangKeyMap = loadProperties(FILES_PREFIX + "_" + DEFAULT_LOCALE); }
// SPR-11806 @Test public void testParseLocaleWithVariantContainingCountryCode() { String variant = "GBtest"; String localeString = "en_GB_" + variant; Locale locale = StringUtils.parseLocaleString(localeString); assertEquals( "Variant containing country code not extracted correctly", variant, locale.getVariant()); }
/** Test methods i18nManager.createLocal() */ @Test public void testCreateLocale() { // standard locale Locale loc = i18nMgr.createLocale("de"); assertNotNull(loc); assertEquals("de", loc.getLanguage()); assertEquals("", loc.getCountry()); assertEquals("", loc.getVariant()); // with country loc = i18nMgr.createLocale("de_CH"); assertNotNull(loc); assertEquals("de", loc.getLanguage()); assertEquals("CH", loc.getCountry()); assertEquals("", loc.getVariant()); // with variant loc = i18nMgr.createLocale("de_CH_ZH"); assertNotNull(loc); assertEquals("de", loc.getLanguage()); assertEquals("CH", loc.getCountry()); assertEquals("ZH", loc.getVariant()); // with variant but no country loc = i18nMgr.createLocale("de__VENDOR"); assertNotNull(loc); assertEquals("de", loc.getLanguage()); assertEquals("", loc.getCountry()); assertEquals("VENDOR", loc.getVariant()); // // With overlay // with language String overlay = I18nModule.getOverlayName(); loc = i18nMgr.createLocale("de"); Locale over = i18nMgr.createOverlay(loc); assertEquals("de____" + overlay, over.toString()); assertEquals(i18nMgr.createOverlayKeyForLanguage(loc.toString()), i18nMgr.getLocaleKey(over)); // with country loc = i18nMgr.createLocale("de_CH"); over = i18nMgr.createOverlay(loc); assertEquals("de_CH___" + overlay, over.toString()); assertEquals(i18nMgr.createOverlayKeyForLanguage(loc.toString()), i18nMgr.getLocaleKey(over)); // with variant loc = i18nMgr.createLocale("de_CH_ZH"); over = i18nMgr.createOverlay(loc); assertEquals("de_CH_ZH__" + overlay, over.toString()); assertEquals(i18nMgr.createOverlayKeyForLanguage(loc.toString()), i18nMgr.getLocaleKey(over)); }
public void TestBasicGetters() { for (int i = 0; i <= MAX_LOCALES; i++) { Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]); logln("Testing " + testLocale + "..."); if (!testLocale.getLanguage().equals(dataTable[LANG][i])) errln( " Language code mismatch: " + testLocale.getLanguage() + " versus " + dataTable[LANG][i]); if (!testLocale.getCountry().equals(dataTable[CTRY][i])) errln( " Country code mismatch: " + testLocale.getCountry() + " versus " + dataTable[CTRY][i]); if (!testLocale.getVariant().equals(dataTable[VAR][i])) errln( " Variant code mismatch: " + testLocale.getVariant() + " versus " + dataTable[VAR][i]); if (!testLocale.toString().equals(dataTable[NAME][i])) errln(" Locale name mismatch: " + testLocale.toString() + " versus " + dataTable[NAME][i]); } logln("Same thing without variant codes..."); for (int i = 0; i <= MAX_LOCALES; i++) { Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i]); logln("Testing " + testLocale + "..."); if (!testLocale.getLanguage().equals(dataTable[LANG][i])) errln( " Language code mismatch: " + testLocale.getLanguage() + " versus " + dataTable[LANG][i]); if (!testLocale.getCountry().equals(dataTable[CTRY][i])) errln( " Country code mismatch: " + testLocale.getCountry() + " versus " + dataTable[CTRY][i]); if (!testLocale.getVariant().equals("")) errln(" Variant code mismatch: " + testLocale.getVariant() + " versus \"\""); } }
private String formatLocale(Locale l) { return (l.getDisplayName() + " : " + l.getLanguage() + ' ' + l.getCountry() + ' ' + l.getVariant()); }
private static void showLocale(Locale locale) { System.out.print("Locale(\""); System.out.print(locale.getLanguage()); System.out.print("\",\""); System.out.print(locale.getCountry()); System.out.print("\",\""); System.out.print(locale.getVariant()); System.out.print("\") \t "); System.out.println(locale.getDisplayName()); }
/** * Find a Locale that matches the supplied language / country / variant triple. * * @param language the language for the required Locale * @param country the country code for the required Locale * @param variant the variant for the required Locale * @return the requested Locale, or <code>null</code> */ protected Locale findLocale(String language, String country, String variant) { for (Locale l : Locale.getAvailableLocales()) { if (l.getCountry().equals(country) && l.getLanguage().equals(language) && l.getVariant().equals(variant)) { return l; } } return null; }
// SPR-3671 @Test public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception { final String variant = "proper northern"; final String localeString = "en GB " + variant; Locale locale = StringUtils.parseLocaleString(localeString); assertEquals( "Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); }
// SPR-8637 @Test public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception { final String variant = "proper-northern"; final String localeString = "en_GB_" + variant; Locale locale = StringUtils.parseLocaleString(localeString); assertEquals( "Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); }
protected static boolean isSameLocale( Locale locale, String language, String country, String variant) { try { return (locale.getLanguage().equals(language) && locale.getCountry().equals(country) && locale.getVariant().equals(variant)); } catch (NullPointerException npe) { // Shouldn't ever happen, no nulls return false; } }
// http://b/2611311; if there's no display language/country/variant, use the raw codes. public void test_getDisplayName_unknown() throws Exception { Locale unknown = new Locale("xx", "YY", "Traditional"); assertEquals("xx", unknown.getLanguage()); assertEquals("YY", unknown.getCountry()); assertEquals("Traditional", unknown.getVariant()); assertEquals("xx", unknown.getDisplayLanguage()); assertEquals("YY", unknown.getDisplayCountry()); assertEquals("TRADITIONAL", unknown.getDisplayVariant()); assertEquals("xx (YY,TRADITIONAL)", unknown.getDisplayName()); }
/** * Tries to load the bundle for a given locale, also loads the backup locales with the same * language. * * @param baseName the raw bundle name, without locale qualifiers * @param locale the locale * @param classLoader the classloader * @param wantBase whether a resource bundle made only from the base name (with no locale * information attached) should be returned. * @return the resource bundle if it was loaded, otherwise the backup */ private static ResourceBundle tryBundle( String baseName, Locale locale, ClassLoader classLoader, boolean wantBase) { String language = locale.getLanguage(); String country = locale.getCountry(); String variant = locale.getVariant(); int baseLen = baseName.length(); // Build up a CPStringBuilder containing the complete bundle name, fully // qualified by locale. CPStringBuilder sb = new CPStringBuilder(baseLen + variant.length() + 7); sb.append(baseName); if (language.length() > 0) { sb.append('_'); sb.append(language); if (country.length() > 0) { sb.append('_'); sb.append(country); if (variant.length() > 0) { sb.append('_'); sb.append(variant); } } } // Now try to load bundles, starting with the most specialized name. // Build up the parent chain as we go. String bundleName = sb.toString(); ResourceBundle first = null; // The most specialized bundle. ResourceBundle last = null; // The least specialized bundle. while (true) { ResourceBundle foundBundle = tryBundle(bundleName, classLoader); if (foundBundle != null) { if (first == null) first = foundBundle; if (last != null) last.parent = foundBundle; foundBundle.locale = locale; last = foundBundle; } int idx = bundleName.lastIndexOf('_'); // Try the non-localized base name only if we already have a // localized child bundle, or wantBase is true. if (idx > baseLen || (idx == baseLen && (first != null || wantBase))) bundleName = bundleName.substring(0, idx); else break; } return first; }
// SPR-3671 @Test public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception { final String variant = "proper_northern"; final String localeString = "en_GB_____" + variant; // lots of underscores Locale locale = StringUtils.parseLocaleString(localeString); assertEquals( "Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); }
/** * Test for lax Locale equality. More precisely, returns true if (a) both are equal; (b) general * only specifies language, and specific has the same language; (c) general specifies language and * country, and specific has the same language and country. Else returns false. */ public static boolean subsumes(Locale general, Locale specific) { if (general == null || specific == null) return false; if (general.equals(specific)) return true; else if (general.getVariant().equals("")) { if (general.getCountry().equals("")) { if (general.getLanguage().equals(specific.getLanguage())) return true; } else { if (general.getLanguage().equals(specific.getLanguage()) && general.getCountry().equals(specific.getCountry())) return true; } } return false; }
/** * Return the resource file suffic for the indicated locale For most locales, this will be based * the language code. However for Chinese, we do distinguish between Taiwan and PRC * * @param locale the locale * @return an String suffix which canbe appended to a resource name */ private static final String getResourceSuffix(Locale locale) { String lang = locale.getLanguage(); String country = locale.getCountry(); String variant = locale.getVariant(); String suffix = "_" + locale.getLanguage(); if (lang.equals("zh")) suffix += "_" + country; if (country.equals("JP")) suffix += "_" + country + "_" + variant; return suffix; }
/** * Gets the locale file name suffix for instance '_de_DE' for the Locale.GERMANY. * * @param locale the locale * @param withCountry with country * @param withVariant with variant * @param withUnderscorePrefix true if the result has to have the underscore prefix * @return the locale file suffix */ public static String getLocaleFileSuffix( final Locale locale, final boolean withCountry, final boolean withVariant, final boolean withUnderscorePrefix) { final StringBuilder localizedName = new StringBuilder(); if (locale != null) { if (locale.getLanguage() != null && !locale.getLanguage().isEmpty()) { if (withUnderscorePrefix) { localizedName.append("_"); } localizedName.append(locale.getLanguage()); } if (withCountry && locale.getCountry() != null && !locale.getCountry().isEmpty()) { localizedName.append("_"); localizedName.append(locale.getCountry()); } if (withVariant && locale.getVariant() != null && !locale.getVariant().isEmpty()) { localizedName.append("_"); localizedName.append(locale.getVariant()); } } return localizedName.toString().trim(); }
// http://b/2611311; if there's no display language/country/variant, use the raw codes. public void test_getDisplayName_invalid() throws Exception { Locale invalid = new Locale("AaBbCc", "DdEeFf", "GgHhIi"); assertEquals("aabbcc", invalid.getLanguage()); assertEquals("DDEEFF", invalid.getCountry()); assertEquals("GgHhIi", invalid.getVariant()); // Android using icu4c < 49.2 returned empty strings for display language, country, // and variant, but a display name made up of the raw strings. // Newer releases return slightly different results, but no less unreasonable. assertEquals("aabbcc", invalid.getDisplayLanguage()); assertEquals("", invalid.getDisplayCountry()); assertEquals("DDEEFF_GGHHII", invalid.getDisplayVariant()); assertEquals("aabbcc (DDEEFF,DDEEFF_GGHHII)", invalid.getDisplayName()); }
/** * Obtains the list of locales to search through when performing a locale search. * * <pre> * localeLookupList(Locale("fr", "CA", "xxx"), Locale("en")) * = [Locale("fr","CA","xxx"), Locale("fr","CA"), Locale("fr"), Locale("en"] * </pre> * * <p>The result list begins with the most specific locale, then the next more general and so on, * finishing with the default locale. The list will never contain the same locale twice. * * @param locale the locale to start from, null returns empty list * @param defaultLocale the default locale to use if no other is found * @return the unmodifiable list of Locale objects, 0 being locale, not null */ public static List<Locale> localeLookupList(final Locale locale, final Locale defaultLocale) { final List<Locale> list = new ArrayList<Locale>(4); if (locale != null) { list.add(locale); if (locale.getVariant().length() > 0) { list.add(new Locale(locale.getLanguage(), locale.getCountry())); } if (locale.getCountry().length() > 0) { list.add(new Locale(locale.getLanguage(), "")); } if (list.contains(defaultLocale) == false) { list.add(defaultLocale); } } return Collections.unmodifiableList(list); }