List of usage examples for java.util Locale getCountry
public String getCountry()
From source file:ca.oson.json.gson.functional.DefaultTypeAdaptersTest.java
public void testLocaleDeserializationWithLanguageCountryVariant() { String json = "\"de_DE_EURO\""; Locale locale = oson.fromJson(json, Locale.class); assertEquals("de", locale.getLanguage()); assertEquals("DE", locale.getCountry()); assertEquals("EURO", locale.getVariant()); }
From source file:org.exoplatform.wcm.webui.seo.UISEOForm.java
public UISEOForm() throws Exception { PortalRequestContext portalRequestContext = Util.getPortalRequestContext(); ExoContainer container = ExoContainerContext.getCurrentContainer(); SEOService seoService = (SEOService) container.getComponentInstanceOfType(SEOService.class); UIFormTextAreaInput uiTitle = new UIFormTextAreaInput(TITLE, TITLE, null); uiTitle.setValue(title);/*from w ww . j a v a 2 s . c om*/ addUIFormInput(uiTitle); UIFormTextAreaInput uiDescription = new UIFormTextAreaInput(DESCRIPTION, DESCRIPTION, null); uiDescription.setValue(description); addUIFormInput(uiDescription); UIFormTextAreaInput uiKeywords = new UIFormTextAreaInput(KEYWORDS, KEYWORDS, null); uiKeywords.setValue(keywords); addUIFormInput(uiKeywords); seoLocales = seoService.getSEOLanguages(portalRequestContext.getPortalOwner(), contentPath, onContent); seoLanguages = new ArrayList<String>(); if (seoLocales != null && seoLocales.size() > 0) { for (Locale locale : seoLocales) { String lang = locale.getLanguage(); String country = locale.getCountry(); if (StringUtils.isNotEmpty(country)) lang += "_" + country; seoLanguages.add(lang); } } if (seoLanguages != null) Collections.sort(seoLanguages); UIFormSelectBox uiSelectForm = new UIFormSelectBox(LANGUAGE_TYPE, LANGUAGE_TYPE, getLanguages()); uiSelectForm.setOnChange("Refresh"); defaultLanguage = portalRequestContext.getLocale().getLanguage(); if (StringUtils.isNotEmpty(portalRequestContext.getLocale().getCountry())) defaultLanguage += "_" + portalRequestContext.getLocale().getCountry(); selectedLanguage = defaultLanguage; if (seoLanguages == null || !seoLanguages.contains(defaultLanguage)) uiSelectForm.setValue(defaultLanguage); addUIFormInput(uiSelectForm); if (!onContent) { List<SelectItemOption<String>> robotIndexItemOptions = new ArrayList<SelectItemOption<String>>(); List<String> robotsindexOptions = seoService.getRobotsIndexOptions(); List<String> robotsfollowOptions = seoService.getRobotsFollowOptions(); List<String> frequencyOptions = seoService.getFrequencyOptions(); if (robotsindexOptions != null && robotsindexOptions.size() > 0) { for (int i = 0; i < robotsindexOptions.size(); i++) { robotIndexItemOptions.add(new SelectItemOption<String>((robotsindexOptions.get(i).toString()))); } } UIFormSelectBox robots_index = new UIFormSelectBox(ROBOTS_INDEX, null, robotIndexItemOptions); if (index != null && index.length() > 0) robots_index.setValue(index); else robots_index.setValue(ROBOTS_INDEX); addUIFormInput(robots_index); List<SelectItemOption<String>> robotFollowItemOptions = new ArrayList<SelectItemOption<String>>(); if (robotsfollowOptions != null && robotsfollowOptions.size() > 0) { for (int i = 0; i < robotsfollowOptions.size(); i++) { robotFollowItemOptions .add(new SelectItemOption<String>((robotsfollowOptions.get(i).toString()))); } } UIFormSelectBox robots_follow = new UIFormSelectBox(ROBOTS_FOLLOW, null, robotFollowItemOptions); if (follow != null && follow.length() > 0) robots_follow.setValue(follow); else robots_follow.setValue(ROBOTS_FOLLOW); addUIFormInput(robots_follow); UIFormCheckBoxInput<Boolean> visibleSitemapCheckbox = new UIFormCheckBoxInput<Boolean>(SITEMAP, SITEMAP, null); visibleSitemapCheckbox.setChecked(sitemap); addUIFormInput(visibleSitemapCheckbox); UIFormStringInput uiPrority = new UIFormStringInput(PRIORITY, null); if (!StringUtils.isEmpty(priority)) uiPrority.setValue(priority); addUIFormInput(uiPrority.addValidator(FloatNumberValidator.class)); List<SelectItemOption<String>> frequencyItemOptions = new ArrayList<SelectItemOption<String>>(); if (frequencyOptions != null && frequencyOptions.size() > 0) { for (int i = 0; i < frequencyOptions.size(); i++) { frequencyItemOptions.add(new SelectItemOption<String>(frequencyOptions.get(i).toString(), (frequencyOptions.get(i).toString()))); } } UIFormSelectBox frequencySelectbox = new UIFormSelectBox(FREQUENCY, null, frequencyItemOptions); if (frequency != null && frequency.length() > 0) frequencySelectbox.setValue(frequency); else frequencySelectbox.setValue(FREQUENCY_DEFAULT_VALUE); addUIFormInput(frequencySelectbox); } setActions(new String[] { "Save", "Cancel" }); }
From source file:org.apache.beehive.netui.compiler.model.validation.ValidationModel.java
private void writeLocaleSet(XmlModelWriter xw, Element element, LocaleSet localeSet) { Locale locale = localeSet.getLocale(); Element formSetElement = null; if (locale == null) { formSetElement = findChildElement(xw, element, "formset", "language", null, false, null); } else {// ww w .j a v a2 s . co m Element possibleMatch = findChildElement(xw, element, "formset", "language", locale.getLanguage()); if (possibleMatch != null) { String country = getElementAttribute(possibleMatch, "country"); String variant = getElementAttribute(possibleMatch, "variant"); String localeCountry = locale.getCountry(); String localeVariant = locale.getVariant(); if (((localeCountry.length() == 0 && country == null) || localeCountry.equals(country)) && ((localeVariant.length() == 0 && variant == null) || localeVariant.equals(variant))) { formSetElement = possibleMatch; } } } if (formSetElement == null) { formSetElement = xw.addElement(element, "formset"); } localeSet.writeXML(xw, formSetElement); }
From source file:com.haulmont.cuba.web.sys.CubaApplicationServlet.java
protected Locale resolveLocale(HttpServletRequest req, Messages messages, GlobalConfig globalConfig) { Map<String, Locale> locales = globalConfig.getAvailableLocales(); if (globalConfig.getLocaleSelectVisible()) { String lastLocale = getCookieValue(req, "LAST_LOCALE"); if (lastLocale != null) { for (Locale locale : locales.values()) { if (locale.toLanguageTag().equals(lastLocale)) { return locale; }/*from w w w. j a v a 2 s. c o m*/ } } } 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(); }
From source file:org.openmrs.module.emrapi.concept.HibernateEmrConceptDAO.java
@Override @Transactional(readOnly = true)/*ww w .ja va 2 s. c om*/ public List<ConceptSearchResult> conceptSearch(String query, Locale locale, Collection<ConceptClass> classes, Collection<Concept> inSets, Collection<ConceptSource> sources, Integer limit) { List<String> uniqueWords = getUniqueWords(query, locale); if (uniqueWords.size() == 0) { return Collections.emptyList(); } List<ConceptSearchResult> results = new ArrayList<ConceptSearchResult>(); // find matches based on name { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptName.class, "cn"); criteria.add(Restrictions.eq("voided", false)); if (StringUtils.isNotBlank(locale.getCountry()) || StringUtils.isNotBlank(locale.getVariant())) { Locale[] locales = new Locale[] { locale, new Locale(locale.getLanguage()) }; criteria.add(Restrictions.in("locale", locales)); } else { criteria.add(Restrictions.eq("locale", locale)); } criteria.setMaxResults(limit); Criteria conceptCriteria = criteria.createCriteria("concept"); conceptCriteria.add(Restrictions.eq("retired", false)); if (classes != null) { conceptCriteria.add(Restrictions.in("conceptClass", classes)); } if (inSets != null) { DetachedCriteria allowedSetMembers = DetachedCriteria.forClass(ConceptSet.class); allowedSetMembers.add(Restrictions.in("conceptSet", inSets)); allowedSetMembers.setProjection(Projections.property("concept")); criteria.add(Subqueries.propertyIn("concept", allowedSetMembers)); } for (String word : uniqueWords) { criteria.add(Restrictions.ilike("name", word, MatchMode.ANYWHERE)); } Set<Concept> conceptsMatchedByPreferredName = new HashSet<Concept>(); for (ConceptName matchedName : (List<ConceptName>) criteria.list()) { results.add(new ConceptSearchResult(null, matchedName.getConcept(), matchedName, calculateMatchScore(query, uniqueWords, matchedName))); if (matchedName.isLocalePreferred()) { conceptsMatchedByPreferredName.add(matchedName.getConcept()); } } // don't display synonym matches if the preferred name matches too for (Iterator<ConceptSearchResult> i = results.iterator(); i.hasNext();) { ConceptSearchResult candidate = i.next(); if (!candidate.getConceptName().isLocalePreferred() && conceptsMatchedByPreferredName.contains(candidate.getConcept())) { i.remove(); } } } // find matches based on mapping if (sources != null) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptMap.class); criteria.setMaxResults(limit); Criteria conceptCriteria = criteria.createCriteria("concept"); conceptCriteria.add(Restrictions.eq("retired", false)); if (classes != null) { conceptCriteria.add(Restrictions.in("conceptClass", classes)); } Criteria mappedTerm = criteria.createCriteria("conceptReferenceTerm"); mappedTerm.add(Restrictions.eq("retired", false)); mappedTerm.add(Restrictions.in("conceptSource", sources)); mappedTerm.add(Restrictions.ilike("code", query, MatchMode.EXACT)); for (ConceptMap mapping : (List<ConceptMap>) criteria.list()) { results.add(new ConceptSearchResult(null, mapping.getConcept(), null, calculateMatchScore(query, mapping))); } } Collections.sort(results, new Comparator<ConceptSearchResult>() { @Override public int compare(ConceptSearchResult left, ConceptSearchResult right) { return right.getTransientWeight().compareTo(left.getTransientWeight()); } }); if (results.size() > limit) { results = results.subList(0, limit); } return results; }
From source file:org.exoplatform.wcm.webui.seo.UISEOForm.java
public void initSEOForm(PageMetadataModel pageModel) throws Exception { PortalRequestContext portalRequestContext = Util.getPortalRequestContext(); if (pageModel != null) { title = pageModel.getTitle();/*from w ww . ja v a 2s.co m*/ description = pageModel.getDescription(); keywords = pageModel.getKeywords(); frequency = pageModel.getFrequency(); if (pageModel.getPriority() >= 0) priority = String.valueOf(pageModel.getPriority()); else priority = null; if (pageModel.getRobotsContent() != null && pageModel.getRobotsContent().length() > 0) { index = pageModel.getRobotsContent().split(",")[0].trim(); follow = pageModel.getRobotsContent().split(",")[1].trim(); } sitemap = pageModel.getSitemap(); } else { if (!onContent) title = portalRequestContext.getTitle(); else title = ""; description = ""; keywords = ""; priority = ""; frequency = ""; index = ""; follow = ""; sitemap = true; } ExoContainer container = ExoContainerContext.getCurrentContainer(); SEOService seoService = (SEOService) container.getComponentInstanceOfType(SEOService.class); UIFormTextAreaInput uiTitle = this.getUIFormTextAreaInput(TITLE); if (uiTitle != null) uiTitle.setValue(title); UIFormTextAreaInput uiDescription = this.getUIFormTextAreaInput(DESCRIPTION); if (uiDescription != null) uiDescription.setValue(description); UIFormTextAreaInput uiKeywords = this.getUIFormTextAreaInput(KEYWORDS); if (uiKeywords != null) uiKeywords.setValue(keywords); UIFormSelectBox uiSelectForm = this.getUIFormSelectBox(LANGUAGE_TYPE); uiSelectForm.setSelectedValues(new String[] { "language" }); if (uiSelectForm != null) { seoLocales = seoService.getSEOLanguages(portalRequestContext.getPortalOwner(), contentPath, onContent); seoLanguages = new ArrayList<String>(); if (seoLocales != null && seoLocales.size() > 0) { for (Locale locale : seoLocales) { String lang = locale.getLanguage(); String country = locale.getCountry(); if (StringUtils.isNotEmpty(country)) lang += "_" + country; seoLanguages.add(lang); } } if (seoLanguages.size() <= 0) setSelectedLanguage(null); List<SelectItemOption<String>> languages = getLanguages(); if (languages.size() == 1) this.setIsAddNew(false); else this.setIsAddNew(true); uiSelectForm.setOptions(languages); uiSelectForm.setValue(selectedLanguage); } if (!onContent) { List<SelectItemOption<String>> robotIndexItemOptions = new ArrayList<SelectItemOption<String>>(); List<String> robotsindexOptions = seoService.getRobotsIndexOptions(); List<String> robotsfollowOptions = seoService.getRobotsFollowOptions(); List<String> frequencyOptions = seoService.getFrequencyOptions(); if (robotsindexOptions != null && robotsindexOptions.size() > 0) { for (int i = 0; i < robotsindexOptions.size(); i++) { robotIndexItemOptions.add(new SelectItemOption<String>((robotsindexOptions.get(i).toString()))); } } UIFormSelectBox robots_index = this.getUIFormSelectBox(ROBOTS_INDEX); if (robots_index != null) { if (index != null && index.length() > 0) robots_index.setValue(index); else robots_index.setValue(ROBOTS_INDEX); } List<SelectItemOption<String>> robotFollowItemOptions = new ArrayList<SelectItemOption<String>>(); if (robotsfollowOptions != null && robotsfollowOptions.size() > 0) { for (int i = 0; i < robotsfollowOptions.size(); i++) { robotFollowItemOptions .add(new SelectItemOption<String>((robotsfollowOptions.get(i).toString()))); } } UIFormSelectBox robots_follow = this.getUIFormSelectBox(ROBOTS_FOLLOW); if (robots_follow != null) { if (follow != null && follow.length() > 0) robots_follow.setValue(follow); else robots_follow.setValue(ROBOTS_FOLLOW); } UIFormCheckBoxInput<Boolean> visibleSitemapCheckbox = this.getUIFormCheckBoxInput(SITEMAP); if (visibleSitemapCheckbox != null) visibleSitemapCheckbox.setChecked(sitemap); UIFormStringInput uiPrority = this.getUIStringInput(PRIORITY); if (uiPrority != null) { if (!StringUtils.isEmpty(priority)) uiPrority.setValue(priority); else uiPrority.setValue(""); } List<SelectItemOption<String>> frequencyItemOptions = new ArrayList<SelectItemOption<String>>(); if (frequencyOptions != null && frequencyOptions.size() > 0) { for (int i = 0; i < frequencyOptions.size(); i++) { frequencyItemOptions.add(new SelectItemOption<String>(frequencyOptions.get(i).toString(), (frequencyOptions.get(i).toString()))); } } UIFormSelectBox frequencySelectbox = this.getUIFormSelectBox(FREQUENCY); if (frequencySelectbox != null) { if (frequency != null && frequency.length() > 0) frequencySelectbox.setValue(frequency); else frequencySelectbox.setValue(FREQUENCY_DEFAULT_VALUE); } } }
From source file:org.openmrs.User.java
/** * Returns a list of Locales for which the User is considered proficient. * /*from w w w.ja va 2 s. c o m*/ * @return List of the User's proficient locales */ public List<Locale> getProficientLocales() { String proficientLocalesProperty = getUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES); if ((proficientLocales == null) || (!OpenmrsUtil.nullSafeEquals(parsedProficientLocalesProperty, proficientLocalesProperty))) { parsedProficientLocalesProperty = proficientLocalesProperty; proficientLocales = new ArrayList<Locale>(); if (proficientLocalesProperty != null) { String[] proficientLocalesArray = proficientLocalesProperty.split(","); 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())) { // add the language also Locale languageOnlyLocale = LocaleUtility .fromSpecification(proficientLocale.getLanguage()); if (!proficientLocales.contains(languageOnlyLocale)) { proficientLocales .add(LocaleUtility.fromSpecification(proficientLocale.getLanguage())); } } } } } } } // return a copy so that the list isn't changed by other processes return new ArrayList<Locale>(proficientLocales); }
From source file:I18NUtil.java
/** * Searches for the nearest locale from the available options. To match any locale, pass in * <tt>null</tt>./*from w w w. ja v a 2s .co m*/ * * @param templateLocale the template to search for or <tt>null</tt> to match any locale * @param options the available locales to search from * @return Returns the best match from the available options, or the <tt>null</tt> if * all matches fail */ public static Locale getNearestLocale(Locale templateLocale, Set<Locale> options) { if (options.isEmpty()) // No point if there are no options { return null; } else if (templateLocale == null) { for (Locale locale : options) { return locale; } } else if (options.contains(templateLocale)) // First see if there is an exact match { return templateLocale; } // make a copy of the set Set<Locale> remaining = new HashSet<Locale>(options); // eliminate those without matching languages Locale lastMatchingOption = null; String templateLanguage = templateLocale.getLanguage(); if (templateLanguage != null && !templateLanguage.equals("")) { Iterator<Locale> iterator = remaining.iterator(); while (iterator.hasNext()) { Locale option = iterator.next(); if (option != null && !templateLanguage.equals(option.getLanguage())) { iterator.remove(); // It doesn't match, so remove } else { lastMatchingOption = option; // Keep a record of the last match } } } if (remaining.isEmpty()) { return null; } else if (remaining.size() == 1 && lastMatchingOption != null) { return lastMatchingOption; } // eliminate those without matching country codes lastMatchingOption = null; String templateCountry = templateLocale.getCountry(); if (templateCountry != null && !templateCountry.equals("")) { Iterator<Locale> iterator = remaining.iterator(); while (iterator.hasNext()) { Locale option = iterator.next(); if (option != null && !templateCountry.equals(option.getCountry())) { // It doesn't match language - remove // Don't remove the iterator. If it matchs a langage but not the country, returns any matched language // iterator.remove(); } else { lastMatchingOption = option; // Keep a record of the last match } } } /*if (remaining.isEmpty()) { return null; } else */ if (remaining.size() == 1 && lastMatchingOption != null) { return lastMatchingOption; } else { // We have done an earlier equality check, so there isn't a matching variant // Also, we know that there are multiple options at this point, either of which will do. // This gets any country match (there will be worse matches so we take the last the country match) if (lastMatchingOption != null) { return lastMatchingOption; } else { for (Locale locale : remaining) { return locale; } } } // The logic guarantees that this code can't be called throw new RuntimeException("Logic should not allow code to get here."); }
From source file:org.radeox.util.i18n.ResourceManager.java
/** * Find a resource bundle by looking up using the locales. This is done by loading * either the specified locale based resource bundle and, if that fails, by * looping through the fallback locales to locate a usable bundle. *//*from w w w . j a v a 2 s. com*/ private ResourceBundle findBundle(String baseName) { ResourceBundle resourceBundle = null; // first try to load the resource bundle with the specified locale ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (null != locale) { try { resourceBundle = ResourceBundle.getBundle(baseName, locale, cl); } catch (Exception e) { log.fatal("unable to load a default bundle: " + baseName + "_" + locale); } // check that the requested main locale matches the resource bundle's, // since we get the system fallback locale if no match is found if (!resourceBundle.getLocale().equals(locale)) { resourceBundle = null; } } // loop through the fall back locales until a bundle is found if (null == resourceBundle) { if (null != fallback) { while (fallback.hasMoreElements()) { Locale testLocale = (Locale) fallback.nextElement(); log.debug("looking up locale " + testLocale); ResourceBundle testBundle = ResourceBundle.getBundle(baseName, testLocale, cl); String language = testBundle.getLocale().getLanguage(); String country = testBundle.getLocale().getCountry(); if (testBundle.getLocale().equals(testLocale)) { resourceBundle = testBundle; log.debug("found bundle for locale " + baseName + "_" + testBundle.getLocale()); break; } else if (testLocale.getLanguage().equals(language)) { if (testLocale.getCountry().equals(country)) { // language and country match which is good, keep looking for variant too resourceBundle = testBundle; log.debug("potential bundle: " + baseName + "_" + testBundle.getLocale()); continue; } else { // only accept this if there is no better previous lookup if (null == resourceBundle) { resourceBundle = testBundle; log.debug("potential bundle: " + baseName + "_" + testBundle.getLocale()); } continue; } } } } // make sure the resource bundle is loaded (should not happen) if (null == resourceBundle) { resourceBundle = ResourceBundle.getBundle(baseName); if (null != resourceBundle) { log.debug("system locale bundle taken: " + baseName + "_" + resourceBundle.getLocale()); } } } return resourceBundle; }
From source file:org.codelibs.fess.helper.ViewHelper.java
public String getPagePath(final String page) { final Locale locale = ComponentUtil.getRequestManager().getUserLocale(); final String lang = locale.getLanguage(); final String country = locale.getCountry(); final String pathLC = getLocalizedPagePath(page, lang, country); final String pLC = pageCacheMap.get(pathLC); if (pLC != null) { return pLC; }/*from w w w.j av a 2 s . com*/ if (existsPage(pathLC)) { pageCacheMap.put(pathLC, pathLC); return pathLC; } final String pathL = getLocalizedPagePath(page, lang, null); final String pL = pageCacheMap.get(pathL); if (pL != null) { return pL; } if (existsPage(pathL)) { pageCacheMap.put(pathLC, pathL); return pathL; } final String path = getLocalizedPagePath(page, null, null); final String p = pageCacheMap.get(path); if (p != null) { return p; } if (existsPage(path)) { pageCacheMap.put(pathLC, path); return path; } return "index.jsp"; }