/** * 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))); } }
/** * 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))); } }
/** * 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 } }
/** * 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 } }
/** * 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 } }