/** * Determine the RFC 3066 compliant language tag, * as used for the HTTP "Accept-Language" header. * @param locale the Locale to transform to a language tag * @return the RFC 3066 compliant language tag as {@code String} * @deprecated as of 5.0.4, in favor of {@link Locale#toLanguageTag()} */ @Deprecated public static String toLanguageTag(Locale locale) { return locale.getLanguage() + (hasText(locale.getCountry()) ? "-" + locale.getCountry() : ""); }
public class MyApplication extends Application { public static String sDefSystemLanguage; @Override public void onCreate() { super.onCreate(); sDefSystemLanguage = Locale.getDefault().getLanguage(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); sDefSystemLanguage = newConfig.locale.getLanguage(); } }
/** * Resolves locale code from locale. */ public static String resolveLocaleCode(Locale locale) { return resolveLocaleCode(locale.getLanguage(), locale.getCountry(), locale.getVariant()); }
/** * Only the given locale is searched. Contrary to java.util.ResourceBundle, no strategy for locating the bundle is implemented in * this method. */ String messageFromFile(Locale locale, String filename, String relatedProperty) { String result = null; String bundleBase = propertyToBundles.get(relatedProperty); if (bundleBase == null) { // this property has no translation return null; } String filePath = bundleBase.replace('.', '/'); if (!"en".equals(locale.getLanguage())) { filePath += "_" + locale.getLanguage(); } filePath += "/" + filename; InputStream input = classloader.getResourceAsStream(filePath); if (input != null) { result = readInputStream(filePath, input); } return result; }
/** * Get the feature processor manager associated with the given locale, if any. * * @param locale * locale * @return the feature processor manager, or null if there is no locale-specific feature processor manager. */ public static FeatureProcessorManager getFeatureProcessorManager(Locale locale) { FeatureProcessorManager m = managersByLocale.get(locale); if (m != null) return m; // Maybe locale is language_COUNTRY, so look up by language also: Locale lang = new Locale(locale.getLanguage()); return managersByLocale.get(lang); }
for (String namespace : config.getLanguageNamespaces()) { String filename = locale.getLanguage() + "_" + locale.getCountry() + File.separator + namespace + ".json"; filename = locale.getLanguage() + File.separator + namespace + ".json"; r = getBaseDirectory().createRelative(filename); return languageMaps.get(locale);
/** * Returns a locale that's one less specific, or {@code null} if there's no less specific locale. */ public static Locale getLessSpecificLocale(Locale locale) { String country = locale.getCountry(); if (locale.getVariant().length() != 0) { String language = locale.getLanguage(); return country != null ? new Locale(language, country) : new Locale(language); } if (country.length() != 0) { return new Locale(locale.getLanguage()); } return null; }
/** * Accept-Language: zh-CN,zh;q=0.8 */ public static String getAcceptLanguage() { if (TextUtils.isEmpty(acceptLanguage)) { Locale locale = Locale.getDefault(); String language = locale.getLanguage(); String country = locale.getCountry(); StringBuilder acceptLanguageBuilder = new StringBuilder(language); if (!TextUtils.isEmpty(country)) acceptLanguageBuilder.append('-').append(country).append(',').append(language).append(";q=0.8"); acceptLanguage = acceptLanguageBuilder.toString(); return acceptLanguage; } return acceptLanguage; }
// may contain simple syntax error, i dont have java rite now to test.. // but this is a bigger picture for ur algo... public String localeToString(Locale l) { return l.getLanguage() + "," + l.getCountry(); } public Locale stringToLocale(String s) { StringTokenizer tempStringTokenizer = new StringTokenizer(s,","); if(tempStringTokenizer.hasMoreTokens()) String l = tempStringTokenizer.nextElement(); if(tempStringTokenizer.hasMoreTokens()) String c = tempStringTokenizer.nextElement(); return new Locale(l,c); }
Locale getBestLocale() { if (SUPPORTED_LOCALES.contains(Locale.getDefault())) { return Locale.getDefault(); } Locale onlyLanguage = new Locale(Locale.getDefault().getLanguage()); if (SUPPORTED_LOCALES.contains(onlyLanguage)) { LOG.trace("Using supported locale closest to default {}", onlyLanguage); return onlyLanguage; } LOG.trace("Using fallback locale"); return Locale.UK; }
@ManagedBean @SessionScoped public class LocaleManager { private Locale locale; @PostConstruct public void init() { locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(); } public Locale getLocale() { return locale; } public String getLanguage() { return locale.getLanguage(); } public void setLanguage(String language) { locale = new Locale(language); FacesContext.getCurrentInstance().getViewRoot().setLocale(locale); } }
@Override public Language toLanguage(Locale locale) { try { Language lang = getLanguagesMap().get(locale.getLanguage()); return lang; } catch (Exception e) { LOGGER.error("Cannot convert locale " + locale.getLanguage() + " to language"); } return new Language(Constants.DEFAULT_LANGUAGE); }
/** * Get the feature processor manager associated with the given locale, if any. * * @param locale * locale * @return the feature processor manager, or null if there is no locale-specific feature processor manager. */ public static FeatureProcessorManager getFeatureProcessorManager(Locale locale) { FeatureProcessorManager m = managersByLocale.get(locale); if (m != null) return m; // Maybe locale is language_COUNTRY, so look up by language also: Locale lang = new Locale(locale.getLanguage()); return managersByLocale.get(lang); }
public static String[] getDays(Locale locale, String pattern) { if (Validator.isNull(pattern)) { pattern = "EEEE"; } StringBundler sb = new StringBundler(6); sb.append("days_"); sb.append(pattern); sb.append("_"); sb.append(locale.getLanguage()); sb.append("_"); sb.append(locale.getCountry()); String key = sb.toString(); String[] days = _calendarPool.get(key); if (days == null) { days = new String[7]; Format dayFormat = FastDateFormatFactoryUtil.getSimpleDateFormat( pattern, locale); Calendar cal = CalendarFactoryUtil.getCalendar(); cal.set(Calendar.DATE, 1); for (int i = 0; i < 7; i++) { cal.set(Calendar.DAY_OF_WEEK, i + 1); days[i] = dayFormat.format(cal.getTime()); } _calendarPool.put(key, days); } return days; }
if (locale != null) { list.add(locale); if (!locale.getVariant().isEmpty()) { list.add(new Locale(locale.getLanguage(), locale.getCountry())); if (!locale.getCountry().isEmpty()) { list.add(new Locale(locale.getLanguage(), StringUtils.EMPTY));
private static List<String> getPotentialMessageFiles() { // Load the message collections Locale locale = Locale.getDefault(); String language = locale.getLanguage(); String country = locale.getCountry(); List<String> potential = new ArrayList<>(3); if (country != null) { potential.add("messages_" + language + "_" + country + ".xml"); } potential.add("messages_" + language + ".xml"); potential.add("messages.xml"); return potential; }
private static Locale forEN() { if ("en".equalsIgnoreCase(Locale.getDefault().getLanguage())) { return Locale.getDefault(); } Locale[] locales = Locale.getAvailableLocales(); for (int i = 0; i != locales.length; i++) { if ("en".equalsIgnoreCase(locales[i].getLanguage())) { return locales[i]; } } return Locale.getDefault(); }
String language = locale.getLanguage(); String country = locale.getCountry(); String variant = locale.getVariant(); StringBuilder temp = new StringBuilder(basename);
public static Tuple2<String,Locale> toLocaleModel(String baseName,Locale locale) { if (StringKit.isBlank(baseName)) { return new Tuple2<>("i18n_"+locale.getLanguage()+"_"+locale.getCountry(),locale); }else { String[] baseNames = pattern.split(baseName); if (baseNames != null && baseNames.length == 3) { return new Tuple2<>(baseName,new Locale(baseNames[1],baseNames[2])); } throw new IllegalArgumentException("baseName illegal,name format is :i18n_{language}_{country}.properties," + "for example:i18n_zh_CN.properties"); } }
@Nullable private Locale findSupportedLocale(HttpServletRequest request, List<Locale> supportedLocales) { Enumeration<Locale> requestLocales = request.getLocales(); Locale languageMatch = null; while (requestLocales.hasMoreElements()) { Locale locale = requestLocales.nextElement(); if (supportedLocales.contains(locale)) { if (languageMatch == null || languageMatch.getLanguage().equals(locale.getLanguage())) { // Full match: language + country, possibly narrowed from earlier language-only match return locale; } } else if (languageMatch == null) { // Let's try to find a language-only match as a fallback for (Locale candidate : supportedLocales) { if (!StringUtils.hasLength(candidate.getCountry()) && candidate.getLanguage().equals(locale.getLanguage())) { languageMatch = candidate; break; } } } } return languageMatch; }