- // private static String lead(Msg msg, Locale locale) {
- // String raw = msg.local(locale).toString();
- // return lead(raw, locale);
- // }
-
- // public Object local() {
- // Object local = local(this);
- // if (local == null)
- // local = getDefault();
- // if (local == null)
- // throw new CmsException("No translation found for " + id);
- // return local;
- // }
- //
- // public Object local(Locale locale) {
- // Object local = local(this, locale);
- // if (local == null)
- // local = getDefault();
- // if (local == null)
- // throw new CmsException("No translation found for " + id);
- // return local;
- // }
- //
- // private static Object local(Msg msg) {
- // Locale locale = UiContext.getLocale();
- // return local(msg, locale);
- // }
- //
- // public static Object local(Msg msg, Locale locale) {
- // String key = msg.getId();
- // int lastDot = key.lastIndexOf('.');
- // String className = key.substring(0, lastDot);
- // String fieldName = key.substring(lastDot + 1);
- // ResourceBundle rb = ResourceBundle.getBundle("/OSGI-INF/l10n/bundle",
- // locale, msg.getClassLoader());
- // // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
- // // msg.getClassLoader());
- // return rb.getString(fieldName);
- // }
-
- // public static void init(Class<?> clss) {
- // final Field[] fieldArray = clss.getDeclaredFields();
- // ClassLoader loader = clss.getClassLoader();
- //
- // for (Field field : fieldArray) {
- // if (Modifier.isStatic(field.getModifiers())
- // && field.getType().isAssignableFrom(Msg.class)) {
- // try {
- // Object obj = field.get(null);
- // String id = clss.getCanonicalName() + "." + field.getName();
- // obj.getClass().getMethod("setId", String.class)
- // .invoke(obj, id);
- // obj.getClass()
- // .getMethod("setClassLoader", ClassLoader.class)
- // .invoke(obj, loader);
- // } catch (Exception e) {
- // throw new CmsException("Cannot prepare field " + field);
- // }
- // }
- // }
- // }
+ /** Returns null if argument is null. */
+ public static List<Locale> asLocaleList(Object locales) {
+ if (locales == null)
+ return null;
+ ArrayList<Locale> availableLocales = new ArrayList<Locale>();
+ String[] codes = locales.toString().split(",");
+ for (int i = 0; i < codes.length; i++) {
+ String code = codes[i];
+ // variant not supported
+ int indexUnd = code.indexOf("_");
+ Locale locale;
+ if (indexUnd > 0) {
+ String language = code.substring(0, indexUnd);
+ String country = code.substring(indexUnd + 1);
+ locale = new Locale(language, country);
+ } else {
+ locale = new Locale(code);
+ }
+ availableLocales.add(locale);
+ }
+ return availableLocales;
+ }