]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/i18n/Msg.java
Remove unused login entry point
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / i18n / Msg.java
index dd38c7483563b3cd551cc69ccc25c49802565e0e..bbbd590cd97356f6365edad5962e261d3ad6d7b1 100644 (file)
@@ -2,10 +2,11 @@ package org.argeo.cms.i18n;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
+import java.util.Locale;
+import java.util.ResourceBundle;
 
 import org.argeo.cms.CmsException;
-import org.argeo.cms.CmsSession;
-import org.eclipse.rap.rwt.RWT;
+import org.argeo.eclipse.ui.specific.UiContext;
 
 /** A single message to be internationalised. */
 public class Msg {
@@ -47,14 +48,24 @@ public class Msg {
 
        /** When used as the first word of a sentence. */
        public String lead() {
-               String raw = toString();
-               return raw.substring(0, 1).toUpperCase(RWT.getLocale())
-                               + raw.substring(1);
+               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 static String lead(String raw, Locale locale) {
+               return raw.substring(0, 1).toUpperCase(locale) + raw.substring(1);
        }
 
        public Object local() {
-               CmsSession cmSession = CmsSession.current.get();
-               Object local = cmSession.local(this);
+               Object local = local(this);
                if (local == null)
                        local = getDefault();
                if (local == null)
@@ -62,6 +73,30 @@ public class Msg {
                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);
+       }
+
+       private 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(className, locale,
+                               msg.getClassLoader());
+               return rb.getString(fieldName);
+       }
+
        public static void init(Class<?> clss) {
                final Field[] fieldArray = clss.getDeclaredFields();
                ClassLoader loader = clss.getClassLoader();