]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/LocaleUtils.java
Make time based UUID generation more robust.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / LocaleUtils.java
1 package org.argeo.cms;
2
3 import java.security.AccessController;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Locale;
7 import java.util.ResourceBundle;
8
9 import javax.security.auth.Subject;
10
11 import org.argeo.api.cms.CmsLog;
12 import org.argeo.cms.auth.CurrentUser;
13
14 /** Utilities simplifying the development of localization enums. */
15 public class LocaleUtils {
16 final static String DEFAULT_OSGI_l10N_BUNDLE = "/OSGI-INF/l10n/bundle";
17
18 private final static CmsLog log = CmsLog.getLog(LocaleUtils.class);
19
20 private final static ThreadLocal<Locale> threadLocale = new ThreadLocal<>();
21
22 public static void setThreadLocale(Locale locale) {
23 threadLocale.set(locale);
24 }
25
26 public static String local(Localized localized) {
27 return local(localized.name(), localized.getClass().getClassLoader());
28 }
29
30 public static String local(Localized localized, Locale locale) {
31 if (localized.name() == null) // untranslated
32 return localized.local(locale);
33 return local(localized.name(), locale, localized.getClass().getClassLoader());
34 }
35
36 @Deprecated
37 public static String local(Enum<?> en) {
38 return local(en, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE);
39 }
40
41 @Deprecated
42 public static String local(Enum<?> en, Locale locale) {
43 return local(en, locale, DEFAULT_OSGI_l10N_BUNDLE);
44 }
45
46 @Deprecated
47 public static String local(Enum<?> en, Locale locale, String resource) {
48 return local(en, locale, resource, en.getClass().getClassLoader());
49 }
50
51 @Deprecated
52 public static String local(Enum<?> en, Locale locale, String resource, ClassLoader classLoader) {
53 return local(en.name(), locale, resource, classLoader);
54 }
55
56 public static String local(String key, ClassLoader classLoader) {
57 return local(key, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE, classLoader);
58 }
59
60 public static String local(String key, Locale locale, ClassLoader classLoader) {
61 return local(key, locale, DEFAULT_OSGI_l10N_BUNDLE, classLoader);
62 }
63
64 /** Where the search for a message is actually performed. */
65 public static String local(String key, Locale locale, String resource, ClassLoader classLoader) {
66 ResourceBundle rb = ResourceBundle.getBundle(resource, locale, classLoader);
67 assert key.length() > 2;
68 if (isLocaleKey(key))
69 key = key.substring(1);
70 if (rb.containsKey(key))
71 return rb.getString(key);
72 else // for simple cases, the key will actually be the English word
73 return key;
74 }
75
76 public static boolean isLocaleKey(String str) {
77 if (str.length() > 2 && ('%' == str.charAt(0)))
78 return true;
79 else
80 return false;
81 }
82
83 /** Lead transformation on the translated string. */
84 public static String toLead(String raw, Locale locale) {
85 return raw.substring(0, 1).toUpperCase(locale) + raw.substring(1);
86 }
87
88 public static String lead(Localized localized, ClassLoader classLoader) {
89 Locale locale = getCurrentLocale();
90 if (localized.name() == null)// untranslated
91 return toLead(localized.local(locale), locale);
92 return toLead(local(localized.name(), getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE, classLoader), locale);
93 }
94
95 public static String lead(Localized localized) {
96 return lead(localized, localized.getL10nClassLoader());
97 }
98
99 public static String lead(Localized localized, Locale locale) {
100 return toLead(local(localized, locale), locale);
101 }
102
103 static Locale getCurrentLocale() {
104 Locale currentLocale = null;
105 if (Subject.getSubject(AccessController.getContext()) != null)
106 currentLocale = CurrentUser.locale();
107 else if (threadLocale.get() != null) {
108 currentLocale = threadLocale.get();
109 }
110 if (log.isTraceEnabled())
111 log.trace("Thread #" + Thread.currentThread().getId() + " " + Thread.currentThread().getName() + " locale: "
112 + currentLocale);
113 if (currentLocale == null)
114 throw new IllegalStateException("No locale found");
115 return currentLocale;
116 // return UiContext.getLocale();
117 // FIXME look into Subject or settings
118 // return Locale.getDefault();
119 }
120
121 /** Returns null if argument is null. */
122 public static List<Locale> asLocaleList(Object locales) {
123 if (locales == null)
124 return null;
125 ArrayList<Locale> availableLocales = new ArrayList<Locale>();
126 String[] codes = locales.toString().split(",");
127 for (int i = 0; i < codes.length; i++) {
128 String code = codes[i];
129 // variant not supported
130 int indexUnd = code.indexOf("_");
131 Locale locale;
132 if (indexUnd > 0) {
133 String language = code.substring(0, indexUnd);
134 String country = code.substring(indexUnd + 1);
135 locale = new Locale(language, country);
136 } else {
137 locale = new Locale(code);
138 }
139 availableLocales.add(locale);
140 }
141 return availableLocales;
142 }
143 }