1 package be
.nikiroo
.jvcard
.i18n
;
3 import java
.util
.Locale
;
4 import java
.util
.ResourceBundle
;
6 import be
.nikiroo
.jvcard
.resources
.Bundles
;
7 import be
.nikiroo
.jvcard
.tui
.UiColors
;
9 import com
.googlecode
.lanterna
.input
.KeyStroke
;
12 * This class manages the translation of {@link Trans#StringId}s into
13 * user-understandable text.
22 * An enum representing information to be translated to the user.
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
40 * Create a translation service with the default language.
47 * Create a translation service for the given language. (Will fall back to
48 * the default one i not found.)
53 public Trans(String language
) {
58 * Translate the given {@link StringId} into user text.
63 * @return the translated text
65 public String
trans(StringId stringId
) {
66 StringId id
= stringId
;
67 if (!UiColors
.getInstance().isUnicode()) {
69 id
= StringId
.valueOf(stringId
.name() + "_NOUTF");
70 } catch (IllegalArgumentException iae
) {
71 // no special _NOUTF version found
75 if (id
== StringId
.NULL
) {
79 if (id
== StringId
.DUMMY
) {
83 if (map
.containsKey(id
.name())) {
84 return map
.getString(id
.name());
91 * Translate the given {@link KeyStroke} into a user text {@link String} of
95 * the key to translate
97 * @return the translated text
99 public String
trans(KeyStroke key
) {
100 String keyTrans
= "";
102 switch (key
.getKeyType()) {
104 if (UiColors
.getInstance().isUnicode())
107 keyTrans
= trans(StringId
.KEY_ENTER
);
110 if (UiColors
.getInstance().isUnicode())
113 keyTrans
= trans(StringId
.KEY_TAB
);
117 keyTrans
= " " + key
.getCharacter() + " ";
120 keyTrans
= "" + key
.getKeyType();
122 if (keyTrans
.length() > width
) {
123 keyTrans
= keyTrans
.substring(0, width
);
124 } else if (keyTrans
.length() < width
) {
126 + new String(new char[width
- keyTrans
.length()])
136 * Initialise the translation mappings for the given language.
139 * the language to initialise
141 private void init(String lang
) {
142 Locale locale
= null;
145 locale
= Locale
.getDefault();
147 locale
= Locale
.forLanguageTag(lang
);
150 map
= Bundles
.getBundle("resources", locale
);