Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.i18n; |
2 | ||
668268fc NR |
3 | import java.util.Locale; |
4 | import java.util.ResourceBundle; | |
296a0b75 NR |
5 | |
6 | import be.nikiroo.jvcard.tui.UiColors; | |
7 | ||
668268fc NR |
8 | import com.googlecode.lanterna.input.KeyStroke; |
9 | ||
a3b510ab NR |
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 { | |
668268fc | 18 | ResourceBundle map; |
a3b510ab NR |
19 | |
20 | /** | |
21 | * An enum representing information to be translated to the user. | |
22 | * | |
23 | * @author niki | |
24 | * | |
25 | */ | |
26 | public enum StringId { | |
bcb54330 | 27 | DUMMY, // <-- TODO : remove |
668268fc | 28 | KEY_TAB, KEY_ENTER, // keys |
bcb54330 NR |
29 | KEY_ACTION_BACK, KEY_ACTION_HELP, // MainWindow |
30 | KEY_ACTION_VIEW_CARD, // FileList | |
ae22c247 | 31 | KEY_ACTION_VIEW_CONTACT, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SAVE_CARD, KEY_ACTION_DELETE_CONTACT, KEY_ACTION_SEARCH, // ContactList |
296a0b75 | 32 | DEAULT_FIELD_SEPARATOR, DEAULT_FIELD_SEPARATOR_NOUTF, // MainContentList |
ae22c247 NR |
33 | KEY_ACTION_INVERT, KEY_ACTION_FULLSCREEN, // ContactDetails |
34 | KEY_ACTION_SWITCH_FORMAT, // multi-usage | |
bcb54330 | 35 | NULL; // Special usage |
a3b510ab NR |
36 | }; |
37 | ||
38 | /** | |
668268fc | 39 | * Create a translation service with the default language. |
a3b510ab | 40 | */ |
668268fc NR |
41 | public Trans() { |
42 | init(null); | |
43 | } | |
a3b510ab | 44 | |
668268fc NR |
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); | |
a3b510ab NR |
54 | } |
55 | ||
296a0b75 NR |
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 | */ | |
a3b510ab | 64 | public String trans(StringId stringId) { |
296a0b75 NR |
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 | ||
668268fc NR |
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()); | |
296a0b75 NR |
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 | |
668268fc | 106 | keyTrans = trans(StringId.KEY_ENTER); |
296a0b75 NR |
107 | break; |
108 | case Tab: | |
109 | if (UiColors.getInstance().isUnicode()) | |
110 | keyTrans = " ↹ "; | |
111 | else | |
668268fc | 112 | keyTrans = trans(StringId.KEY_TAB); |
296a0b75 NR |
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; | |
a3b510ab NR |
129 | } |
130 | ||
296a0b75 | 131 | return keyTrans; |
a3b510ab NR |
132 | } |
133 | ||
668268fc NR |
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()); | |
a3b510ab NR |
151 | } |
152 | } |