/** * @see Languages#getLanguageOrDefault(String) */ protected final String getLanguageOrDefault(String language) { return languages.getLanguageOrDefault(language); }
/** * @see Languages#getLanguageOrDefault(RouteContext) */ protected final String getLanguageOrDefault(RouteContext routeContext) { return languages.getLanguageOrDefault(routeContext); }
/** * Returns the Java Locale for the specified language or the Locale for the * default language if the requested language can not be mapped to a Locale. * * @param language * @return a Java Locale */ public Locale getLocaleOrDefault(String language) { String lang = getLanguageOrDefault(language); return Locale.forLanguageTag(lang); }
/** * Returns the Java Locale for the Request & Response cycle. If the language * specified in the Request/Response cycle can not be mapped to a Java * Locale, the default language Locale is returned. * * @param routeContext * @return a Java Locale */ public Locale getLocaleOrDefault(RouteContext routeContext) { String language = getLanguageOrDefault(routeContext); return Locale.forLanguageTag(language); }
/** * Returns true if the exact language is a registered language in this * application. * <p> * For example, this application may have a Portuguese translation * (messages_pt.properties) but not a Brazilian Portuguese translation * (messages_pt-BR.properties). In this case, pt-BR is not a registered * language, though it may be a supported language by the 'pt' language * component. * </p> * * @param language * @return true if the language is registered */ public boolean isRegisteredLanguage(String language) { String lang = getLanguageOrDefault(language); return getRegisteredLanguages().contains(lang); }
/** * Returns all localized messages. * <p> * The current Request and Response are used to help determine the messages * resource to use. * <ol> * <li>Exact locale match, return the registered locale messages * <li>Language match but not a locale match, return the registered language * messages * <li>Return the default messages * </ol> * </p> * * @param routeContext * @return all localized messages */ public Map<String, String> getAll(RouteContext routeContext) { String language = languages.getLanguageOrDefault(routeContext); return getAll(language); }
/** * Gets the requested localized message. * <p/> * <p> * The current Request and Response are used to help determine the messages * resource to use. * <ol> * <li>Exact locale match, return the registered locale message * <li>Language match, but not a locale match, return the registered * language message * <li>Return the default resource message * </ol> * <p> * The message can be formatted with optional arguments using the * {@link java.text.MessageFormat} syntax. * </p> * <p> * If the key does not exist in the messages resource, then the key name is * returned. * </p> * * @param key * @param routeContext * @param args * @return the message or the key if the key does not exist */ public String get(String key, RouteContext routeContext, Object... args) { String language = languages.getLanguageOrDefault(routeContext); return get(key, language, args); }
String language = languages.getLanguageOrDefault(routeContext); return getWithDefault(key, defaultMessage, language, args);
private VelocityContext createVelocityContext(Map<String, Object> model) { // prepare the locale-aware i18n method String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } // prepare the locale-aware prettyTime method Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } VelocityContext context = new VelocityContext(model); context.put("pippo", new PippoHelper(messages, language, locale, router)); context.put("contextPath", router.getContextPath()); context.put("appPath", router.getApplicationPath()); return context; }
String supportedLanguage = languages.getLanguageOrDefault(language); if (StringUtils.isNullOrEmpty(supportedLanguage)) { log.debug("Messages for '{}' were requested. Using default messages.", language);
return getLanguageOrDefault(cookie.getValue()); return getLanguageOrDefault(cookie.getValue()); language = getLanguageOrDefault(language); return language; return getLanguageOrDefault(acceptLanguage);
/** * Sets the application language cookie. * <p> * If the language does not match a registered language or language * component an exception is thrown. * </p> * * @param language * @param routeContext * @throws PippoRuntimeException */ public void setLanguageCookie(String language, RouteContext routeContext) { String lang = getLanguageOrDefault(language); if (lang.equals(language)) { Cookie cookie = generateLanguageCookie(language); routeContext.getResponse().cookie(cookie); } else { throw new PippoRuntimeException("'{}' is not a registered language!", language); } }
@SuppressWarnings("unchecked") public void setup(Languages languages, Messages messages, Router router) { this.languages = languages; this.messages = messages; this.router = router; // set global template variables getModel().put("contextPath", router.getContextPath()); getModel().put("appPath", router.getApplicationPath()); String language = (String) getModel().get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } this.language = language; // prepare the locale Locale locale = (Locale) getModel().get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } this.locale = locale; }
@Override public void handle(RouteContext routeContext) { String language = enableQueryParameter ? routeContext.getParameter(PippoConstants.REQUEST_PARAMETER_LANG).toString() : null; if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(routeContext); } Locale locale = languages.getLocaleOrDefault(language); routeContext.setLocal(PippoConstants.REQUEST_PARAMETER_LANG, language); routeContext.setLocal(PippoConstants.REQUEST_PARAMETER_LOCALE, locale); if (setCookie) { if (routeContext.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, routeContext); } } routeContext.next(); }
private VelocityContext createVelocityContext(Map<String, Object> model) { // prepare the locale-aware i18n method String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } // prepare the locale-aware prettyTime method Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } VelocityContext context = new VelocityContext(model); context.put("pippo", new PippoHelper(messages, language, locale, router)); context.put("contextPath", router.getContextPath()); context.put("appPath", router.getApplicationPath()); return context; }
@SuppressWarnings("unchecked") public void setup(Languages languages, Messages messages, Router router) { this.languages = languages; this.messages = messages; this.router = router; // set global template variables getModel().put("contextPath", router.getContextPath()); getModel().put("appPath", router.getApplicationPath()); String language = (String) getModel().get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } this.language = language; // prepare the locale Locale locale = (Locale) getModel().get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } this.locale = locale; }
@Override public void handle(Context context) { String language = enableQueryParameter ? context.getParameter(Parameter.LANG).toString() : null; if (Strings.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(context); } Locale locale = languages.getLocaleOrDefault(language); context.setLocal(Parameter.LANG, language); context.setLocal(Parameter.LOCALE, locale); context.setLocal(Parameter.LANGUAGES, languages.getRegisteredLanguages()); if (setCookie) { if (context.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, context); } } context.next(); }
@Override public void handle(Context context) { String language = enableQueryParameter ? context.getParameter(Parameter.LANG).toString() : null; if (Strings.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(context); } Locale locale = languages.getLocaleOrDefault(language); context.setLocal(Parameter.LANG, language); context.setLocal(Parameter.LOCALE, locale); context.setLocal(Parameter.LANGUAGES, languages.getRegisteredLanguages()); if (setCookie) { if (context.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, context); } } context.next(); }