/** * @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; }
locale = LocaleUtility.getDefaultLocale(); foundDescription = availableDescription; if (availableLocale.equals(LocaleUtility.getDefaultLocale())) { defaultDescription = availableDescription;
locales.add(getDefaultLocale()); if (localesAllowedListCache == null) { localesAllowedListCache = Context.getAdministrationService().getAllowedLocales();
/** * 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(); }
/** * 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_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()); }
localeList.add(LocaleUtility.fromSpecification(localeString.trim())); if (!localeList.contains(LocaleUtility.getDefaultLocale())) { gp.setPropertyValue(StringUtils.join(getAllowedLocales(), ", ")); throw new APIException(Context.getMessageSourceService().getMessage( "general.locale.localeListNotIncludingDefaultLocale", new Object[] { LocaleUtility.getDefaultLocale() }, null)); gp.setPropertyValue(LocaleUtility.getDefaultLocale().toString()); throw new APIException((Context.getMessageSourceService().getMessage( "general.locale.defaultNotInAllowedLocalesList", new Object[] { value }, null)));
/** * @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()); }
/** * @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); }
@Test public void saveGlobalProperty_shouldFailIfGivenAllowedLocaleListDoesNotContainDefaultLocale() { String localeList = "fr,es"; assertThat("localeList contains default locale but should not for this test case", localeList, not(containsString(LocaleUtility.getDefaultLocale().toString()))); expectedException.expect(APIException.class); expectedException.expectMessage("can not be removed from allowed locales list because it is the default locale"); adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, localeList)); }
/** * @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 Context#getLocale() */ @Test public void getLocale_shouldNotFailIfSessionHasntBeenOpened() { Context.closeSession(); Assert.assertEquals(LocaleUtility.getDefaultLocale(), Context.getLocale()); }
/** * @see LocaleUtility#getDefaultLocale() */ @Test public void getDefaultLocale_shouldNotFailWithEmptyGlobalPropertyValue() { Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "")); // check for nonnullness Assert.assertNotNull(LocaleUtility.getDefaultLocale()); }
/** * @see LocaleUtility#getDefaultLocale() */ @Test public void getDefaultLocale_shouldNotReturnNullIfGlobalPropertyDoesNotExist() { // sanity check Assert.assertNull(Context.getAdministrationService().getGlobalProperty( OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE)); // check for nonnullness Assert.assertNotNull(LocaleUtility.getDefaultLocale()); }
/** * @see LocaleUtility#getDefaultLocale() */ @Test public void getDefaultLocale_shouldReturnLocaleObjectForGlobalProperty() { Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, ja")); Context.getAdministrationService() .saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "ja")); Assert.assertEquals(Locale.JAPANESE, LocaleUtility.getDefaultLocale()); }
LocaleUtility.getDefaultLocale()); Assert.assertEquals(Locale.JAPANESE, LocaleUtility.getDefaultLocale());
/** * @see LocaleUtility#getDefaultLocale() */ @Test public void getDefaultLocale_shouldNotFailWithBogusGlobalPropertyValue() { Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, asdfasdf")); Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "asdfasdf")); // check for nonnullness Assert.assertNotNull(LocaleUtility.getDefaultLocale()); Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "")); }
public FormEntryContext(Mode mode) { this.mode = mode; setupExistingData((Encounter) null); translator.setDefaultLocaleStr(LocaleUtility.getDefaultLocale().toString()); }