| 1 | package be.nikiroo.jvcard.i18n; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Map; |
| 5 | |
| 6 | import com.googlecode.lanterna.input.KeyStroke; |
| 7 | |
| 8 | import be.nikiroo.jvcard.tui.UiColors; |
| 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 | static private Object lock = new Object(); |
| 19 | static private Trans instance = null; |
| 20 | |
| 21 | private Map<StringId, String> map = null; |
| 22 | |
| 23 | /** |
| 24 | * An enum representing information to be translated to the user. |
| 25 | * |
| 26 | * @author niki |
| 27 | * |
| 28 | */ |
| 29 | public enum StringId { |
| 30 | DUMMY, // <-- TODO : remove |
| 31 | KEY_ACTION_BACK, KEY_ACTION_HELP, // MainWindow |
| 32 | KEY_ACTION_VIEW_CARD, // FileList |
| 33 | KEY_ACTION_VIEW_CONTACT, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SAVE_CARD, KEY_ACTION_DELETE_CONTACT, KEY_ACTION_SEARCH, // ContactList |
| 34 | DEAULT_FIELD_SEPARATOR, DEAULT_FIELD_SEPARATOR_NOUTF, // MainContentList |
| 35 | KEY_ACTION_INVERT, KEY_ACTION_FULLSCREEN, // ContactDetails |
| 36 | KEY_ACTION_SWITCH_FORMAT, // multi-usage |
| 37 | NULL; // Special usage |
| 38 | |
| 39 | public String trans() { |
| 40 | return Trans.getInstance().trans(this); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * Get the (unique) instance of this class. |
| 46 | * |
| 47 | * @return the (unique) instance |
| 48 | */ |
| 49 | static public Trans getInstance() { |
| 50 | synchronized (lock) { |
| 51 | if (instance == null) |
| 52 | instance = new Trans(); |
| 53 | } |
| 54 | |
| 55 | return instance; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Translate the given {@link StringId} into user text. |
| 60 | * |
| 61 | * @param stringId |
| 62 | * the ID to translate |
| 63 | * |
| 64 | * @return the translated text |
| 65 | */ |
| 66 | public String trans(StringId stringId) { |
| 67 | StringId id = stringId; |
| 68 | if (!UiColors.getInstance().isUnicode()) { |
| 69 | try { |
| 70 | id = StringId.valueOf(stringId.toString() + "_NOUTF"); |
| 71 | } catch (IllegalArgumentException iae) { |
| 72 | // no special _NOUTF version found |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (map.containsKey(id)) { |
| 77 | return map.get(id); |
| 78 | } |
| 79 | |
| 80 | return id.toString(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Translate the given {@link KeyStroke} into a user text {@link String} of |
| 85 | * size 3. |
| 86 | * |
| 87 | * @param key |
| 88 | * the key to translate |
| 89 | * |
| 90 | * @return the translated text |
| 91 | */ |
| 92 | public String trans(KeyStroke key) { |
| 93 | String keyTrans = ""; |
| 94 | |
| 95 | switch (key.getKeyType()) { |
| 96 | case Enter: |
| 97 | if (UiColors.getInstance().isUnicode()) |
| 98 | keyTrans = " ⤶ "; |
| 99 | else |
| 100 | keyTrans = "ENT"; |
| 101 | break; |
| 102 | case Tab: |
| 103 | if (UiColors.getInstance().isUnicode()) |
| 104 | keyTrans = " ↹ "; |
| 105 | else |
| 106 | keyTrans = "TAB"; |
| 107 | |
| 108 | break; |
| 109 | case Character: |
| 110 | keyTrans = " " + key.getCharacter() + " "; |
| 111 | break; |
| 112 | default: |
| 113 | keyTrans = "" + key.getKeyType(); |
| 114 | int width = 3; |
| 115 | if (keyTrans.length() > width) { |
| 116 | keyTrans = keyTrans.substring(0, width); |
| 117 | } else if (keyTrans.length() < width) { |
| 118 | keyTrans = keyTrans |
| 119 | + new String(new char[width - keyTrans.length()]) |
| 120 | .replace('\0', ' '); |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | return keyTrans; |
| 126 | } |
| 127 | |
| 128 | private Trans() { |
| 129 | map = new HashMap<StringId, String>(); |
| 130 | |
| 131 | // TODO: get from a file instead? |
| 132 | map.put(StringId.NULL, ""); |
| 133 | map.put(StringId.DUMMY, "[dummy]"); |
| 134 | // we could use: " ", "┃", "│"... |
| 135 | map.put(StringId.DEAULT_FIELD_SEPARATOR, "┃"); |
| 136 | map.put(StringId.DEAULT_FIELD_SEPARATOR_NOUTF, "|"); |
| 137 | map.put(StringId.KEY_ACTION_BACK, "Back"); |
| 138 | map.put(StringId.KEY_ACTION_HELP, "Help"); |
| 139 | map.put(StringId.KEY_ACTION_VIEW_CONTACT, "Open"); |
| 140 | map.put(StringId.KEY_ACTION_VIEW_CARD, "Open"); |
| 141 | map.put(StringId.KEY_ACTION_EDIT_CONTACT, "Edit"); |
| 142 | map.put(StringId.KEY_ACTION_DELETE_CONTACT, "Delete"); |
| 143 | map.put(StringId.KEY_ACTION_SWITCH_FORMAT, "Change view"); |
| 144 | map.put(StringId.KEY_ACTION_INVERT, "Invert colours"); |
| 145 | map.put(StringId.KEY_ACTION_FULLSCREEN, "Fullscreen"); |
| 146 | map.put(StringId.KEY_ACTION_SEARCH, "Search"); |
| 147 | } |
| 148 | } |