List of usage examples for java.util Locale getLanguage
public String getLanguage()
From source file:de.uni_koeln.spinfo.maalr.webapp.i18n.InternationalUrlFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //long start = System.nanoTime(); HttpServletRequest h = (HttpServletRequest) request; String uri = h.getRequestURI(); String path = h.getContextPath(); Locale userLocale = request.getLocale(); h.getSession().setAttribute("lang", userLocale.getLanguage()); String url = uri.substring(path.length()); /*//w w w . j a va 2 s.c o m * Idee: die URL in ihre Bestandteile zerlegen und die einzelnen Teile * in einem Keyword-Tree matchen. Auf jeder Stufe wird dabei die eigentliche * URL um ein neues Element ergnzt. * * Funktioniert auf diese Weise gut fr statische URLs, aufbau eines Baums * fr URLs mit fest definierten Klassen (z.B. bersetzungsrichtung) ist ebenfalls mglich. * * Funktioniert aber nicht fr dynamische Bestandteile - da muss eine Wildcard * rein. * * :wrterbuch:deutsch-rumantsch:nase.html oder .json oder .xml * * Baum: * wrterbuch -> dictionary * deutsch-rumantsch -> tudesg->rumantsch * * -> * * ending * * Filter-Funktionalitt: * * a) Prfen, ob die URL schon in die Zielsprache bersetzt wurde. Falls ja: * doFilter() aufrufen, sonst bersetzen (andernfalls Endlos-Schleife). * * b) die gewhlte Sprache irgendwo in der Session hinterlegen, damit der * Rest des Programms (GUI) entsprechend der URL auch die richtigen Elemente * darstellt. * * c) Hilfsklasse notwendig, die URLs entsprechend der Sprache generiert, * z.B. fr dictionary (s.o.), aber auch fr "translate", einschlielich der * Durchblttern-Funktion. HTTP-GET sollte auch bersetzt werden (also Parameter-Namen), * POST nicht. * * d) Die Generierung des Baums darf lange dauern, die Abfrage muss aber * schnell sein - also z.B. Pattern.compile() beim Aufbau des Baums, nicht * beim Abfragen. * */ if ("/hilfe.html".equals(url) || "/help.html".equals(url)) { // change url and forward RequestDispatcher dispatcher = request.getRequestDispatcher("/agid.html"); // long end = System.nanoTime(); dispatcher.forward(request, response); } else { // long end = System.nanoTime(); chain.doFilter(request, response); } }
From source file:org.cee.service.language.LanguageService.java
public LanguageList getSupportedLanguages() { List<String> langIds = searchService.getSupportedLanguages(); if (langIds == null) { //search service does not support languages at all, provide user with full list of languages langIds = Arrays.asList(Locale.getISOLanguages()); }/*from w w w .j a va 2s. c o m*/ List<EntityKey> languages = new ArrayList<EntityKey>(); int defaultLanguageIndex = 0; String defaultLanguage = "en"; Locale currentUserLocale = LocaleContextHolder.getLocaleContext().getLocale(); if (currentUserLocale != null) { defaultLanguage = currentUserLocale.getLanguage(); } for (String langId : langIds) { Locale locale = Locale.forLanguageTag(langId); if (locale != null) { if (locale.getLanguage().startsWith(defaultLanguage) || defaultLanguage.startsWith(locale.getLanguage())) { defaultLanguageIndex = languages.size(); } languages.add(EntityKey.get(locale.getDisplayLanguage(), langId)); } else { languages.add(EntityKey.get(langId, langId)); } } LanguageList languageList = new LanguageList(); languageList.setLanguages(languages); languageList.setDefaultLanguage(defaultLanguageIndex); return languageList; }
From source file:com.cyclopsgroup.waterview.ui.view.system.status.SetLocale.java
/** * Overwrite or implement method execute() * * @see com.cyclopsgroup.waterview.Module#execute(com.cyclopsgroup.waterview.RuntimeData, com.cyclopsgroup.waterview.Context) */// ww w .j a va 2 s . c om public void execute(RuntimeData data, Context context) throws Exception { Locale[] locales = Locale.getAvailableLocales(); TreeMap availableLocales = new TreeMap(); for (int i = 0; i < locales.length; i++) { Locale locale = locales[i]; if (StringUtils.isEmpty(locale.getCountry())) { continue; } String key = locale.getCountry() + '|' + locale.getLanguage(); availableLocales.put(key, locale); } context.put("availableLocales", availableLocales.values()); context.put("currentLocale", data.getSessionContext().get(RuntimeData.LOCALE_NAME)); }
From source file:eu.supersede.fe.rest.ApplicationRest.java
@RequestMapping("/page") public List<ApplicationGrouped> getUserAuthenticatedApplicationsPage(Authentication auth, Locale locale) { String lang = locale.getLanguage(); Map<String, ApplicationGrouped> appsMap = new HashMap<>(); Map<String, Map<String, Page>> appsPagesMap = new HashMap<>(); List<ApplicationGrouped> applications = new ArrayList<>(); List<String> authNames = new ArrayList<>(); Collection<? extends GrantedAuthority> authorities = auth.getAuthorities(); for (GrantedAuthority ga : authorities) { authNames.add(ga.getAuthority().substring(5)); }/* w ww . j a v a 2s. c o m*/ List<Profile> profList = profiles.findByNameIn(authNames); // make data nicer for frontend for (Profile p : profList) { Set<ApplicationPage> apps = applicationUtil.getApplicationsPagesByProfileName(p.getName()); for (ApplicationPage app : apps) { ApplicationGrouped ag; if (!appsMap.containsKey(app.getApplicationName())) { Application a = applicationUtil.getApplication(app.getApplicationName()); String appLabel = a.getLocalizedApplicationLabel(lang); ag = new ApplicationGrouped(app.getApplicationName(), appLabel); ag.setHomePage(a.getHomePage()); applications.add(ag); appsMap.put(app.getApplicationName(), ag); appsPagesMap.put(app.getApplicationName(), new HashMap<String, Page>()); } else { ag = appsMap.get(app.getApplicationName()); } Page page; if (!appsPagesMap.get(app.getApplicationName()).containsKey(app.getApplicationPage())) { page = new Page(app.getApplicationPage(), app.getLocalizedApplicationPageLabel(lang)); ag.getPages().add(page); appsPagesMap.get(app.getApplicationName()).put(app.getApplicationPage(), page); } } } return applications; }
From source file:LocaleMap.java
private String getFullKey(Locale locale) { return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant(); }
From source file:com.github.jrh3k5.plugin.maven.l10n.data.AbstractMessagesPropertiesParserTest.java
/** * Test the determination of supported locale for a messages properties file with only the language specified. * /*from ww w .j av a2s . c o m*/ * @throws Exception * If any errors occur during the test run. */ @Test public void testDetermineSupportedLocale() throws Exception { final File messagesFile = new File("messages_es.properties"); final Locale supportedLocale = parser.determineSupportedLocale(messagesFile); assertThat(supportedLocale.getLanguage()).isEqualTo("es"); assertThat(supportedLocale.getCountry()).isEmpty(); }
From source file:com.github.jrh3k5.plugin.maven.l10n.data.AbstractMessagesPropertiesParserTest.java
/** * Test the determination of locale support with a specified country. * //from www. ja v a2 s. c om * @throws Exception * If any errors occur during the test run. */ @Test public void testDetermineSupportedLocaleWithCountry() throws Exception { final File messagesFile = new File("messages_es_MX.properties"); final Locale supportedLocale = parser.determineSupportedLocale(messagesFile); assertThat(supportedLocale.getLanguage()).isEqualTo("es"); assertThat(supportedLocale.getCountry()).isEqualTo("MX"); }
From source file:com.puppycrawl.tools.checkstyle.filters.SuppressWarningsFilterTest.java
@Override protected Checker createChecker(Configuration checkConfig) throws Exception { final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration"); final DefaultConfiguration checksConfig = createCheckConfig(TreeWalker.class); final DefaultConfiguration holderConfig = createCheckConfig(SuppressWarningsHolder.class); holderConfig.addAttribute("aliasList", "com.puppycrawl.tools.checkstyle.checks.sizes." + "ParameterNumberCheck=paramnum"); checksConfig.addChild(holderConfig); checksConfig.addChild(createCheckConfig(MemberNameCheck.class)); checksConfig.addChild(createCheckConfig(ConstantNameCheck.class)); checksConfig.addChild(createCheckConfig(ParameterNumberCheck.class)); checksConfig.addChild(createCheckConfig(IllegalCatchCheck.class)); checkerConfig.addChild(checksConfig); if (checkConfig != null) { checkerConfig.addChild(checkConfig); }//from w w w.j a v a 2s . com final Checker checker = new Checker(); final Locale locale = Locale.ROOT; checker.setLocaleCountry(locale.getCountry()); checker.setLocaleLanguage(locale.getLanguage()); checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader()); checker.configure(checkerConfig); checker.addListener(new BriefLogger(stream)); return checker; }
From source file:com.ibm.watson.WatsonTranslate.java
public WatsonTranslate(Locale locale) { String bcp47Tag = locale.toLanguageTag(); String isoLang = locale.getLanguage(); logger.debug("BCP 47 language tag {}", bcp47Tag); logger.debug("ISO language tag {}", isoLang); // see if this is a supported language for Watson translate if (isoLang.equalsIgnoreCase("es")) { watsonLangPair = "mt-enus-eses"; } else if (isoLang.equalsIgnoreCase("fr")) { watsonLangPair = "mt-enus-frfr"; } else if (bcp47Tag.equalsIgnoreCase("pt-BR")) { watsonLangPair = "mt-enus-ptbr"; }/*from w w w .java2 s . c om*/ }
From source file:fr.cls.atoll.motu.library.converter.jaxb.LocaleAdapter.java
/** * Convert a given uri into a string representation. * /*from ww w . j a v a2s.c o m*/ * @param locale the locale * * @return the string representation. */ @Override public String marshal(Locale locale) { if (locale == null) { return null; } return locale.getLanguage() + (StringUtils.isNotEmpty(locale.getCountry()) ? "-" + locale.getCountry() : ""); }