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