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