Colours are now taken from a .properties file
[jvcard.git] / src / be / nikiroo / jvcard / i18n / Trans.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard.i18n;
2
668268fc
NR
3import java.util.Locale;
4import java.util.ResourceBundle;
296a0b75 5
2a96e7b2 6import be.nikiroo.jvcard.resources.Bundles;
296a0b75
NR
7import be.nikiroo.jvcard.tui.UiColors;
8
668268fc
NR
9import com.googlecode.lanterna.input.KeyStroke;
10
a3b510ab
NR
11/**
12 * This class manages the translation of {@link Trans#StringId}s into
13 * user-understandable text.
14 *
15 * @author niki
16 *
17 */
18public class Trans {
668268fc 19 ResourceBundle map;
a3b510ab
NR
20
21 /**
22 * An enum representing information to be translated to the user.
23 *
24 * @author niki
25 *
26 */
27 public enum StringId {
bcb54330 28 DUMMY, // <-- TODO : remove
668268fc 29 KEY_TAB, KEY_ENTER, // keys
bcb54330
NR
30 KEY_ACTION_BACK, KEY_ACTION_HELP, // MainWindow
31 KEY_ACTION_VIEW_CARD, // FileList
ae22c247 32 KEY_ACTION_VIEW_CONTACT, KEY_ACTION_EDIT_CONTACT, KEY_ACTION_SAVE_CARD, KEY_ACTION_DELETE_CONTACT, KEY_ACTION_SEARCH, // ContactList
296a0b75 33 DEAULT_FIELD_SEPARATOR, DEAULT_FIELD_SEPARATOR_NOUTF, // MainContentList
ae22c247
NR
34 KEY_ACTION_INVERT, KEY_ACTION_FULLSCREEN, // ContactDetails
35 KEY_ACTION_SWITCH_FORMAT, // multi-usage
bcb54330 36 NULL; // Special usage
a3b510ab
NR
37 };
38
39 /**
668268fc 40 * Create a translation service with the default language.
a3b510ab 41 */
668268fc
NR
42 public Trans() {
43 init(null);
44 }
a3b510ab 45
668268fc
NR
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);
a3b510ab
NR
55 }
56
296a0b75
NR
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 */
a3b510ab 65 public String trans(StringId stringId) {
296a0b75
NR
66 StringId id = stringId;
67 if (!UiColors.getInstance().isUnicode()) {
68 try {
2a96e7b2 69 id = StringId.valueOf(stringId.name() + "_NOUTF");
296a0b75
NR
70 } catch (IllegalArgumentException iae) {
71 // no special _NOUTF version found
72 }
73 }
74
668268fc
NR
75 if (id == StringId.NULL) {
76 return "";
77 }
78
79 if (id == StringId.DUMMY) {
80 return "[dummy]";
81 }
82
2a96e7b2
NR
83 if (map.containsKey(id.name())) {
84 return map.getString(id.name());
296a0b75
NR
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
668268fc 107 keyTrans = trans(StringId.KEY_ENTER);
296a0b75
NR
108 break;
109 case Tab:
110 if (UiColors.getInstance().isUnicode())
111 keyTrans = " ↹ ";
112 else
668268fc 113 keyTrans = trans(StringId.KEY_TAB);
296a0b75
NR
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;
a3b510ab
NR
130 }
131
296a0b75 132 return keyTrans;
a3b510ab
NR
133 }
134
668268fc
NR
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
2a96e7b2 150 map = Bundles.getBundle("resources", locale);
a3b510ab
NR
151 }
152}