3 import java
.util
.Locale
;
4 import java
.util
.ResourceBundle
;
6 import org
.argeo
.api
.cms
.CmsLog
;
8 /** Utilities simplifying the development of localization enums. */
9 public class LocaleUtils
{
10 final static String DEFAULT_OSGI_l10N_BUNDLE
= "/OSGI-INF/l10n/bundle";
12 private final static CmsLog log
= CmsLog
.getLog(LocaleUtils
.class);
14 private final static ThreadLocal
<Locale
> threadLocale
= new ThreadLocal
<>();
16 public static void setThreadLocale(Locale locale
) {
17 threadLocale
.set(locale
);
20 public static String
local(Localized localized
) {
21 return local(localized
.name(), localized
.getClass().getClassLoader());
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());
31 public static String
local(Enum
<?
> en
) {
32 return local(en
, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
);
36 public static String
local(Enum
<?
> en
, Locale locale
) {
37 return local(en
, locale
, DEFAULT_OSGI_l10N_BUNDLE
);
41 public static String
local(Enum
<?
> en
, Locale locale
, String resource
) {
42 return local(en
, locale
, resource
, en
.getClass().getClassLoader());
46 public static String
local(Enum
<?
> en
, Locale locale
, String resource
, ClassLoader classLoader
) {
47 return local(en
.name(), locale
, resource
, classLoader
);
50 public static String
local(String key
, ClassLoader classLoader
) {
51 return local(key
, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
, classLoader
);
54 public static String
local(String key
, Locale locale
, ClassLoader classLoader
) {
55 return local(key
, locale
, DEFAULT_OSGI_l10N_BUNDLE
, classLoader
);
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);
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
) {
82 return raw
.substring(0, 1).toUpperCase(locale
) + raw
.substring(1);
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
);
92 public static String
lead(Localized localized
) {
93 return lead(localized
, localized
.getL10nClassLoader());
96 public static String
lead(Localized localized
, Locale locale
) {
97 return toLead(local(localized
, locale
), locale
);
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();
107 if (log
.isTraceEnabled())
108 log
.trace("Thread #" + Thread
.currentThread().getId() + " " + Thread
.currentThread().getName() + " locale: "
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();