3 import java
.util
.Locale
;
4 import java
.util
.ResourceBundle
;
6 import org
.argeo
.api
.cms
.CmsLog
;
7 import org
.argeo
.cms
.auth
.CurrentUser
;
9 /** Utilities simplifying the development of localization enums. */
10 public class LocaleUtils
{
11 final static String DEFAULT_OSGI_l10N_BUNDLE
= "/OSGI-INF/l10n/bundle";
13 private final static CmsLog log
= CmsLog
.getLog(LocaleUtils
.class);
15 private final static ThreadLocal
<Locale
> threadLocale
= new ThreadLocal
<>();
17 public static void setThreadLocale(Locale locale
) {
18 threadLocale
.set(locale
);
21 public static String
local(Localized localized
) {
22 return local(localized
.name(), localized
.getClass().getClassLoader());
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());
32 public static String
local(Enum
<?
> en
) {
33 return local(en
, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
);
37 public static String
local(Enum
<?
> en
, Locale locale
) {
38 return local(en
, locale
, DEFAULT_OSGI_l10N_BUNDLE
);
42 public static String
local(Enum
<?
> en
, Locale locale
, String resource
) {
43 return local(en
, locale
, resource
, en
.getClass().getClassLoader());
47 public static String
local(Enum
<?
> en
, Locale locale
, String resource
, ClassLoader classLoader
) {
48 return local(en
.name(), locale
, resource
, classLoader
);
51 public static String
local(String key
, ClassLoader classLoader
) {
52 return local(key
, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
, classLoader
);
55 public static String
local(String key
, Locale locale
, ClassLoader classLoader
) {
56 return local(key
, locale
, DEFAULT_OSGI_l10N_BUNDLE
, classLoader
);
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;
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
71 public static boolean isLocaleKey(String str
) {
72 if (str
.length() > 2 && ('%' == str
.charAt(0)))
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);
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
);
90 public static String
lead(Localized localized
) {
91 return lead(localized
, localized
.getL10nClassLoader());
94 public static String
lead(Localized localized
, Locale locale
) {
95 return toLead(local(localized
, locale
), locale
);
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();
105 if (log
.isTraceEnabled())
106 log
.trace("Thread #" + Thread
.currentThread().getId() + " " + Thread
.currentThread().getName() + " locale: "
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();