/** * @bug 4108126 */ public void TestGetLocale() { // try to find TestResource_fr_CH. Should get fr_CH as its locale ResourceBundle test = ResourceBundle.getBundle("TestResource", new Locale("fr", "CH", "")); Locale locale = test.getLocale(); if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH"))) errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale); // try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale test = ResourceBundle.getBundle("TestResource", new Locale("fr", "BE", "")); locale = test.getLocale(); if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals(""))) errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale); // try to find TestResource_iw_IL, which doesn't exist. Should get root locale // as its locale test = ResourceBundle.getBundle("TestResource", new Locale("iw", "IL", ""), Control.getNoFallbackControl(Control.FORMAT_DEFAULT)); locale = test.getLocale(); if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals(""))) errln("Actual locale for TestResource_iw_IL should have been the root locale, got " + locale); }
public String getTranslation(String code) { if (bundle == null) { Locale l = this.locale; if (l == null) { if (UI.getCurrent() != null) { l = UI.getCurrent().getLocale(); } else { l = Locale.getDefault(); } } if (useDefaultLocaleFallback) { bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_BASENAME, l); } else { bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_BASENAME, l, Control.getNoFallbackControl(Control.FORMAT_PROPERTIES)); } } return bundle.getString(code); }
public String getLocalizedProperty(String property, Locale locale) { Control control = Control.getControl(Control.FORMAT_PROPERTIES); String value = null; // we're not looking at the fallback locale to be consistent with JRResourcesUtil.loadResourceBundle List<Locale> locales = control.getCandidateLocales(property, locale); for (Locale candidate : locales) { String candidateString = candidate.toString(); String candidateProperty = candidateString.isEmpty() ? property : (property + "_" + candidateString); String candidateValue = getProperty(candidateProperty); if (candidateValue != null)// test for empty? { value = candidateValue; break; } } return value; }
@Test public void test_Remote_French() { TestBundle testBundle = TEST_BUNDLES[0]; String bundleId = testBundle.id; CloudResourceBundleControl ctrl = CloudResourceBundleControl.getInstance( account, Control.TTL_DONT_CACHE, null, null, null, null); ResourceBundle bundle = ResourceBundle.getBundle(bundleId, new Locale("fr"), ctrl); for (Entry<String, String> entry : testBundle.strings.entrySet()) { String key = entry.getKey(); try { String frVal = bundle.getString(key); assertNotNull("Test1 fr " + key + " value", frVal); } catch (MissingResourceException e) { fail("Test1 fr should contain " + key); } } }
private ValueMap<? extends LanguageTermDefinition> readLangs(Locale locale) { ArrayList<LangTermValue> langs = new ArrayList<LangTermValue>(); // to read properties file in UTF-8 use PropertyResourceBundle(Reader) Control control = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES); ResourceBundle rb = ResourceBundle.getBundle(BundleName.LANGUAGES_ISO639_2.toString(), locale, control); for (String key : rb.keySet()) { LangTermValue lt = new LangTermValue(); lt.setAuthority("iso639-2b"); lt.setType(CodeOrText.CODE); lt.setValue(key); lt.setTitle(rb.getString(key)); langs.add(lt); } Collections.sort(langs, new LangComparator(locale)); return new ValueMap<LangTermValue>(BundleName.LANGUAGES_ISO639_2.getValueMapId(), langs); }
/** * * @param locale * the Locale * @return The ResourceBundle for the application and the locale. The default * name of the properties file is the same as the application class * name. * @see MultiResourceBundle MultiResourceBundle to combine several * ResourceBundle */ public ResourceBundle getResourceBundle(Locale locale) { List<ResourceBundle> resourceBundles = new ArrayList<>(); Class<?> applicationClass = getClass(); do { try { String resourceBundleName = applicationClass.getName(); resourceBundles.add(ResourceBundle.getBundle(resourceBundleName, locale, Control.getNoFallbackControl(Control.FORMAT_PROPERTIES))); } catch (MissingResourceException x) { if (applicationClass == getInstance().getClass()) { Logger logger = Logger.getLogger(Application.class.getName()); logger.warning("Missing the default ResourceBundle for " + this.getClass().getName()); logger.fine("The default ResourceBundle has the same name as the Application that is launched."); logger.fine("See the MjExampleApplication.java and MjExampleApplication.properties"); } } applicationClass = applicationClass.getSuperclass(); } while (applicationClass != Application.class); return new MultiResourceBundle(resourceBundles); }
/** * {@inheritDoc} * * <p>This method will throw {@link ControlFactoryException} if the passed * {@code args} are not empty or if the charset specified at * {@code mapping.encoding()} is not valid.</p> * * @throws ControlFactoryException If {@code args.length != 0} or the charset * provided at {@code mapping.encoding()} is not supported. */ @Override public Control create(ResourceMapping mapping, String[] args) { final Charset charset; if (args.length != 0) { throw new ControlFactoryException( "This class has no additional parameters"); } else { try { charset = Charset.forName(mapping.encoding()); } catch (UnsupportedCharsetException e) { throw new ControlFactoryException(String.format( "Unsupported charset: %s", args[0]), e); } } return new CharsetBundleControl(charset); }
public LocalizerFile(final String localizerBase, final ClassLoader classLoader, final Control control) { super(); if (log.isTraceEnabled()) log.trace(HelperLog.constructor(localizerBase, classLoader)); if (null == localizerBase) { throw new RuntimeExceptionIsNull("localizerBase"); //$NON-NLS-1$ } if (null == classLoader) { throw new RuntimeExceptionIsNull("classLoader"); //$NON-NLS-1$ } this.localizerBase = localizerBase; this.classLoader = classLoader; this.control = control; setupBundle(getLocale()); }
@Override public NodeRef getLocalizedSibling(NodeRef nodeRef) { Locale userLocale = I18NUtil.getLocale(); String name = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME); NodeRef parentNodeRef = nodeService.getPrimaryParent(nodeRef).getParentRef(); // Work out the base name we are working with Pair<String, String> split = getExtension(name, false); String base = split.getFirst(); String ext = split.getSecond(); NodeRef resultNodeRef = nodeRef; // Search for siblings with the same name Control resourceHelper = Control.getControl(Control.FORMAT_DEFAULT); List<Locale> candidateLocales = resourceHelper.getCandidateLocales(base, userLocale); for (Locale candidateLocale : candidateLocales) { String filename = resourceHelper.toBundleName(base, candidateLocale) + "." + ext; // Attempt to find the file NodeRef foundNodeRef = searchSimple(parentNodeRef, filename); if (foundNodeRef != null) // TODO: Check for read permissions { resultNodeRef = foundNodeRef; break; } } // Done return resultNodeRef; }
/** * Returns a list of candidate locales for service look up. * @param locale the input locale * @return the list of candidate locales for the given locale */ static List<Locale> getLookupLocales(Locale locale) { // Note: We currently use the default implementation of // ResourceBundle.Control.getCandidateLocales. The result // returned by getCandidateLocales are already normalized // (no extensions) for service look up. List<Locale> lookupLocales = Control.getNoFallbackControl(Control.FORMAT_DEFAULT) .getCandidateLocales("", locale); return lookupLocales; }
/** * Returns a Map of the known resources for the given locale. */ private Map<String, Object> getResourceCache(Locale l) { Map<String, Object> values = resourceCache.get(l); if (values == null) { values = new TextAndMnemonicHashMap(); for (int i=resourceBundles.size()-1; i >= 0; i--) { String bundleName = resourceBundles.get(i); try { Control c = CoreResourceBundleControl.getRBControlInstance(bundleName); ResourceBundle b; if (c != null) { b = ResourceBundle.getBundle(bundleName, l, c); } else { b = ResourceBundle.getBundle(bundleName, l); } Enumeration keys = b.getKeys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); if (values.get(key) == null) { Object value = b.getObject(key); values.put(key, value); } } } catch( MissingResourceException mre ) { // Keep looking } } resourceCache.put(l, values); } return values; }
public void TestListResourceBundle() { // load up the resource and check to make sure we got the right class // (we don't define be_BY or be, so we fall back on the root default) ResourceBundle bundle = ResourceBundle.getBundle("TestResource", new Locale("be", "BY"), Control.getNoFallbackControl(Control.FORMAT_DEFAULT)); if (!bundle.getClass().getName().equals("TestResource")) errln("Expected TestResource, got " + bundle.getClass().getName()); doListResourceBundleTest(bundle); }
private static void setupBaseLocales(String localeList) { Arrays.stream(localeList.split(",")) .map(Locale::forLanguageTag) .map(l -> Control.getControl(Control.FORMAT_DEFAULT) .getCandidateLocales("", l)) .forEach(BASE_LOCALES::addAll); }
@Override protected String toBundleName(String baseName, Locale locale) { // The resource bundle for Locale.JAPAN is loccated at jdk.test.resources // in module "asiabundles". String name = locale.equals(Locale.JAPAN) ? baseName : addRegion(baseName); return Control.getControl(Control.FORMAT_DEFAULT).toBundleName(name, locale); }
@Test public void test_Remote_English() { TestBundle testBundle = TEST_BUNDLES[0]; String bundleId = testBundle.id; CloudResourceBundleControl ctrl = CloudResourceBundleControl.getInstance( account, Control.TTL_DONT_CACHE, null, null, null, null); ResourceBundle bundle = ResourceBundle.getBundle(bundleId, new Locale("en"), ctrl); for (Entry<String, String> entry : testBundle.strings.entrySet()) { String key = entry.getKey(); String resValue = bundle.getString(key); assertEquals("resource value for " + key, entry.getValue(), resValue); } }
@Test public void test_LocalOnly_KeyOnlyInLocalEN() { TestBundle testBundle = TEST_BUNDLES[0]; String bundleId = testBundle.id; CloudResourceBundleControl ctrl = CloudResourceBundleControl.getInstance( account, Control.TTL_DONT_CACHE, null, null, null, LookupMode.LOCAL_ONLY); ResourceBundle bundle = ResourceBundle.getBundle(bundleId, new Locale("en"), ctrl); try { String localOnly = bundle.getString("local_only"); assertNotNull("Local Test1 en key local_only value", localOnly); } catch (MissingResourceException e) { fail("Local Test1 en should contain key local_only"); } }
@Test public void test_RemoteOnly_KeyOnlyInLocalEN() { TestBundle testBundle = TEST_BUNDLES[0]; String bundleId = testBundle.id; CloudResourceBundleControl ctrl = CloudResourceBundleControl.getInstance( account, Control.TTL_DONT_CACHE, null, null, null, LookupMode.REMOTE_ONLY); ResourceBundle bundle = ResourceBundle.getBundle(bundleId, new Locale("en"), ctrl); expectedException.expect(MissingResourceException.class); String s = bundle.getString("local_only"); System.out.println(s); }
@Test public void test_LocalThenRemote_KeyOnlyInLocalJA() { TestBundle testBundle = TEST_BUNDLES[0]; String bundleId = testBundle.id; CloudResourceBundleControl ctrl = CloudResourceBundleControl.getInstance( account, Control.TTL_DONT_CACHE, null, null, null, LookupMode.LOCAL_THEN_REMOTE); ResourceBundle bundle = ResourceBundle.getBundle(bundleId, new Locale("ja"), ctrl); try { String localOnly = bundle.getString("local_only"); assertNotNull("Local Test1 ja key local_only value", localOnly); } catch (MissingResourceException e) { fail("Local Test1 ja should contain key local_only"); } }