/** * @see org.openmrs.api.GlobalPropertyListener#globalPropertyChanged(org.openmrs.GlobalProperty) */ @Override public void globalPropertyChanged(GlobalProperty newValue) { allowedLocales = new LinkedHashSet<>(); for (String allowedLocaleString : newValue.getPropertyValue().split(",")) { Locale allowedLocale = LocaleUtility.fromSpecification(allowedLocaleString.trim()); if (allowedLocale != null) { allowedLocales.add(allowedLocale); } } }
/** * Utility method for deriving a locale from a filename. * * @param filename the name to parse * @return Locale derived from the given string */ private Locale parseLocaleFrom(String filename, String basename) { Locale result; String tempFilename = filename; if (filename.startsWith(basename)) { tempFilename = filename.substring(basename.length()); } String localespec = tempFilename.substring(0, tempFilename.indexOf('.')); if ("".equals(localespec)) { result = Locale.ENGLISH; } else { localespec = localespec.substring(1); result = LocaleUtility.fromSpecification(localespec); } return result; }
/** * Utility method for deriving a locale from a filename, presumed to have an embedded locale * specification near the end. For instance messages_it.properties if the filename is * messages.properties, the Locale is presumed to be the default set for Java * * @param filename the name to parse * @return Locale derived from the given string */ private Locale parseLocaleFrom(String filename) { Locale parsedLocale; // trim off leading basename filename = filename.substring("messages".length()); // trim off extension String localespec = filename.substring(0, filename.indexOf('.')); if ("".equals(localespec)) { parsedLocale = Locale.getDefault(); } else { localespec = localespec.substring(1); // trim off leading '_' parsedLocale = LocaleUtility.fromSpecification(localespec); } return parsedLocale; }
for (String proficientLocaleSpec : proficientLocalesArray) { if (proficientLocaleSpec.length() > 0) { Locale proficientLocale = LocaleUtility.fromSpecification(proficientLocaleSpec); if (!proficientLocales.contains(proficientLocale)) { proficientLocales.add(proficientLocale); if (StringUtils.isNotEmpty(proficientLocale.getCountry())) { Locale languageOnlyLocale = LocaleUtility.fromSpecification(proficientLocale.getLanguage()); if (!proficientLocales.contains(languageOnlyLocale)) { proficientLocales.add(LocaleUtility.fromSpecification(proficientLocale.getLanguage()));
Locale locale = LocaleUtility.fromSpecification(localeString); if (LocaleUtility.isValid(locale)) { return locale;
defaultLocaleCache = fromSpecification(locale); defaultLocaleCache = fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE); return fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
locales.add(fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE));
String defaultLocaleStr = rsDefaultLocale.getString("property_value"); if (!StringUtils.isBlank(defaultLocaleStr) && defaultLocaleStr.length() > 1) { Locale defaultLocaleGP = LocaleUtility.fromSpecification(defaultLocaleStr); if (defaultLocaleGP != null) { defaultLocale = defaultLocaleGP; for (String localeStr : localesArray) { if (localeStr.trim().length() > 1) { allowedLocales.add(LocaleUtility.fromSpecification(localeStr.trim())); } else { updateWarnings.add("'" + localeStr
/** * To be able to load resource bundles outside the class path we need to override this method * * @see org.apache.velocity.tools.generic.ResourceTool#getBundle(java.lang.String, * java.lang.Object) */ @Override protected ResourceBundle getBundle(String baseName, Object loc) { Locale locale = (loc == null) ? getLocale() : LocaleUtility.fromSpecification(String.valueOf(loc)); if (baseName == null || locale == null) { return null; } //This messages_XX.properties file doesn't exist, default to messages.properties ResourceBundle rb = CustomResourceLoader.getInstance(null).getResourceBundle(locale); if (rb == null) { rb = getDefaultResourceBundle(); } return rb; }
conceptName.setLocale(!StringUtils.isBlank(localeString) ? LocaleUtility.fromSpecification(localeString) : null); conceptName.setLocalePreferred(rs.getBoolean("locale_preferred"));
/** * @see LocaleUtility#fromSpecification(String) */ @Test public void fromSpecification_shouldGetLocaleFromTwoCharacterLanguageCode() { Assert.assertEquals(Locale.ENGLISH, LocaleUtility.fromSpecification("en")); }
/** * @see LocaleUtility#fromSpecification(String) */ @Test public void fromSpecification_shouldGetLocaleFromLanguageCodeAndCountryCode() { Assert.assertEquals(Locale.UK, LocaleUtility.fromSpecification("en_GB")); }
/** * Become a different user. (You should only be able to do this as a superuser.) * * @param systemId * @throws ContextAuthenticationException * @should change locale when become another user */ public static void becomeUser(String systemId) throws ContextAuthenticationException { log.info("systemId: {}", systemId); User user = getUserContext().becomeUser(systemId); // if assuming identity procedure finished successfully, we should change context locale parameter Locale locale = null; if (user.getUserProperties().containsKey(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE)) { String localeString = user.getUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE); locale = LocaleUtility.fromSpecification(localeString); } // when locale parameter is not valid or does not exist if (locale == null) { locale = LocaleUtility.getDefaultLocale(); } Context.setLocale(locale); }
/** * @see LocaleUtility#fromSpecification(String) */ @Test public void fromSpecification_shouldGetLocaleFromLanguageCodeCountryCodeAndVariant() { Locale locale = LocaleUtility.fromSpecification("en_US_Traditional_WIN"); Assert.assertEquals(Locale.US.getLanguage(), locale.getLanguage()); Assert.assertEquals(Locale.US.getCountry(), locale.getCountry()); Assert.assertEquals("Traditional,WIN", locale.getDisplayVariant()); }
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldAlwaysHaveDefaultLocaleDefaultValueIncludedInTheReturnedCollection() { Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Assert.assertEquals(true, localesInOrder.contains(LocaleUtility .fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE))); }
localeList.add(LocaleUtility.fromSpecification(localeString.trim())); if (!localeList.contains(LocaleUtility.fromSpecification(gp.getPropertyValue().trim()))) { String value = gp.getPropertyValue(); gp.setPropertyValue(LocaleUtility.getDefaultLocale().toString());
/** * @param specification * @return a java Locale object for the given String specification */ public Locale locale(String specification) { return LocaleUtility.fromSpecification(specification); }
Locale systemLocale = LocaleUtility.fromSpecification(locale);
/** * Gets the translation for a specific code and locale * * @param localeStr the name of the locale * @param key the code * @return the translation associated with that code and locale */ public String translate(String localeStr, String key) { Map<String, String> translations = getTranslations(localeStr); String ret = translations.get(key); if (StringUtils.isEmpty(ret)) { ret = Context.getMessageSourceService().getMessage(key, null, LocaleUtility.fromSpecification(localeStr)); } return (ret == null ? key : ret); }
Assert.assertEquals(LocaleUtility.fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE), LocaleUtility.getDefaultLocale());