19 package org.turro.i18n;
21 import java.util.Locale;
22 import javax.servlet.http.Cookie;
23 import javax.servlet.http.HttpServletRequest;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.impl.util.CookieUtil;
28 import org.turro.elephant.web.ElContext;
29 import org.turro.elephant.web.ElContextMap;
30 import org.turro.i18n.locale.I18nLocales;
31 import org.turro.i18n.mapped.I18nByKey;
32 import org.turro.i18n.mapped.I18nMap;
33 import org.turro.lock.Initializer;
41 public static String
get(String msg) {
42 return instance().get(msg);
45 public static String
get(String msg,
int count) {
46 return instance().get(msg, count);
49 public static String
format(String msg, Object... arguments) {
50 return instance().format(msg, arguments);
53 public static String
get(Locale locale, String msg) {
54 return instance().get(locale, msg);
57 public static String
get(Locale locale, String msg,
int count) {
58 return instance().get(locale, msg, count);
61 public static String
format(Locale locale, String msg, Object... arguments) {
62 return instance().format(locale, msg, arguments);
65 public static I18nApiWrapper
api() {
69 public static I18nMapWrapper
map() {
70 return instance().getAsMap();
73 public static I18nMapWrapper
map(Locale locale) {
74 return instance().getAsMap(locale);
80 return instance().keyValue(key);
83 public static String
byKey(String key) {
84 return instance().byKey(key);
87 public static String
byKey(Locale locale, String key) {
88 return instance().byKey(locale, key);
92 return instance().getByKeyAsMap();
97 private static I18nApiWrapper
api(Locale locale) {
98 return apiFrom(constructFrom(locale));
104 return instance().getLocales();
127 public static I18nLocales
locales(HttpServletRequest request) {
128 return constructFrom(request);
133 private static final String I_ATTRB =
"i_apictx";
135 private static I18nApiWrapper instance() {
138 if(request ==
null) {
142 api = (I18nApiWrapper) request.getAttribute(I_ATTRB);
144 I18nLocales
locales = constructFrom(request);
146 request.setAttribute(I_ATTRB,
api);
152 private static I18nLocales constructFrom(HttpServletRequest request) {
153 I18nLocales
locales =
new I18nLocales();
154 if(ElContextMap.getCurrent() !=
null) {
155 for(String s : ElContextMap.getCurrent().getDefaultLocales()) {
159 if(request !=
null) {
160 Cookie cookie = CookieUtil.getCookie(request,
"lang");
161 if(cookie !=
null && cookie.getValue().trim().length() > 0) {
162 locales.addPreferred(cookie.getValue());
168 private static I18nLocales constructFrom(ElContext context) {
169 I18nLocales
locales =
new I18nLocales();
170 for(String s : context.getDefaultLocales()) {
176 private static I18nLocales constructFrom(Locale locale) {
177 I18nLocales
locales =
new I18nLocales();
178 for(String s : Strings.csvToList(ElephantContext.getSiteLocales())) {
187 private static final Initializer<I18nMap> INIT =
new Initializer<>();
189 private static I18nApiWrapper apiFrom(I18nLocales
locales) {
190 I18nMap i18nMap = INIT.instance(() -> I18nMap.getFrom(I18nContext.getI18nFolder().toFile()));
191 I18nApiWrapper
api = I18nApiWrapper.from(i18nMap,
locales.getUsed());
192 api.setC(I18nCompatibilityWrapper.from(
api));
198 I18nByKey.resetMap();
static Application getApplication()
abstract HttpServletRequest getHttpServletRequest()
static ElContext getRoot()
static I18nCompatibilityMap from(I18nCompatibilityWrapper i18nCompatibilityWrapper)
static I18nCompatibilityWrapper from(I18nApiWrapper i18nApiWrapper)
static String format(String msg, Object... arguments)
static I18nByKey byKeyMap()
static String byKey(Locale locale, String key)
static String byKey(String key)
static I18nCompatibilityWrapper compatibility(Locale locale)
static String keyValue(String key)
static I18nMapWrapper map(Locale locale)
static I18nLocales getLocales()
static I18nCompatibilityMap compatibilityMap(Locale locale)
static I18nLocales locales(HttpServletRequest request)
static I18nApiWrapper api()
static I18nCompatibilityWrapper compatibility()
static I18nMapWrapper map()
static String format(Locale locale, String msg, Object... arguments)
static I18nCompatibilityMap compatibilityMap()