3 import java
.security
.AccessController
;
4 import java
.util
.ArrayList
;
6 import java
.util
.Locale
;
7 import java
.util
.ResourceBundle
;
9 import javax
.security
.auth
.Subject
;
11 import org
.argeo
.api
.cms
.CmsLog
;
12 import org
.argeo
.cms
.auth
.CurrentUser
;
14 /** Utilities simplifying the development of localization enums. */
15 public class LocaleUtils
{
16 final static String DEFAULT_OSGI_l10N_BUNDLE
= "/OSGI-INF/l10n/bundle";
18 private final static CmsLog log
= CmsLog
.getLog(LocaleUtils
.class);
20 private final static ThreadLocal
<Locale
> threadLocale
= new ThreadLocal
<>();
22 public static void setThreadLocale(Locale locale
) {
23 threadLocale
.set(locale
);
26 public static String
local(Localized localized
) {
27 return local(localized
.name(), localized
.getClass().getClassLoader());
30 public static String
local(Localized localized
, Locale locale
) {
31 if (localized
.name() == null) // untranslated
32 return localized
.local(locale
);
33 return local(localized
.name(), locale
, localized
.getClass().getClassLoader());
37 public static String
local(Enum
<?
> en
) {
38 return local(en
, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
);
42 public static String
local(Enum
<?
> en
, Locale locale
) {
43 return local(en
, locale
, DEFAULT_OSGI_l10N_BUNDLE
);
47 public static String
local(Enum
<?
> en
, Locale locale
, String resource
) {
48 return local(en
, locale
, resource
, en
.getClass().getClassLoader());
52 public static String
local(Enum
<?
> en
, Locale locale
, String resource
, ClassLoader classLoader
) {
53 return local(en
.name(), locale
, resource
, classLoader
);
56 public static String
local(String key
, ClassLoader classLoader
) {
57 return local(key
, getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
, classLoader
);
60 public static String
local(String key
, Locale locale
, ClassLoader classLoader
) {
61 return local(key
, locale
, DEFAULT_OSGI_l10N_BUNDLE
, classLoader
);
64 /** Where the search for a message is actually performed. */
65 public static String
local(String key
, Locale locale
, String resource
, ClassLoader classLoader
) {
66 ResourceBundle rb
= ResourceBundle
.getBundle(resource
, locale
, classLoader
);
67 assert key
.length() > 2;
69 key
= key
.substring(1);
70 if (rb
.containsKey(key
))
71 return rb
.getString(key
);
72 else // for simple cases, the key will actually be the English word
76 public static boolean isLocaleKey(String str
) {
77 if (str
.length() > 2 && ('%' == str
.charAt(0)))
83 /** Lead transformation on the translated string. */
84 public static String
toLead(String raw
, Locale locale
) {
85 return raw
.substring(0, 1).toUpperCase(locale
) + raw
.substring(1);
88 public static String
lead(Localized localized
, ClassLoader classLoader
) {
89 Locale locale
= getCurrentLocale();
90 if (localized
.name() == null)// untranslated
91 return toLead(localized
.local(locale
), locale
);
92 return toLead(local(localized
.name(), getCurrentLocale(), DEFAULT_OSGI_l10N_BUNDLE
, classLoader
), locale
);
95 public static String
lead(Localized localized
) {
96 return lead(localized
, localized
.getL10nClassLoader());
99 public static String
lead(Localized localized
, Locale locale
) {
100 return toLead(local(localized
, locale
), locale
);
103 static Locale
getCurrentLocale() {
104 Locale currentLocale
= null;
105 if (Subject
.getSubject(AccessController
.getContext()) != null)
106 currentLocale
= CurrentUser
.locale();
107 else if (threadLocale
.get() != null) {
108 currentLocale
= threadLocale
.get();
110 if (log
.isTraceEnabled())
111 log
.trace("Thread #" + Thread
.currentThread().getId() + " " + Thread
.currentThread().getName() + " locale: "
113 if (currentLocale
== null)
114 throw new IllegalStateException("No locale found");
115 return currentLocale
;
116 // return UiContext.getLocale();
117 // FIXME look into Subject or settings
118 // return Locale.getDefault();
121 /** Returns null if argument is null. */
122 public static List
<Locale
> asLocaleList(Object locales
) {
125 ArrayList
<Locale
> availableLocales
= new ArrayList
<Locale
>();
126 String
[] codes
= locales
.toString().split(",");
127 for (int i
= 0; i
< codes
.length
; i
++) {
128 String code
= codes
[i
];
129 // variant not supported
130 int indexUnd
= code
.indexOf("_");
133 String language
= code
.substring(0, indexUnd
);
134 String country
= code
.substring(indexUnd
+ 1);
135 locale
= new Locale(language
, country
);
137 locale
= new Locale(code
);
139 availableLocales
.add(locale
);
141 return availableLocales
;