BrightSide Workbench Full Report + Source Code
I_.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.i18n;
20 
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;
34 
39 public class I_ {
40 
41  public static String get(String msg) {
42  return instance().get(msg);
43  }
44 
45  public static String get(String msg, int count) {
46  return instance().get(msg, count);
47  }
48 
49  public static String format(String msg, Object... arguments) {
50  return instance().format(msg, arguments);
51  }
52 
53  public static String get(Locale locale, String msg) {
54  return instance().get(locale, msg);
55  }
56 
57  public static String get(Locale locale, String msg, int count) {
58  return instance().get(locale, msg, count);
59  }
60 
61  public static String format(Locale locale, String msg, Object... arguments) {
62  return instance().format(locale, msg, arguments);
63  }
64 
65  public static I18nApiWrapper api() {
66  return instance();
67  }
68 
69  public static I18nMapWrapper map() {
70  return instance().getAsMap();
71  }
72 
73  public static I18nMapWrapper map(Locale locale) {
74  return instance().getAsMap(locale);
75  }
76 
77  /* By key */
78 
79  public static String keyValue(String key) {
80  return instance().keyValue(key);
81  }
82 
83  public static String byKey(String key) {
84  return instance().byKey(key);
85  }
86 
87  public static String byKey(Locale locale, String key) {
88  return instance().byKey(locale, key);
89  }
90 
91  public static I18nByKey byKeyMap() {
92  return instance().getByKeyAsMap();
93  }
94 
95  /* Specific locale */
96 
97  private static I18nApiWrapper api(Locale locale) {
98  return apiFrom(constructFrom(locale));
99  }
100 
101  /* Locales */
102 
103  public static I18nLocales getLocales() {
104  return instance().getLocales();
105  }
106 
107  /* Compatibility */
108 
110  return I18nCompatibilityWrapper.from(instance());
111  }
112 
113  public static I18nCompatibilityWrapper compatibility(Locale locale) {
114  return I18nCompatibilityWrapper.from(api(locale));
115  }
116 
119  }
120 
121  public static I18nCompatibilityMap compatibilityMap(Locale locale) {
122  return I18nCompatibilityMap.from(compatibility(locale));
123  }
124 
125  /* Locales */
126 
127  public static I18nLocales locales(HttpServletRequest request) {
128  return constructFrom(request);
129  }
130 
131  /* Factory */
132 
133  private static final String I_ATTRB = "i_apictx";
134 
135  private static I18nApiWrapper instance() {
136  I18nApiWrapper api;
137  HttpServletRequest request = Application.getApplication().getHttpServletRequest();
138  if(request == null) {
139  I18nLocales locales = constructFrom(ElContextMap.getRoot());
140  api = apiFrom(locales);
141  } else {
142  api = (I18nApiWrapper) request.getAttribute(I_ATTRB);
143  if(api == null) {
144  I18nLocales locales = constructFrom(request);
145  api = apiFrom(locales);
146  request.setAttribute(I_ATTRB, api);
147  }
148  }
149  return api;
150  }
151 
152  private static I18nLocales constructFrom(HttpServletRequest request) {
153  I18nLocales locales = new I18nLocales();
154  if(ElContextMap.getCurrent() != null) {
155  for(String s : ElContextMap.getCurrent().getDefaultLocales()) {
156  locales.addConfigured(s);
157  }
158  }
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());
163  }
164  }
165  return locales;
166  }
167 
168  private static I18nLocales constructFrom(ElContext context) {
169  I18nLocales locales = new I18nLocales();
170  for(String s : context.getDefaultLocales()) {
171  locales.addConfigured(s);
172  }
173  return locales;
174  }
175 
176  private static I18nLocales constructFrom(Locale locale) {
177  I18nLocales locales = new I18nLocales();
178  for(String s : Strings.csvToList(ElephantContext.getSiteLocales())) {
179  locales.addConfigured(s);
180  }
181  locales.addPreferred(locale);
182  return locales;
183  }
184 
185  /* Global map */
186 
187  private static final Initializer<I18nMap> INIT = new Initializer<>();
188 
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));
193  return api;
194  }
195 
196  public static void resetMaps() {
197  INIT.reset();
198  I18nByKey.resetMap();
200  }
201 
202 }
abstract HttpServletRequest getHttpServletRequest()
static I18nCompatibilityMap from(I18nCompatibilityWrapper i18nCompatibilityWrapper)
static I18nCompatibilityWrapper from(I18nApiWrapper i18nApiWrapper)
static String format(String msg, Object... arguments)
Definition: I_.java:49
static I18nByKey byKeyMap()
Definition: I_.java:91
static String byKey(Locale locale, String key)
Definition: I_.java:87
static String byKey(String key)
Definition: I_.java:83
static I18nCompatibilityWrapper compatibility(Locale locale)
Definition: I_.java:113
static String keyValue(String key)
Definition: I_.java:79
static I18nMapWrapper map(Locale locale)
Definition: I_.java:73
static I18nLocales getLocales()
Definition: I_.java:103
static I18nCompatibilityMap compatibilityMap(Locale locale)
Definition: I_.java:121
static I18nLocales locales(HttpServletRequest request)
Definition: I_.java:127
static I18nApiWrapper api()
Definition: I_.java:65
static I18nCompatibilityWrapper compatibility()
Definition: I_.java:109
static I18nMapWrapper map()
Definition: I_.java:69
static void resetMaps()
Definition: I_.java:196
static String format(Locale locale, String msg, Object... arguments)
Definition: I_.java:61
static I18nCompatibilityMap compatibilityMap()
Definition: I_.java:117