/** * @return first locale from the list defined in {@code cuba.availableLocales} app property, taking into * account {@link #useLocaleLanguageOnly()} return value. */ public Locale getDefaultLocale() { if (globalConfig.getAvailableLocales().isEmpty()) throw new DevelopmentException("Invalid cuba.availableLocales application property"); return globalConfig.getAvailableLocales().entrySet().iterator().next().getValue(); }
protected Locale localeFromString(String localeStr) { return StringUtils.isBlank(localeStr) ? globalConfig.getAvailableLocales().values().iterator().next() : LocaleUtils.toLocale(localeStr); }
/** * @return whether to use a full locale representation, or language only. Returns true if all locales listed * in {@code cuba.availableLocales} app property are language only. */ public boolean useLocaleLanguageOnly() { if (useLocaleLanguageOnly == null) { boolean found = false; for (Locale locale : globalConfig.getAvailableLocales().values()) { if (!StringUtils.isEmpty(locale.getCountry()) || !StringUtils.isEmpty(locale.getVariant())) { useLocaleLanguageOnly = false; found = true; break; } } if (!found) useLocaleLanguageOnly = true; } return useLocaleLanguageOnly; }
@Override public void init(Map<String, Object> params) { Map<String, Locale> map = globalConfig.getAvailableLocales(); for (Map.Entry<String, Locale> entry : map.entrySet()) { localesScrollBox.add(createLocaleListComponent(entry.getValue(), entry.getKey())); } }
/** * Method extracts locale information from the Accept-Language header. If no such header is specified or the * passed locale is not among application available locales, then null is returned */ @Nullable public Locale extractLocaleFromRequestHeader(HttpServletRequest request) { Locale locale = null; if (!Strings.isNullOrEmpty(request.getHeader(HttpHeaders.ACCEPT_LANGUAGE))) { Locale requestLocale = request.getLocale(); GlobalConfig globalConfig = configuration.getConfig(GlobalConfig.class); Map<String, Locale> availableLocales = globalConfig.getAvailableLocales(); if (availableLocales.values().contains(requestLocale)) { locale = requestLocale; } else { log.warn("Locale {} passed in the Accept-Language header is not supported by the application. It was ignored."); } } return locale; } }
@GetMapping(value = "/locales", produces = "application/json; charset=UTF-8") @ResponseBody public LocalesInfo getLocales() { LocalesInfo localesInfo = new LocalesInfo(); localesInfo.setLocaleSelectVisible(globalConfig.getLocaleSelectVisible()); Map<String, String> locales = new LinkedHashMap<>(); for (Map.Entry<String, Locale> entry : globalConfig.getAvailableLocales().entrySet()) { locales.put(entry.getValue().toLanguageTag(), entry.getKey()); } localesInfo.setLocales(locales); return localesInfo; }
public Properties getFreeMarkerSettings() { GlobalConfig globalConfig = configuration.getConfig(GlobalConfig.class); Map<String, Locale> availableLocales = globalConfig.getAvailableLocales(); if (availableLocales.isEmpty()) { throw new IllegalStateException("Property cuba.availableLocales is not configured"); } Locale locale = availableLocales.values().iterator().next(); FormatStrings formatStrings = formatStringsRegistry.getFormatStrings(locale); final Properties freemarkerSettings = new Properties(); freemarkerSettings.setProperty("number_format", "#"); freemarkerSettings.setProperty("datetime_format", formatStrings.getDateTimeFormat()); freemarkerSettings.setProperty("date_format", formatStrings.getDateFormat()); freemarkerSettings.setProperty("template_exception_handler", "rethrow"); return freemarkerSettings; } }
private String formatEnum(Enum enumValue) { Messages messages = AppBeans.get(Messages.class); GlobalConfig globalConfig = AppBeans.get(Configuration.class).getConfig(GlobalConfig.class); Set<String> localizedValues = new HashSet<>(); for (Locale locale : globalConfig.getAvailableLocales().values()) { localizedValues.add(messages.getMessage(enumValue, locale)); } return Joiner.on(" ").join(localizedValues); }
protected Locale resolveLocale(@Nullable Locale requestLocale) { Map<String, Locale> locales = globalConfig.getAvailableLocales(); if (globalConfig.getLocaleSelectVisible()) { String lastLocale = getCookieValue(COOKIE_LOCALE); if (lastLocale != null) { for (Locale locale : locales.values()) { if (locale.toLanguageTag().equals(lastLocale)) { return locale; } } } } if (requestLocale != null) { Locale requestTrimmedLocale = messageTools.trimLocale(requestLocale); if (locales.containsValue(requestTrimmedLocale)) { return requestTrimmedLocale; } // if not found and application locale contains country, try to match by language only if (!StringUtils.isEmpty(requestLocale.getCountry())) { Locale appLocale = Locale.forLanguageTag(requestLocale.getLanguage()); for (Locale locale : locales.values()) { if (Locale.forLanguageTag(locale.getLanguage()).equals(appLocale)) { return locale; } } } } // return default locale return messageTools.getDefaultLocale(); }
protected Locale resolveLocale(HttpServletRequest req, Messages messages, GlobalConfig globalConfig) { Map<String, Locale> locales = globalConfig.getAvailableLocales(); if (globalConfig.getLocaleSelectVisible()) { String lastLocale = getLocaleFromCookie(req); if (lastLocale != null) { for (Locale locale : locales.values()) { if (locale.toLanguageTag().equals(lastLocale)) { return locale; } } } } Locale requestLocale = req.getLocale(); if (requestLocale != null) { Locale requestTrimmedLocale = messages.getTools().trimLocale(requestLocale); if (locales.containsValue(requestTrimmedLocale)) { return requestTrimmedLocale; } // if not found and application locale contains country, try to match by language only if (!StringUtils.isEmpty(requestLocale.getCountry())) { Locale appLocale = Locale.forLanguageTag(requestLocale.getLanguage()); for (Locale locale : locales.values()) { if (Locale.forLanguageTag(locale.getLanguage()).equals(appLocale)) { return locale; } } } } return messages.getTools().getDefaultLocale(); }
protected void createLanguageLookup() { languageLookup = uiComponents.create(LookupField.TYPE_STRING); FieldGroup.FieldConfig languageLookupFc = fieldGroupRight.getFieldNN("language"); languageLookup.setDatasource(languageLookupFc.getTargetDatasource(), languageLookupFc.getProperty()); languageLookup.setRequired(false); Map<String, Locale> locales = configuration.getConfig(GlobalConfig.class).getAvailableLocales(); Map<String, String> options = new TreeMap<>(); for (Map.Entry<String, Locale> entry : locales.entrySet()) { options.put(entry.getKey(), messages.getTools().localeToString(entry.getValue())); } languageLookup.setOptionsMap(options); languageLookupFc.setComponent(languageLookup); }
protected void initLocalizedFrame() { if (globalConfig.getAvailableLocales().size() > 1) { tabsheet.getTab("localization").setVisible(true); localizedFrame = (LocalizedNameFrame) openFrame( tabsheet.getTabComponent("localization"), "localizedNameFrame"); localizedFrame.setWidth("100%"); localizedFrame.setHeight("250px"); } }
protected void initLocalizedFrame() { if (globalConfig.getAvailableLocales().size() > 1) { localizedGroupBox.setVisible(true); localizedFrame = (LocalizedNameFrame) openFrame(localizedGroupBox, "localizedNameFrame"); localizedFrame.setWidth("100%"); localizedFrame.setHeight(AUTO_SIZE); localizedFrame.setValue(category.getLocaleNames()); } }
} else { try { Map<String, Locale> availableLocales = configuration.getConfig(GlobalConfig.class).getAvailableLocales(); Locale defaultLocale = availableLocales.values().iterator().next();
for (Locale locale : globalConfig.getAvailableLocales().values()) { String numberDecimalSeparator = getMainMessage("numberDecimalSeparator", locale); String numberGroupingSeparator = getMainMessage("numberGroupingSeparator", locale);
protected void initLocalesField() { Map<String, Locale> locales = globalConfig.getAvailableLocales(); localesSelect.setOptionsMap(locales); localesSelect.addValueChangeListener(createLocaleSelectValueChangeListener()); localesSelect.setValue(userSessionSource.getLocale()); }
Map<String, Locale> availableLocales = globalConfig.getAvailableLocales(); Locale requestedLocale = Locale.forLanguageTag(auth.getLocale());
protected void initLocales() { localesSelect.setOptionsMap(globalConfig.getAvailableLocales()); localesSelect.setValue(app.getLocale()); boolean localeSelectVisible = globalConfig.getLocaleSelectVisible(); localesSelect.setVisible(localeSelectVisible); // if old layout is used Component localesSelectLabel = getComponent("localesSelectLabel"); if (localesSelectLabel != null) { localesSelectLabel.setVisible(localeSelectVisible); } localesSelect.addValueChangeListener(e -> { Locale selectedLocale = (Locale) e.getValue(); app.setLocale(selectedLocale); authInfoThreadLocal.set(new AuthInfo(loginField.getValue(), passwordField.getValue(), rememberMeCheckBox.getValue())); try { app.createTopLevelWindow(); } finally { authInfoThreadLocal.set(null); } }); }
if (!globalConfig.getAvailableLocales().containsValue(requestLocale)) { requestLocale = null;
Map<String, Locale> locales = globalConfig.getAvailableLocales(); TreeMap<String, String> options = new TreeMap<>(); for (Map.Entry<String, Locale> entry : locales.entrySet()) {