List of usage examples for java.util ResourceBundle getLocale
public Locale getLocale()
From source file:org.jahia.utils.i18n.Messages.java
/** * Looks up the resource bundle key in the specified bundle. Additionally placeholders are replaced with the provided arguments. * /*from w w w .j av a2s .c o m*/ * @param bundle * the bundle to perform the lookup in * @param key * the key to perform lookup for * @param args * the arguments to replace placeholders with * @return the label for the requested key */ public static String getWithArgs(ResourceBundle bundle, String key, Object... arguments) throws MissingResourceException { return format(bundle.getString(key), bundle.getLocale(), arguments); }
From source file:org.jahia.utils.i18n.Messages.java
/** * Looks up the resource bundle key in the specified bundle. Additionally placeholders are replaced with the provided arguments. * //from www. jav a 2s.c om * @param bundle * the bundle to perform the lookup in * @param key * the key to perform lookup for * @param defaultValue * the default value to return if the lookup has not found anything * @param args * the arguments to replace placeholders with * @return the label for the requested key */ public static String getWithArgs(ResourceBundle bundle, String key, String defaultValue, Object... arguments) throws MissingResourceException { return format(get(bundle, key, defaultValue), bundle.getLocale(), arguments); }
From source file:org.jahia.utils.LanguageCodeConverters.java
public static List<Locale> getAvailableBundleLocales(String resourceBundleName, Locale defaultLocale) { final List<Locale> availableBundleLocales = new LinkedList<Locale>(); // first let's add the default locale if it exists. if (defaultLocale != null && ResourceBundle.getBundle(resourceBundleName, defaultLocale) != null) { availableBundleLocales.add(defaultLocale); }/* w w w. j a va2 s . c o m*/ for (Locale locale : getAvailableLocales()) { if (!StringUtils.isEmpty(locale.getDisplayName())) { // Avoid "default/system" empty locale ResourceBundle res = ResourceBundle.getBundle(resourceBundleName, locale); if (res != null && res.getLocale().equals(locale) && (defaultLocale == null || !locale.equals(defaultLocale))) { availableBundleLocales.add(locale); } } } return availableBundleLocales; }
From source file:org.jboss.test.bpel.ws.consumption.partner.resource.ResourceDictionaryFactory.java
protected ResourceBundle getBundle(Locale sourceLocale, Locale targetLocale) { String sourceLanguage = sourceLocale.getLanguage(); log.debug("loading bundle: sourceLanguage=" + sourceLanguage + ", targetLocale=" + targetLocale); ResourceBundle bundle; try {// w ww. j a v a 2 s . c om bundle = ResourceBundle.getBundle(getBaseName(sourceLanguage), targetLocale); String bundleLanguage = bundle.getLocale().getLanguage(); if (bundleLanguage.equals(targetLocale.getLanguage())) { log.debug("loaded bundle: bundleLanguage=" + bundleLanguage); } else { bundle = null; log.debug("loaded bundle, but it does not correspond to the target locale: " + "bundleLanguage=" + bundleLanguage); } } catch (MissingResourceException e) { bundle = null; log.debug("bundle not found", e); } return bundle; }
From source file:org.openspaces.core.internal.commons.math.MathException.java
/** * Translate a string to a given locale. * * @param s string to translate/*from w ww .j av a2 s . c o m*/ * @param locale locale into which to translate the string * @return translated string or original string for unsupported locales or unknown strings */ private static String translate(String s, Locale locale) { try { ResourceBundle bundle = ResourceBundle.getBundle("org.apache.commons.math.MessagesResources", locale); if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) { // the value of the resource is the translated string return bundle.getString(s); } } catch (MissingResourceException mre) { // do nothing here } // the locale is not supported or the resource is unknown // don't translate and fall back to using the string as is return s; }
From source file:org.opentaps.notes.rest.locale.Messages.java
/** * Returns class initialized with resource bundle of the requested locale.<br> * First we check URL for embed locale identifier that equals to Locale valid string representation.<br> * Example of encoding locale with URL is: <code>/notes/note/fr_fr/noteID</code><br> * If not found we have to check <code>Accept-Language</code> header and select first available language * looking through the list in the natural order. *///from w w w.j a v a 2 s. c o m public static Messages getInstance(Request request) { List<Locale> localeList = null; // get encoded locale String lang = (String) request.getAttributes().get("lang"); if (!GenericValidator.isBlankOrNull(lang)) { try { Locale locale = new Locale(lang); localeList = Arrays.asList(locale); } catch (NullPointerException e) { //do nothing } } // get languages from Accept-Language if (localeList == null || localeList.size() == 0) { ClientInfo clientInfo = request.getClientInfo(); List<Preference<Language>> preferences = clientInfo.getAcceptedLanguages(); localeList = new ArrayList<Locale>(preferences.size()); for (Preference<Language> pref : preferences) { localeList.add(new Locale(pref.getMetadata().toString())); } } // looking for the first available if (localeList != null && localeList.size() > 0) { ResourceBundle.clearCache(); for (Locale locale : localeList) { try { String langCode = locale.toString().replaceAll("-", "_"); ResourceBundle rb = ResourceBundle.getBundle(BUNDLE_NAME, locale); Locale rbLocale = rb.getLocale(); if (rbLocale == null || GenericValidator.isBlankOrNull(rbLocale.toString())) { rbLocale = Locale.getDefault(); } if (langCode.equalsIgnoreCase(rbLocale.toString())) { return new Messages(rb); } else { continue; } } catch (MissingResourceException e) { Log.logWarning(e.getLocalizedMessage()); continue; } } } return new Messages(ResourceBundle.getBundle(BUNDLE_NAME)); }
From source file:org.orekit.errors.OrekitMessages.java
/** {@inheritDoc} */ public String getLocalizedString(final Locale locale) { try {//from w ww . j ava 2 s . com final ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BASE_NAME, locale, new UTF8Control()); if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) { final String translated = bundle.getString(name()); if ((translated != null) && (translated.length() > 0) && (!translated.toLowerCase().contains("missing translation"))) { // the value of the resource is the translated format return translated; } } } catch (MissingResourceException mre) { // do nothing here } // either the locale is not supported or the resource is not translated or // it is unknown: don't translate and fall back to using the source format return sourceFormat; }
From source file:org.osk.errors.OskMessages.java
/** {@inheritDoc} */ public String getLocalizedString(final Locale locale) { try {// w w w. ja va2s . c o m final ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BASE_NAME, locale); if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) { final String translated = bundle.getString(name()); if ((translated != null) && (translated.length() > 0) && (!translated.toLowerCase().contains("missing translation"))) { // the value of the resource is the translated format return translated; } } } catch (MissingResourceException mre) { // do nothing here } // either the locale is not supported or the resource is not translated or // it is unknown: don't translate and fall back to using the source format return sourceFormat; }
From source file:org.radeox.util.i18n.ResourceManager.java
/** * Find a resource bundle by looking up using the locales. This is done by loading * either the specified locale based resource bundle and, if that fails, by * looping through the fallback locales to locate a usable bundle. *//*ww w .j av a 2 s. co m*/ private ResourceBundle findBundle(String baseName) { ResourceBundle resourceBundle = null; // first try to load the resource bundle with the specified locale ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (null != locale) { try { resourceBundle = ResourceBundle.getBundle(baseName, locale, cl); } catch (Exception e) { log.fatal("unable to load a default bundle: " + baseName + "_" + locale); } // check that the requested main locale matches the resource bundle's, // since we get the system fallback locale if no match is found if (!resourceBundle.getLocale().equals(locale)) { resourceBundle = null; } } // loop through the fall back locales until a bundle is found if (null == resourceBundle) { if (null != fallback) { while (fallback.hasMoreElements()) { Locale testLocale = (Locale) fallback.nextElement(); log.debug("looking up locale " + testLocale); ResourceBundle testBundle = ResourceBundle.getBundle(baseName, testLocale, cl); String language = testBundle.getLocale().getLanguage(); String country = testBundle.getLocale().getCountry(); if (testBundle.getLocale().equals(testLocale)) { resourceBundle = testBundle; log.debug("found bundle for locale " + baseName + "_" + testBundle.getLocale()); break; } else if (testLocale.getLanguage().equals(language)) { if (testLocale.getCountry().equals(country)) { // language and country match which is good, keep looking for variant too resourceBundle = testBundle; log.debug("potential bundle: " + baseName + "_" + testBundle.getLocale()); continue; } else { // only accept this if there is no better previous lookup if (null == resourceBundle) { resourceBundle = testBundle; log.debug("potential bundle: " + baseName + "_" + testBundle.getLocale()); } continue; } } } } // make sure the resource bundle is loaded (should not happen) if (null == resourceBundle) { resourceBundle = ResourceBundle.getBundle(baseName); if (null != resourceBundle) { log.debug("system locale bundle taken: " + baseName + "_" + resourceBundle.getLocale()); } } } return resourceBundle; }
From source file:org.sakaiproject.jsf.util.LocaleUtil.java
public static String getLocalizedString(FacesContext context, String bundleName, String key) { String localized = null;/*from w ww . j a va 2s. c om*/ Locale locale = getLocale(context); ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale); if (log.isDebugEnabled()) log.debug("getLocalizedString; locale=" + locale.getDisplayName() + ", bundleName=" + bundleName + ", rb=" + rb.getLocale() + ", rb getCountry()=" + rb.getLocale().getCountry()); localized = rb.getString(key); return localized; }