i18n: rework of the system + French translation
[jvcard.git] / src / be / nikiroo / jvcard / i18n / Trans.java
index 8c744d38f2123ccf08eeb309d6837d69319133d7..1046d4cdbc982c06f061f0e36830a93305de8ff2 100644 (file)
@@ -1,7 +1,11 @@
 package be.nikiroo.jvcard.i18n;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import be.nikiroo.jvcard.tui.UiColors;
+
+import com.googlecode.lanterna.input.KeyStroke;
 
 /**
  * This class manages the translation of {@link Trans#StringId}s into
@@ -11,10 +15,7 @@ import java.util.Map;
  * 
  */
 public class Trans {
-       static private Object lock = new Object();
-       static private Trans instance = null;
-
-       private Map<StringId, String> map = null;
+       ResourceBundle map;
 
        /**
         * An enum representing information to be translated to the user.
@@ -23,44 +24,129 @@ public class Trans {
         * 
         */
        public enum StringId {
-               KEY_ACTION_BACK, KEY_ACTION_HELP, KEY_ACTION_VIEW_CONTACT, KEY_ACTION_VIEW_CARD, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SWITCH_FORMAT, TITLE, NULL;
-
-               public String trans() {
-                       return Trans.getInstance().trans(this);
-               }
+               DUMMY, // <-- TODO : remove
+               KEY_TAB, KEY_ENTER, // keys
+               KEY_ACTION_BACK, KEY_ACTION_HELP, // MainWindow
+               KEY_ACTION_VIEW_CARD, // FileList
+               KEY_ACTION_VIEW_CONTACT, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SAVE_CARD, KEY_ACTION_DELETE_CONTACT, KEY_ACTION_SEARCH, // ContactList
+               DEAULT_FIELD_SEPARATOR, DEAULT_FIELD_SEPARATOR_NOUTF, // MainContentList
+               KEY_ACTION_INVERT, KEY_ACTION_FULLSCREEN, // ContactDetails
+               KEY_ACTION_SWITCH_FORMAT, // multi-usage
+               NULL; // Special usage
        };
 
        /**
-        * Get the (unique) instance of this class.
+        * Create a translation service with the default language.
+        */
+       public Trans() {
+               init(null);
+       }
+
+       /**
+        * Create a translation service for the given language. (Will fall back to
+        * the default one i not found.)
         * 
-        * @return the (unique) instance
+        * @param language
+        *            the language to use
         */
-       static public Trans getInstance() {
-               synchronized (lock) {
-                       if (instance == null)
-                               instance = new Trans();
+       public Trans(String language) {
+               init(language);
+       }
+
+       /**
+        * Translate the given {@link StringId} into user text.
+        * 
+        * @param stringId
+        *            the ID to translate
+        * 
+        * @return the translated text
+        */
+       public String trans(StringId stringId) {
+               StringId id = stringId;
+               if (!UiColors.getInstance().isUnicode()) {
+                       try {
+                               id = StringId.valueOf(stringId.toString() + "_NOUTF");
+                       } catch (IllegalArgumentException iae) {
+                               // no special _NOUTF version found
+                       }
+               }
+
+               if (id == StringId.NULL) {
+                       return "";
+               }
+
+               if (id == StringId.DUMMY) {
+                       return "[dummy]";
+               }
+
+               if (map.containsKey(id.toString())) {
+                       return map.getString(id.toString());
                }
 
-               return instance;
+               return id.toString();
        }
 
-       public String trans(StringId stringId) {
-               if (map.containsKey(stringId)) {
-                       return map.get(stringId);
+       /**
+        * Translate the given {@link KeyStroke} into a user text {@link String} of
+        * size 3.
+        * 
+        * @param key
+        *            the key to translate
+        * 
+        * @return the translated text
+        */
+       public String trans(KeyStroke key) {
+               String keyTrans = "";
+
+               switch (key.getKeyType()) {
+               case Enter:
+                       if (UiColors.getInstance().isUnicode())
+                               keyTrans = " ⤶ ";
+                       else
+                               keyTrans = trans(StringId.KEY_ENTER);
+                       break;
+               case Tab:
+                       if (UiColors.getInstance().isUnicode())
+                               keyTrans = " ↹ ";
+                       else
+                               keyTrans = trans(StringId.KEY_TAB);
+
+                       break;
+               case Character:
+                       keyTrans = " " + key.getCharacter() + " ";
+                       break;
+               default:
+                       keyTrans = "" + key.getKeyType();
+                       int width = 3;
+                       if (keyTrans.length() > width) {
+                               keyTrans = keyTrans.substring(0, width);
+                       } else if (keyTrans.length() < width) {
+                               keyTrans = keyTrans
+                                               + new String(new char[width - keyTrans.length()])
+                                                               .replace('\0', ' ');
+                       }
+                       break;
                }
 
-               return stringId.toString();
+               return keyTrans;
        }
 
-       private Trans() {
-               map = new HashMap<StringId, String>();
+       /**
+        * Initialise the translation mappings for the given language.
+        * 
+        * @param lang
+        *            the language to initialise
+        */
+       private void init(String lang) {
+               Locale locale = null;
+
+               if (lang == null) {
+                       locale = Locale.getDefault();
+               } else {
+                       locale = Locale.forLanguageTag(lang);
+               }
 
-               // TODO: get from a file instead?
-               map.put(StringId.NULL, "");
-               map.put(StringId.KEY_ACTION_BACK, "Back");
-               map.put(StringId.TITLE, "[ jVcard: version 0.9 ]");
-               map.put(StringId.KEY_ACTION_VIEW_CONTACT, "view");
-               map.put(StringId.KEY_ACTION_EDIT_CONTACT, "edit");
-               map.put(StringId.KEY_ACTION_SWITCH_FORMAT, "Change view");      
+               map = ResourceBundle.getBundle(Trans.class.getPackage().getName()
+                               + ".resources", locale, new FixedResourceBundleControl());
        }
 }