Translation: update system to support arguments, add some translations
authorNiki Roo <niki@nikiroo.be>
Sat, 26 Mar 2016 06:21:24 +0000 (07:21 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 26 Mar 2016 06:21:24 +0000 (07:21 +0100)
src/be/nikiroo/jvcard/launcher/Main.java
src/be/nikiroo/jvcard/resources/Trans.java
src/be/nikiroo/jvcard/resources/resources.properties
src/be/nikiroo/jvcard/resources/resources_en.properties
src/be/nikiroo/jvcard/resources/resources_fr.properties
src/be/nikiroo/jvcard/tui/panes/ContactList.java

index 178dff106d11394fa0b08bf7fb754134caf5a52f..1a77291c28903120079f430661ca181f7ac4a6dd 100644 (file)
@@ -36,15 +36,18 @@ public class Main {
        static private Trans transService;
 
        /**
-        * Translate the given {@link StringId}.
+        * Translate the given {@link StringId} into user text.
         * 
-        * @param id
+        * @param stringId
         *            the ID to translate
+        * @param values
+        *            the values to insert instead of the place holders in the
+        *            translation
         * 
-        * @return the translation
+        * @return the translated text with the given value where required
         */
-       static public String trans(StringId id) {
-               return transService.trans(id);
+       static public String trans(StringId id, String... values) {
+               return transService.trans(id, (String[]) values);
        }
 
        /**
@@ -160,14 +163,14 @@ public class Main {
                                        System.exit(ERR_SYNTAX);
                                        return;
                                }
-                               
+
                                i18nDir = args[index];
                        } else {
                                filesTried = true;
                                files.addAll(open(arg));
                        }
                }
-               
+
                // Force headless mode if we run in forced-text mode
                if (textMode != null && textMode) {
                        // same as -Djava.awt.headless=true
index 537b3a3cfb38877363a0159835eeb501001d2ad9..8dce6f1f9eb575744a552280864f7d1f1fd5efb0 100644 (file)
@@ -19,6 +19,7 @@ import java.util.ResourceBundle;
 public class Trans {
        private ResourceBundle map;
        private boolean utf = true;
+       private Locale locale;
 
        /**
         * Create a translation service with the default language.
@@ -43,11 +44,16 @@ public class Trans {
         * 
         * @param stringId
         *            the ID to translate
+        * @param values
+        *            the values to insert instead of the place holders in the
+        *            translation
         * 
-        * @return the translated text
+        * @return the translated text with the given value where required
         */
-       public String trans(StringId stringId) {
+       public String trans(StringId stringId, String... values) {
                StringId id = stringId;
+               String result = null;
+
                if (!isUnicode()) {
                        try {
                                id = StringId.valueOf(stringId.name() + "_NOUTF");
@@ -57,18 +63,19 @@ public class Trans {
                }
 
                if (id == StringId.NULL) {
-                       return "";
-               }
-
-               if (id == StringId.DUMMY) {
-                       return "[dummy]";
-               }
-
-               if (map.containsKey(id.name())) {
-                       return map.getString(id.name());
+                       result = "";
+               } else if (id == StringId.DUMMY) {
+                       result = "[dummy]";
+               } else if (map.containsKey(id.name())) {
+                       result = map.getString(id.name());
+               } else {
+                       result = id.toString();
                }
 
-               return id.toString();
+               if (values != null && values.length > 0)
+                       return String.format(locale, result, (Object[]) values);
+               else
+                       return result;
        }
 
        /**
@@ -98,7 +105,8 @@ public class Trans {
         *            instance
         */
        private void setLanguage(String language) {
-               map = Bundles.getBundle("resources", getLocaleFor(language));
+               locale = getLocaleFor(language);
+               map = Bundles.getBundle("resources", locale);
        }
 
        /**
@@ -246,7 +254,10 @@ public class Trans {
                                builder.append("FORMAT: " + format);
                        }
 
-                       builder.append(")\n# ");
+                       builder.append(")");
+                       if (info.length() > 0) {
+                               builder.append("\n# ");
+                       }
                }
 
                builder.append(info);
@@ -298,5 +309,13 @@ public class Trans {
                KEY_ACTION_FULLSCREEN, //
                @Meta(what = "", where = "", format = "", info = "")
                KEY_ACTION_SWITCH_FORMAT, // multi-usage
+               @Meta(what = "Action key", where = "Contact list, Edit Contact", format = "", info = "Add a new contact/field")
+               KEY_ACTION_ADD, //
+               @Meta(what = "User question: TEXT", where = "Contact list", format = "", info = "New contact")
+               ASK_USER_CONTACT_NAME, //
+               @Meta(what = "User question: [Y|N]", where = "Contact list", format = "%s = contact name", info = "Delete contact")
+               CONFIRM_USER_DELETE_CONTACT, //
+               @Meta(what = "Error", where = "Contact list", format = "%s = contact name", info = "cannot delete a contact")
+               ERR_CANNOT_DELETE_CONTACT, //
        };
 }
index c35e0790ad251e22b14881d6e6f32f7778dd5b71..ac55df23e217142fc7955555e29342eee8b4c080 100644 (file)
@@ -26,3 +26,15 @@ DEAULT_FIELD_SEPARATOR_NOUTF = |
 KEY_ACTION_INVERT = Invert colours
 KEY_ACTION_FULLSCREEN = Fullscreen
 KEY_ACTION_SWITCH_FORMAT = Change view
+# (WHAT: Action key, WHERE: Contact list, Edit Contact)
+# Add a new contact/field
+KEY_ACTION_ADD = Add
+# (WHAT: User question: TEXT, WHERE: Contact list)
+# New contact
+ASK_USER_CONTACT_NAME = New contact name:
+# (WHAT: User question: [Y|N], WHERE: Contact list, FORMAT: %s = contact name)
+# Delete contact
+CONFIRM_USER_DELETE_CONTACT = Delete "%s"? [Y/N]
+# (WHAT: Error, WHERE: Contact list, FORMAT: %s = contact name)
+# cannot delete a contact
+ERR_CANNOT_DELETE_CONTACT = Cannot delete "%s"
index eb08a134f890dd00e18c4280008e837fca015c05..a2e41f23bf61f70ace6e0983f21847040f590763 100644 (file)
@@ -26,3 +26,15 @@ DEAULT_FIELD_SEPARATOR_NOUTF = |
 KEY_ACTION_INVERT = Invert colours
 KEY_ACTION_FULLSCREEN = Fullscreen
 KEY_ACTION_SWITCH_FORMAT = Change view
+# (WHAT: Action key, WHERE: Contact list, Edit Contact)
+# Add a new contact/field
+KEY_ACTION_ADD = Add
+# (WHAT: User question: TEXT, WHERE: Contact list)
+# New contact
+ASK_USER_CONTACT_NAME = New contact name:
+# (WHAT: User question: [Y|N], WHERE: Contact list, FORMAT: %s = contact name)
+# Delete contact
+CONFIRM_USER_DELETE_CONTACT = Delete "%s"? [Y/N]
+# (WHAT: Error, WHERE: Contact list, FORMAT: %s = contact name)
+# cannot delete a contact
+ERR_CANNOT_DELETE_CONTACT = Cannot delete "%s"
index 9f3841c086542901c500b5a2dc71b66e65e9014f..d7fca13fc5a52aaf915ce7249aad9d15d68f7394 100644 (file)
@@ -26,3 +26,15 @@ DEAULT_FIELD_SEPARATOR_NOUTF = |
 KEY_ACTION_INVERT = Couleurs inversées
 KEY_ACTION_FULLSCREEN = Plein écran
 KEY_ACTION_SWITCH_FORMAT = Autre affichage
+# (WHAT: Action key, WHERE: Contact list, Edit Contact)
+# Add a new contact/field
+KEY_ACTION_ADD = Ajouter
+# (WHAT: User question: TEXT, WHERE: Contact list)
+# New contact
+ASK_USER_CONTACT_NAME = Nom du nouveau contact:
+# (WHAT: User question: [Y|N], WHERE: Contact list, FORMAT: %s = contact name)
+# Delete contact
+CONFIRM_USER_DELETE_CONTACT = Supprimer "%s"? [Y/N]
+# (WHAT: Error, WHERE: Contact list, FORMAT: %s = contact name)
+# cannot delete a contact
+ERR_CANNOT_DELETE_CONTACT = Impossible de supprimer "%s"
index 73c6a84c65e8de094dbdb2afffd8b01277180986..a68efa249c6c502d4f6b071ef21d35b870ee00ff 100644 (file)
@@ -92,7 +92,8 @@ public class ContactList extends MainContentList {
                List<KeyAction> actions = new LinkedList<KeyAction>();
 
                // TODO ui
-               actions.add(new KeyAction(Mode.ASK_USER, 'a', Trans.StringId.DUMMY) {
+               actions.add(new KeyAction(Mode.ASK_USER, 'a',
+                               Trans.StringId.KEY_ACTION_ADD) {
                        @Override
                        public Object getObject() {
                                return card;
@@ -100,8 +101,7 @@ public class ContactList extends MainContentList {
 
                        @Override
                        public String getQuestion() {
-                               // TODO i18n
-                               return "new contact name: ";
+                               return Main.trans(Trans.StringId.ASK_USER_CONTACT_NAME);
                        }
 
                        @Override
@@ -125,8 +125,12 @@ public class ContactList extends MainContentList {
 
                        @Override
                        public String getQuestion() {
-                               // TODO i18n
-                               return "Delete contact? [Y/N]";
+                               Contact contact = getSelectedContact();
+                               String contactName = "null";
+                               if (contact != null)
+                                       contactName = "" + contact.getPreferredDataValue("FN");
+
+                               return Main.trans(Trans.StringId.CONFIRM_USER_DELETE_CONTACT, contactName);
                        }
 
                        @Override
@@ -138,8 +142,12 @@ public class ContactList extends MainContentList {
                                                return null;
                                        }
 
-                                       // TODO i18n
-                                       return "Cannot delete contact";
+                                       String contactName = "null";
+                                       if (contact != null)
+                                               contactName = "" + contact.getPreferredDataValue("FN");
+
+                                       return Main.trans(Trans.StringId.ERR_CANNOT_DELETE_CONTACT,
+                                                       contactName);
                                }
 
                                return null;