]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/LocaleUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 assert key.length() > 2;
63 if (isLocaleKey(key))
64 key = key.substring(1);
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 return raw.substring(0, 1).toUpperCase(locale) + raw.substring(1);
81 }
82
83 public static String lead(Localized localized, ClassLoader classLoader) {
84 Locale locale = getCurrentLocale();
85 if (localized.name() == null)// untranslated
86 return toLead(localized.local(locale), locale);
87 return toLead(local(localized.name(), getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE, classLoader), locale);
88 }
89
90 public static String lead(Localized localized) {
91 return lead(localized, localized.getL10nClassLoader());
92 }
93
94 public static String lead(Localized localized, Locale locale) {
95 return toLead(local(localized, locale), locale);
96 }
97
98 static Locale getCurrentLocale() {
99 Locale currentLocale = null;
100 if (CurrentUser.isAvailable())
101 currentLocale = CurrentUser.locale();
102 else if (threadLocale.get() != null) {
103 currentLocale = threadLocale.get();
104 }
105 if (log.isTraceEnabled())
106 log.trace("Thread #" + Thread.currentThread().getId() + " " + Thread.currentThread().getName() + " locale: "
107 + currentLocale);
108 if (currentLocale == null)
109 throw new IllegalStateException("No locale found");
110 return currentLocale;
111 // return UiContext.getLocale();
112 // FIXME look into Subject or settings
113 // return Locale.getDefault();
114 }
115
116 }