/** * @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); } } }
locales.add(getDefaultLocale()); if (localesAllowedListCache == null) { localesAllowedListCache = Context.getAdministrationService().getAllowedLocales(); locales.add(fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE));
/** * @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))); }
Locale locale = LocaleUtility.fromSpecification(localeString); if (LocaleUtility.isValid(locale)) { return locale; } else {
locale = LocaleUtility.getDefaultLocale(); if (!exact && LocaleUtility.areCompatible(availableLocale, desiredLocale)) { foundDescription = availableDescription; if (availableLocale.equals(LocaleUtility.getDefaultLocale())) { defaultDescription = availableDescription;
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldHaveDefaultLocaleAsTheFirstElementIfUserHasNoPreferredLocale() { // make sure the user doesn't have a locale Context.setLocale(null); Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Assert.assertEquals(LocaleUtility.getDefaultLocale(), localesInOrder.iterator().next()); }
/** * @return current locale for this context */ public Locale getLocale() { if (locale == null) { // don't cache default locale - allows recognition of changed default at login page return LocaleUtility.getDefaultLocale(); } return locale; }
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldAlwaysHaveEnglishIncludedInTheReturnedCollection() { Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Assert.assertEquals(true, localesInOrder.contains(Locale.ENGLISH)); }
compatibleNames = new ArrayList<>(); for (ConceptName possibleName : getNames()) { if (LocaleUtility.areCompatible(possibleName.getLocale(), desiredLocale)) { compatibleNames.add(possibleName);
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldHaveDefaultLocaleAsTheSecondElementIfUserHasAPreferredLocale() { Locale lu_UG = new Locale("lu", "UG"); Context.setLocale(lu_UG); Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Iterator<Locale> it = localesInOrder.iterator(); Assert.assertEquals(lu_UG, it.next()); Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next()); }
/** * Convenience method. Passes through to {@link UserContext#getLocale()} * * @should not fail if session hasn't been opened */ public static Locale getLocale() { // if a session hasn't been opened, just fetch the default if (!isSessionOpen()) { return LocaleUtility.getDefaultLocale(); } return getUserContext().getLocale(); }
for (Locale currentLocale : LocaleUtility.getLocalesInOrder()) { ConceptName preferredName = getPreferredName(currentLocale); if (preferredName != null) {
/** * Two locales, where one has no country specified, but where the language does not match, * should not be considered compatible. * * @see LocaleUtility#areCompatible(Locale,Locale) */ @Test public void areCompatible_shouldConfirmDifferentLanguageMissingCountryAsCompatible() { Locale lhs = Locale.US; Locale rhs = Locale.FRENCH; assertFalse(LocaleUtility.areCompatible(lhs, rhs)); }
/** * 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; }
/** * 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#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldReturnASetOfLocalesWithAPredictableOrder() { GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "lu, sw_KE, en_US, en_GB", "Test Allowed list of locales"); Context.getAdministrationService().saveGlobalProperty(gp); Locale lu_UG = new Locale("lu", "UG"); Context.setLocale(lu_UG); Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Iterator<Locale> it = localesInOrder.iterator(); Assert.assertEquals(new Locale("lu", "UG"), it.next()); Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next()); Assert.assertEquals(new Locale("lu"), it.next()); Assert.assertEquals(new Locale("sw", "KE"), it.next()); Assert.assertEquals(new Locale("en", "US"), it.next()); Assert.assertEquals(new Locale("en"), it.next()); }
/** * @see org.openmrs.api.AdministrationService#getAllowedLocales() */ @Override @Transactional(readOnly = true) public List<Locale> getAllowedLocales() { // lazy-load the global locale list and initialize with current global property value if (globalLocaleList == null) { globalLocaleList = new GlobalLocaleList(); Context.getAdministrationService().addGlobalPropertyListener(globalLocaleList); } Set<Locale> allowedLocales = globalLocaleList.getAllowedLocales(); // update the GlobalLocaleList.allowedLocales by faking a global property change if (allowedLocales == null) { // use a default language of "english" if they have cleared this GP for some reason String currentPropertyValue = Context.getAdministrationService().getGlobalProperty( OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, LocaleUtility.getDefaultLocale().toString()); GlobalProperty allowedLocalesProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, currentPropertyValue); globalLocaleList.globalPropertyChanged(allowedLocalesProperty); allowedLocales = globalLocaleList.getAllowedLocales(); } // allowedLocales is guaranteed to not be null at this point return new ArrayList<>(allowedLocales); }
/** * This test fetches all concepts in the xml test dataset and ensures that every locale for a * concept name is among those listed in the global property 'locale.allowed.list' and default * locale. NOTE that it doesn't test a particular API method directly. */ @Test public void saveConcept_shouldNotAcceptALocaleThatIsNeitherAmongTheLocaleAllowedListNorADefaultLocale() { List<Concept> concepts = Context.getConceptService().getAllConcepts(); Set<Locale> allowedLocales = LocaleUtility.getLocalesInOrder(); for (Concept concept : concepts) { if (!CollectionUtils.isEmpty(concept.getNames())) { for (ConceptName cn : concept.getNames()) { Assert.assertTrue("The locale '" + cn.getLocale() + "' of conceptName with id: " + cn.getConceptNameId() + " is not among those listed in the global property 'locale.allowed.list'", allowedLocales .contains(cn.getLocale())); } } } }
/** * @see LocaleUtility#areCompatible(Locale,Locale) */ @Test public void areCompatible_shouldConfirmMatchingCountryAsCompatible() { Locale lhs = Locale.US; Locale rhs = Locale.US; assertTrue(LocaleUtility.areCompatible(lhs, rhs)); }
/** * 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; }