1 package be
.nikiroo
.jvcard
.i18n
;
3 import java
.util
.Locale
;
4 import java
.util
.ResourceBundle
;
6 import be
.nikiroo
.jvcard
.tui
.UiColors
;
8 import com
.googlecode
.lanterna
.input
.KeyStroke
;
11 * This class manages the translation of {@link Trans#StringId}s into
12 * user-understandable text.
21 * An enum representing information to be translated to the user.
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
39 * Create a translation service with the default language.
46 * Create a translation service for the given language. (Will fall back to
47 * the default one i not found.)
52 public Trans(String language
) {
57 * Translate the given {@link StringId} into user text.
62 * @return the translated text
64 public String
trans(StringId stringId
) {
65 StringId id
= stringId
;
66 if (!UiColors
.getInstance().isUnicode()) {
68 id
= StringId
.valueOf(stringId
.toString() + "_NOUTF");
69 } catch (IllegalArgumentException iae
) {
70 // no special _NOUTF version found
74 if (id
== StringId
.NULL
) {
78 if (id
== StringId
.DUMMY
) {
82 if (map
.containsKey(id
.toString())) {
83 return map
.getString(id
.toString());
90 * Translate the given {@link KeyStroke} into a user text {@link String} of
94 * the key to translate
96 * @return the translated text
98 public String
trans(KeyStroke key
) {
101 switch (key
.getKeyType()) {
103 if (UiColors
.getInstance().isUnicode())
106 keyTrans
= trans(StringId
.KEY_ENTER
);
109 if (UiColors
.getInstance().isUnicode())
112 keyTrans
= trans(StringId
.KEY_TAB
);
116 keyTrans
= " " + key
.getCharacter() + " ";
119 keyTrans
= "" + key
.getKeyType();
121 if (keyTrans
.length() > width
) {
122 keyTrans
= keyTrans
.substring(0, width
);
123 } else if (keyTrans
.length() < width
) {
125 + new String(new char[width
- keyTrans
.length()])
135 * Initialise the translation mappings for the given language.
138 * the language to initialise
140 private void init(String lang
) {
141 Locale locale
= null;
144 locale
= Locale
.getDefault();
146 locale
= Locale
.forLanguageTag(lang
);
149 map
= ResourceBundle
.getBundle(Trans
.class.getPackage().getName()
150 + ".resources", locale
, new FixedResourceBundleControl());