X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fi18n%2FLocaleUtils.java;h=6253e585beeb50b25420f967157fc6d0e92718f9;hb=972528f4de2d00690362c01d3ce843ca9cd10250;hp=e422e21fea8212608520fd7d935d4288522d973a;hpb=c873a0359345503b8e3ca07828bd99d525ec7cc0;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/i18n/LocaleUtils.java b/org.argeo.cms/src/org/argeo/cms/i18n/LocaleUtils.java index e422e21fe..6253e585b 100644 --- a/org.argeo.cms/src/org/argeo/cms/i18n/LocaleUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/i18n/LocaleUtils.java @@ -1,5 +1,7 @@ package org.argeo.cms.i18n; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.ResourceBundle; @@ -41,110 +43,27 @@ public class LocaleUtils { static Locale getCurrentLocale() { return UiContext.getLocale(); } - // private String id; - // private ClassLoader classLoader; - // private final Object defaultLocal; - // - // public Msg() { - // defaultLocal = null; - // } - // - // public Msg(Object defaultMessage) { - // this.defaultLocal = defaultMessage; - // } - // - // public String getId() { - // return id; - // } - // - // public void setId(String id) { - // this.id = id; - // } - // - // public ClassLoader getClassLoader() { - // return classLoader; - // } - // - // public void setClassLoader(ClassLoader classLoader) { - // this.classLoader = classLoader; - // } - // - // public Object getDefault() { - // return defaultLocal; - // } - // - // public String toString() { - // return local().toString(); - // } - // - // /** When used as the first word of a sentence. */ - // public String lead() { - // return lead(UiContext.getLocale()); - // } - // - // public String lead(Locale locale) { - // return lead(this, locale); - // } - // 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 asLocaleList(Object locales) { + if (locales == null) + return null; + ArrayList availableLocales = new ArrayList(); + 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; + } }