/** * Create a new version of this object with the given transient language set. * The transient language will be taken into account when using {@link MessagesApi#preferred(RequestHeader)}} (It will take precedence over any other language). * * @param locale The language to use. * @return The new version of this object with the given transient language set. */ default RequestHeader withTransientLang(Locale locale) { return addAttr(Messages.Attrs.CurrentLang, new Lang(locale)); }
/** * Change durably the lang for the current user. * * @param code New lang code to use (e.g. "fr", "en-US", etc.) * @return true if the requested lang was supported by the application, otherwise false * * @deprecated Deprecated as of 2.7.0. Use {@link MessagesApi#setLang(Result, Lang)}. */ @Deprecated public boolean changeLang(String code) { return changeLang(Lang.forCode(code)); }
/** * Convert to a Java Locale value. */ public java.util.Locale toLocale() { return locale(); }
/** * Guess the preferred lang in the langs set passed as argument. * The first Lang that matches an available Lang wins, otherwise returns the first Lang available in this application. * * @param app the currept application * @param availableLangs the set of langs from which to guess the preferred * @return the preferred lang. */ public static Lang preferred(Application app, List<Lang> availableLangs) { play.api.i18n.Langs langs = app.injector().instanceOf(play.api.i18n.Langs.class); Stream<Lang> stream = availableLangs.stream(); List<play.api.i18n.Lang> langSeq = stream.map(l -> new play.api.i18n.Lang(l.toLocale())).collect(toList()); return new Lang(langs.preferred(Scala.toSeq(langSeq))); } }
/** * Sets the locale of the current request (if there is one) into Spring's LocaleContextHolder. * * @param <T> the return type. * @param code The code to execute while the locale is set * @return the result of the code block */ private static <T> T withRequestLocale(Lang lang, Supplier<T> code) { try { LocaleContextHolder.setLocale(lang != null ? lang.toLocale() : null); } catch(Exception e) { // Just continue (Maybe there is no context or some internal error in LocaleContextHolder). System default locale will be used. } try { return code.get(); } finally { LocaleContextHolder.resetLocaleContext(); // Clean up ThreadLocal } }
/** * Change durably the lang for the current user. * * @param lang New Lang object to use * @return true if the requested lang was supported by the application, otherwise false. * * @deprecated Deprecated as of 2.7.0. Use {@link MessagesApi#setLang(Result, Lang)}. */ @Deprecated public boolean changeLang(Lang lang) { if (langs().availables().contains(lang)) { this.lang = lang; scala.Option<String> domain = sessionDomain(); Cookie langCookie = new Cookie(messagesApi().langCookieName(), lang.code(), null, sessionPath(), domain.isDefined() ? domain.get() : null, messagesApi().langCookieSecure(), messagesApi().langCookieHttpOnly(), messagesApi().langCookieSameSite().orElse(null) ); response.setCookie(langCookie); return true; } else { return false; } }
/** * Change durably the lang for the current user. * @param lang New Lang object to use * @return true if the requested lang was supported by the application, otherwise false */ public boolean changeLang(Lang lang) { if (Lang.availables().contains(lang)) { this.lang = lang; scala.Option<String> domain = play.api.mvc.Session.domain(); response.setCookie(Play.langCookieName(), lang.code(), null, play.api.mvc.Session.path(), domain.isDefined() ? domain.get() : null, Play.langCookieSecure(), Play.langCookieHttpOnly()); return true; } else { return false; } }
/** * Guess the preferred lang in the langs set passed as argument. * The first Lang that matches an available Lang wins, otherwise returns the first Lang available in this application. * * @param app the currept application * @param availableLangs the set of langs from which to guess the preferred * @return the preferred lang. */ public static Lang preferred(Application app, List<Lang> availableLangs) { play.api.i18n.Langs langs = app.injector().instanceOf(play.api.i18n.Langs.class); Stream<Lang> stream = availableLangs.stream(); List<play.api.i18n.Lang> langSeq = stream.map(l -> new play.api.i18n.Lang(l.toLocale())).collect(toList()); return new Lang(langs.preferred(Scala.toSeq(langSeq))); } }
/** * Sets the locale of the current request (if there is one) into Spring's LocaleContextHolder. * * @param <T> the return type. * @param code The code to execute while the locale is set * @return the result of the code block */ private static <T> T withRequestLocale(Lang lang, Supplier<T> code) { try { LocaleContextHolder.setLocale(lang != null ? lang.toLocale() : null); } catch(Exception e) { // Just continue (Maybe there is no context or some internal error in LocaleContextHolder). System default locale will be used. } try { return code.get(); } finally { LocaleContextHolder.resetLocaleContext(); // Clean up ThreadLocal } }
/** * Change durably the lang for the current user. * * @param lang New Lang object to use * @return true if the requested lang was supported by the application, otherwise false. * * @deprecated Deprecated as of 2.7.0. Use {@link MessagesApi#setLang(Result, Lang)}. */ @Deprecated public boolean changeLang(Lang lang) { if (langs().availables().contains(lang)) { this.lang = lang; scala.Option<String> domain = sessionDomain(); Cookie langCookie = new Cookie(messagesApi().langCookieName(), lang.code(), null, sessionPath(), domain.isDefined() ? domain.get() : null, messagesApi().langCookieSecure(), messagesApi().langCookieHttpOnly(), messagesApi().langCookieSameSite().orElse(null) ); response.setCookie(langCookie); return true; } else { return false; } }
/** * Create a Lang value from a code (such as fr or en-US). * * @param code the language code * @return the Lang for the code, or null of no matching lang was found. */ public static Lang forCode(String code) { try { return new Lang(play.api.i18n.Lang.apply(code)); } catch (Exception e) { return null; } }
/** * Guess the preferred lang in the langs set passed as argument. * The first Lang that matches an available Lang wins, otherwise returns the first Lang available in this application. * * @param app the currept application * @param availableLangs the set of langs from which to guess the preferred * @return the preferred lang. */ public static Lang preferred(Application app, List<Lang> availableLangs) { play.api.i18n.Langs langs = app.injector().instanceOf(play.api.i18n.Langs.class); Stream<Lang> stream = availableLangs.stream(); List<play.api.i18n.Lang> langSeq = stream.map(l -> new play.api.i18n.Lang(l.toLocale())).collect(toList()); return new Lang(langs.preferred(Scala.toSeq(langSeq))); } }
/** * Change durably the lang for the current user. * * @param code New lang code to use (e.g. "fr", "en-US", etc.) * @return true if the requested lang was supported by the application, otherwise false * * @deprecated Deprecated as of 2.7.0. Use {@link MessagesApi#setLang(Result, Lang)}. */ @Deprecated public boolean changeLang(String code) { return changeLang(Lang.forCode(code)); }
/** * Convert to a Java Locale value. */ public java.util.Locale toLocale() { return locale(); }
/** * Sets the locale of the current request (if there is one) into Spring's LocaleContextHolder. * * @param <T> the return type. * @param code The code to execute while the locale is set * @return the result of the code block */ private static <T> T withRequestLocale(Lang lang, Supplier<T> code) { try { LocaleContextHolder.setLocale(lang != null ? lang.toLocale() : null); } catch(Exception e) { // Just continue (Maybe there is no context or some internal error in LocaleContextHolder). System default locale will be used. } try { return code.get(); } finally { LocaleContextHolder.resetLocaleContext(); // Clean up ThreadLocal } }
/** * Change durably the lang for the current user. * * @param lang New Lang object to use * @return true if the requested lang was supported by the application, otherwise false. * * @deprecated Deprecated as of 2.7.0. Use {@link MessagesApi#setLang(Result, Lang)}. */ @Deprecated public boolean changeLang(Lang lang) { if (langs().availables().contains(lang)) { this.lang = lang; scala.Option<String> domain = sessionDomain(); Cookie langCookie = new Cookie(messagesApi().langCookieName(), lang.code(), null, sessionPath(), domain.isDefined() ? domain.get() : null, messagesApi().langCookieSecure(), messagesApi().langCookieHttpOnly(), messagesApi().langCookieSameSite().orElse(null) ); response.setCookie(langCookie); return true; } else { return false; } }
/** * Create a new version of this object with the given transient language set. * The transient language will be taken into account when using {@link MessagesApi#preferred(RequestHeader)}} (It will take precedence over any other language). * * @param locale The language to use. * @return The new version of this object with the given transient language set. */ default RequestHeader withTransientLang(Locale locale) { return addAttr(Messages.Attrs.CurrentLang, new Lang(locale)); }
/** * Create a new version of this object with the given transient language set. * The transient language will be taken into account when using {@link MessagesApi#preferred(RequestHeader)}} (It will take precedence over any other language). * * @param code The language to use. * @return The new version of this object with the given transient language set. */ default RequestHeader withTransientLang(String code) { return addAttr(Messages.Attrs.CurrentLang, Lang.forCode(code)); }
/** * Convert to a Java Locale value. */ public java.util.Locale toLocale() { return locale(); }
private static List<Locale> requestAcceptedLanguages(final Http.Context context, final ProjectContext projectContext) { return context.request().acceptLanguages().stream() .map(lang -> Locale.forLanguageTag(lang.code())) .filter(projectContext::isLocaleSupported) .collect(toList()); }