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