| 1 | package be.nikiroo.jvcard.i18n; |
| 2 | |
| 3 | import java.util.Locale; |
| 4 | import java.util.ResourceBundle; |
| 5 | |
| 6 | import be.nikiroo.jvcard.tui.UiColors; |
| 7 | |
| 8 | import com.googlecode.lanterna.input.KeyStroke; |
| 9 | |
| 10 | /** |
| 11 | * This class manages the translation of {@link Trans#StringId}s into |
| 12 | * user-understandable text. |
| 13 | * |
| 14 | * @author niki |
| 15 | * |
| 16 | */ |
| 17 | public class Trans { |
| 18 | ResourceBundle map; |
| 19 | |
| 20 | /** |
| 21 | * An enum representing information to be translated to the user. |
| 22 | * |
| 23 | * @author niki |
| 24 | * |
| 25 | */ |
| 26 | public enum StringId { |
| 27 | DUMMY, // <-- TODO : remove |
| 28 | KEY_TAB, KEY_ENTER, // keys |
| 29 | KEY_ACTION_BACK, KEY_ACTION_HELP, // MainWindow |
| 30 | KEY_ACTION_VIEW_CARD, // FileList |
| 31 | KEY_ACTION_VIEW_CONTACT, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SAVE_CARD, KEY_ACTION_DELETE_CONTACT, KEY_ACTION_SEARCH, // ContactList |
| 32 | DEAULT_FIELD_SEPARATOR, DEAULT_FIELD_SEPARATOR_NOUTF, // MainContentList |
| 33 | KEY_ACTION_INVERT, KEY_ACTION_FULLSCREEN, // ContactDetails |
| 34 | KEY_ACTION_SWITCH_FORMAT, // multi-usage |
| 35 | NULL; // Special usage |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Create a translation service with the default language. |
| 40 | */ |
| 41 | public Trans() { |
| 42 | init(null); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create a translation service for the given language. (Will fall back to |
| 47 | * the default one i not found.) |
| 48 | * |
| 49 | * @param language |
| 50 | * the language to use |
| 51 | */ |
| 52 | public Trans(String language) { |
| 53 | init(language); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Translate the given {@link StringId} into user text. |
| 58 | * |
| 59 | * @param stringId |
| 60 | * the ID to translate |
| 61 | * |
| 62 | * @return the translated text |
| 63 | */ |
| 64 | public String trans(StringId stringId) { |
| 65 | StringId id = stringId; |
| 66 | if (!UiColors.getInstance().isUnicode()) { |
| 67 | try { |
| 68 | id = StringId.valueOf(stringId.toString() + "_NOUTF"); |
| 69 | } catch (IllegalArgumentException iae) { |
| 70 | // no special _NOUTF version found |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (id == StringId.NULL) { |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | if (id == StringId.DUMMY) { |
| 79 | return "[dummy]"; |
| 80 | } |
| 81 | |
| 82 | if (map.containsKey(id.toString())) { |
| 83 | return map.getString(id.toString()); |
| 84 | } |
| 85 | |
| 86 | return id.toString(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Translate the given {@link KeyStroke} into a user text {@link String} of |
| 91 | * size 3. |
| 92 | * |
| 93 | * @param key |
| 94 | * the key to translate |
| 95 | * |
| 96 | * @return the translated text |
| 97 | */ |
| 98 | public String trans(KeyStroke key) { |
| 99 | String keyTrans = ""; |
| 100 | |
| 101 | switch (key.getKeyType()) { |
| 102 | case Enter: |
| 103 | if (UiColors.getInstance().isUnicode()) |
| 104 | keyTrans = " ⤶ "; |
| 105 | else |
| 106 | keyTrans = trans(StringId.KEY_ENTER); |
| 107 | break; |
| 108 | case Tab: |
| 109 | if (UiColors.getInstance().isUnicode()) |
| 110 | keyTrans = " ↹ "; |
| 111 | else |
| 112 | keyTrans = trans(StringId.KEY_TAB); |
| 113 | |
| 114 | break; |
| 115 | case Character: |
| 116 | keyTrans = " " + key.getCharacter() + " "; |
| 117 | break; |
| 118 | default: |
| 119 | keyTrans = "" + key.getKeyType(); |
| 120 | int width = 3; |
| 121 | if (keyTrans.length() > width) { |
| 122 | keyTrans = keyTrans.substring(0, width); |
| 123 | } else if (keyTrans.length() < width) { |
| 124 | keyTrans = keyTrans |
| 125 | + new String(new char[width - keyTrans.length()]) |
| 126 | .replace('\0', ' '); |
| 127 | } |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | return keyTrans; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Initialise the translation mappings for the given language. |
| 136 | * |
| 137 | * @param lang |
| 138 | * the language to initialise |
| 139 | */ |
| 140 | private void init(String lang) { |
| 141 | Locale locale = null; |
| 142 | |
| 143 | if (lang == null) { |
| 144 | locale = Locale.getDefault(); |
| 145 | } else { |
| 146 | locale = Locale.forLanguageTag(lang); |
| 147 | } |
| 148 | |
| 149 | map = ResourceBundle.getBundle(Trans.class.getPackage().getName() |
| 150 | + ".resources", locale, new FixedResourceBundleControl()); |
| 151 | } |
| 152 | } |