X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fi18n%2FTrans.java;h=210cffca302dbe1cfb0d7d2eaf1ae53d2c2ea80f;hb=2a96e7b2cf3f155cccc633272b2f547ffd8f4672;hp=0a06b573f5d188bcd101676584c10a945f649028;hpb=ae22c2473f7203b8713dec1c1de532c312000d1e;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/i18n/Trans.java b/src/be/nikiroo/jvcard/i18n/Trans.java index 0a06b57..210cffc 100644 --- a/src/be/nikiroo/jvcard/i18n/Trans.java +++ b/src/be/nikiroo/jvcard/i18n/Trans.java @@ -1,12 +1,13 @@ package be.nikiroo.jvcard.i18n; -import java.util.HashMap; -import java.util.Map; - -import com.googlecode.lanterna.input.KeyStroke; +import java.util.Locale; +import java.util.ResourceBundle; +import be.nikiroo.jvcard.resources.Bundles; import be.nikiroo.jvcard.tui.UiColors; +import com.googlecode.lanterna.input.KeyStroke; + /** * This class manages the translation of {@link Trans#StringId}s into * user-understandable text. @@ -15,10 +16,7 @@ import be.nikiroo.jvcard.tui.UiColors; * */ public class Trans { - static private Object lock = new Object(); - static private Trans instance = null; - - private Map map = null; + ResourceBundle map; /** * An enum representing information to be translated to the user. @@ -28,6 +26,7 @@ public class Trans { */ public enum StringId { 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 @@ -35,24 +34,24 @@ public class Trans { KEY_ACTION_INVERT, KEY_ACTION_FULLSCREEN, // ContactDetails KEY_ACTION_SWITCH_FORMAT, // multi-usage NULL; // Special usage - - public String trans() { - return Trans.getInstance().trans(this); - } }; /** - * Get the (unique) instance of this class. - * - * @return the (unique) instance + * Create a translation service with the default language. */ - static public Trans getInstance() { - synchronized (lock) { - if (instance == null) - instance = new Trans(); - } + public Trans() { + init(null); + } - return instance; + /** + * Create a translation service for the given language. (Will fall back to + * the default one i not found.) + * + * @param language + * the language to use + */ + public Trans(String language) { + init(language); } /** @@ -67,14 +66,22 @@ public class Trans { StringId id = stringId; if (!UiColors.getInstance().isUnicode()) { try { - id = StringId.valueOf(stringId.toString() + "_NOUTF"); + id = StringId.valueOf(stringId.name() + "_NOUTF"); } catch (IllegalArgumentException iae) { // no special _NOUTF version found } } - if (map.containsKey(id)) { - return map.get(id); + if (id == StringId.NULL) { + return ""; + } + + if (id == StringId.DUMMY) { + return "[dummy]"; + } + + if (map.containsKey(id.name())) { + return map.getString(id.name()); } return id.toString(); @@ -97,13 +104,13 @@ public class Trans { if (UiColors.getInstance().isUnicode()) keyTrans = " ⤶ "; else - keyTrans = "ENT"; + keyTrans = trans(StringId.KEY_ENTER); break; case Tab: if (UiColors.getInstance().isUnicode()) keyTrans = " ↹ "; else - keyTrans = "TAB"; + keyTrans = trans(StringId.KEY_TAB); break; case Character: @@ -125,24 +132,21 @@ public class Trans { return keyTrans; } - private Trans() { - map = new HashMap(); - - // TODO: get from a file instead? - map.put(StringId.NULL, ""); - map.put(StringId.DUMMY, "[dummy]"); - // we could use: " ", "┃", "│"... - map.put(StringId.DEAULT_FIELD_SEPARATOR, "┃"); - map.put(StringId.DEAULT_FIELD_SEPARATOR_NOUTF, "|"); - map.put(StringId.KEY_ACTION_BACK, "Back"); - map.put(StringId.KEY_ACTION_HELP, "Help"); - map.put(StringId.KEY_ACTION_VIEW_CONTACT, "Open"); - map.put(StringId.KEY_ACTION_VIEW_CARD, "Open"); - map.put(StringId.KEY_ACTION_EDIT_CONTACT, "Edit"); - map.put(StringId.KEY_ACTION_DELETE_CONTACT, "Delete"); - map.put(StringId.KEY_ACTION_SWITCH_FORMAT, "Change view"); - map.put(StringId.KEY_ACTION_INVERT, "Invert colours"); - map.put(StringId.KEY_ACTION_FULLSCREEN, "Fullscreen"); - map.put(StringId.KEY_ACTION_SEARCH, "Search"); + /** + * 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); + } + + map = Bundles.getBundle("resources", locale); } }