Resources system rewrite + new "--save-config DIR" option
[jvcard.git] / src / be / nikiroo / jvcard / resources / bundles / TransBundle.java
1 package be.nikiroo.jvcard.resources.bundles;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.Writer;
6 import java.util.Locale;
7
8 import be.nikiroo.jvcard.resources.Bundles;
9 import be.nikiroo.jvcard.resources.Bundles.Bundle;
10 import be.nikiroo.jvcard.resources.Bundles.Target;
11 import be.nikiroo.jvcard.resources.enums.StringId;
12
13 /**
14 * This class manages the translation of {@link TransBundle.StringId}s into
15 * user-understandable text.
16 *
17 * @author niki
18 *
19 */
20 public class TransBundle extends Bundle<StringId> {
21 private boolean utf = true;
22 private Locale locale;
23
24 /**
25 * Create a translation service with the default language.
26 */
27 public TransBundle() {
28 new Bundles().super(StringId.class, Target.resources);
29 setLanguage(null);
30 }
31
32 /**
33 * Create a translation service for the given language. (Will fall back to
34 * the default one i not found.)
35 *
36 * @param language
37 * the language to use
38 */
39 public TransBundle(String language) {
40 new Bundles().super(StringId.class, Target.resources);
41
42 setLanguage(language);
43 }
44
45 @Override
46 public String getString(StringId id) {
47 return getString(id, (Object[]) null);
48 }
49
50 /**
51 * Translate the given {@link StringId} into user text.
52 *
53 * @param stringId
54 * the ID to translate
55 * @param values
56 * the values to insert instead of the place holders in the
57 * translation
58 *
59 * @return the translated text with the given value where required
60 */
61 public String getString(StringId stringId, Object... values) {
62 StringId id = stringId;
63 String result = "";
64
65 if (!isUnicode()) {
66 try {
67 id = StringId.valueOf(stringId.name() + "_NOUTF");
68 } catch (IllegalArgumentException iae) {
69 // no special _NOUTF version found
70 }
71 }
72
73 if (id == StringId.NULL) {
74 result = "";
75 } else if (id == StringId.DUMMY) {
76 result = "[dummy]";
77 } else if (map.containsKey(id.name())) {
78 result = map.getString(id.name());
79 } else {
80 result = id.toString();
81 }
82
83 if (values != null && values.length > 0)
84 return String.format(locale, result, (Object[]) values);
85 else
86 return result;
87 }
88
89 /**
90 * Check if unicode characters should be used.
91 *
92 * @return TRUE to allow unicode
93 */
94 public boolean isUnicode() {
95 return utf;
96 }
97
98 /**
99 * Allow or disallow unicode characters in the program.
100 *
101 * @param utf
102 * TRUE to allow unuciode, FALSE to only allow ASCII characters
103 */
104 public void setUnicode(boolean utf) {
105 this.utf = utf;
106 }
107
108 @Override
109 protected File getUpdateFile(String path) {
110 String code = locale.toString();
111 File file = null;
112 if (code.length() > 0) {
113 file = new File(path, name.name() + "_" + code + ".properties");
114 } else {
115 // Default properties file:
116 file = new File(path, name.name() + ".properties");
117 }
118
119 return file;
120 }
121
122 @Override
123 protected void writeHeader(Writer writer) throws IOException {
124 String code = locale.toString();
125 String name = locale.getDisplayCountry(locale);
126
127 if (name.length() == 0)
128 name = locale.getDisplayLanguage(locale);
129 if (name.length() == 0)
130 name = "default";
131
132 if (code.length() > 0) {
133 name = name + " (" + code + ")";
134 }
135
136 StringId.writeHeader(writer, name);
137 }
138
139 /**
140 * Initialise the translation mappings for the given language.
141 *
142 * @param language
143 * the language to initialise, in the form "en-GB" or "fr" for
144 * instance
145 */
146 private void setLanguage(String language) {
147 locale = getLocaleFor(language);
148 map = getBundle(Target.resources, locale);
149 }
150
151 /**
152 * Return the {@link Locale} representing the given language.
153 *
154 * @param language
155 * the language to initialise, in the form "en-GB" or "fr" for
156 * instance
157 *
158 * @return the corresponding {@link Locale} or the default {@link Locale} if
159 * it is not known
160 */
161 static private Locale getLocaleFor(String language) {
162 Locale locale;
163
164 if (language == null) {
165 locale = Locale.getDefault();
166 } else {
167 language = language.replaceAll("_", "-");
168 String lang = language;
169 String country = null;
170 if (language.contains("-")) {
171 lang = language.split("-")[0];
172 country = language.split("-")[1];
173 }
174
175 if (country != null)
176 locale = new Locale(lang, country);
177 else
178 locale = new Locale(lang);
179 }
180
181 return locale;
182 }
183 }