1 package be
.nikiroo
.utils
.resources
;
4 import java
.io
.IOException
;
6 import java
.util
.LinkedList
;
8 import java
.util
.Locale
;
9 import java
.util
.regex
.Pattern
;
11 import be
.nikiroo
.utils
.resources
.Bundles
;
14 * This class manages a translation-dedicated Bundle.
16 * Two special cases are handled for the used enum:
18 * <li>NULL will always will return an empty {@link String}</li>
19 * <li>DUMMY will return "[DUMMY]" (maybe with a suffix and/or "NOUTF")</li>
24 public class TransBundle
<E
extends Enum
<E
>> extends Bundle
<E
> {
25 private boolean utf
= true;
26 private Locale locale
;
27 private boolean defaultLocale
= false;
30 * Create a translation service with the default language.
33 * a runtime instance of the class of E
35 * the name of the {@link Bundles}
37 public TransBundle(Class
<E
> type
, Enum
<?
> name
) {
43 * Create a translation service for the given language (will fall back to
44 * the default one i not found).
47 * a runtime instance of the class of E
49 * the name of the {@link Bundles}
53 public TransBundle(Class
<E
> type
, Enum
<?
> name
, String language
) {
55 setLanguage(language
);
59 * Translate the given id into user text.
64 * the values to insert instead of the place holders in the
67 * @return the translated text with the given value where required or NULL
68 * if not found (not present in the resource file)
70 public String
getString(E stringId
, Object
... values
) {
71 return getStringX(stringId
, "", values
);
75 * Translate the given id into user text.
80 * the values to insert instead of the place holders in the
83 * @return the translated text with the given value where required or NULL
84 * if not found (not present in the resource file)
86 public String
getStringNOUTF(E stringId
, Object
... values
) {
87 return getStringX(stringId
, "NOUTF", values
);
91 * Translate the given id suffixed with the runtime value "_suffix" (that
92 * is, "_" and suffix) into user text.
97 * the values to insert instead of the place holders in the
102 * @return the translated text with the given value where required or NULL
103 * if not found (not present in the resource file)
105 public String
getStringX(E stringId
, String suffix
, Object
... values
) {
109 String key
= id
.name()
110 + ((suffix
== null || suffix
.isEmpty()) ?
"" : "_"
111 + suffix
.toUpperCase());
114 if (containsKey(key
+ "_NOUTF")) {
119 if ("NULL".equals(id
.name().toUpperCase())) {
121 } else if ("DUMMY".equals(id
.name().toUpperCase())) {
122 result
= "[" + key
.toLowerCase() + "]";
123 } else if (containsKey(key
)) {
124 result
= getString(key
);
129 if (values
!= null && values
.length
> 0 && result
!= null)
130 return String
.format(locale
, result
, values
);
136 * Check if unicode characters should be used.
138 * @return TRUE to allow unicode
140 public boolean isUnicode() {
145 * Allow or disallow unicode characters in the program.
148 * TRUE to allow unuciode, FALSE to only allow ASCII characters
150 public void setUnicode(boolean utf
) {
155 * Return all the languages known by the program.
158 * @return the known language codes
160 public List
<String
> getKnownLanguages() {
161 return getKnownLanguages(name
);
165 * Initialise the translation mappings for the given language.
168 * the language to initialise, in the form "en-GB" or "fr" for
171 private void setLanguage(String language
) {
172 defaultLocale
= (language
== null || language
.length() == 0);
173 locale
= getLocaleFor(language
);
174 setBundle(name
, locale
);
178 public void reload() {
179 setBundle(name
, locale
);
183 public String
getString(E id
) {
184 return getString(id
, (Object
[]) null);
188 * Create/update the .properties files for each supported language and for
189 * the default language.
191 * Note: this method is <b>NOT</b> thread-safe.
194 * the path where the .properties files are
196 * @throws IOException
197 * in case of IO errors
200 public void updateFile(String path
) throws IOException
{
201 String prev
= locale
.getLanguage();
203 setLanguage(null); // default locale
204 super.updateFile(path
);
206 for (String lang
: getKnownLanguages()) {
208 super.updateFile(path
);
215 protected File
getUpdateFile(String path
) {
216 String code
= locale
.toString();
218 if (!defaultLocale
&& code
.length() > 0) {
219 file
= new File(path
, name
.name() + "_" + code
+ ".properties");
221 // Default properties file:
222 file
= new File(path
, name
.name() + ".properties");
229 protected void writeHeader(Writer writer
) throws IOException
{
230 String code
= locale
.toString();
231 String name
= locale
.getDisplayCountry(locale
);
233 if (name
.length() == 0) {
234 name
= locale
.getDisplayLanguage(locale
);
237 if (name
.length() == 0) {
241 if (code
.length() > 0) {
242 name
= name
+ " (" + code
+ ")";
245 name
= (name
+ " " + getBundleDisplayName()).trim();
247 writer
.write("# " + name
+ " translation file (UTF-8)\n");
248 writer
.write("# \n");
249 writer
.write("# Note that any key can be doubled with a _NOUTF suffix\n");
250 writer
.write("# to use when the NOUTF env variable is set to 1\n");
251 writer
.write("# \n");
252 writer
.write("# Also, the comments always refer to the key below them.\n");
253 writer
.write("# \n");
257 protected void writeValue(Writer writer
, E id
) throws IOException
{
258 super.writeValue(writer
, id
);
260 String name
= id
.name() + "_NOUTF";
261 if (containsKey(name
)) {
262 String value
= getString(name
);
263 writeValue(writer
, name
, value
);
268 * Return the {@link Locale} representing the given language.
271 * the language to initialise, in the form "en-GB" or "fr" for
274 * @return the corresponding {@link Locale} or the default {@link Locale} if
277 static private Locale
getLocaleFor(String language
) {
280 if (language
== null) {
281 locale
= Locale
.getDefault();
283 language
= language
.replaceAll("_", "-");
284 String lang
= language
;
285 String country
= null;
286 if (language
.contains("-")) {
287 lang
= language
.split("-")[0];
288 country
= language
.split("-")[1];
292 locale
= new Locale(lang
, country
);
294 locale
= new Locale(lang
);
301 * Return all the languages known by the program.
304 * the enumeration on which we translate
306 * @return the known language codes
308 static protected List
<String
> getKnownLanguages(Enum
<?
> name
) {
309 List
<String
> resources
= new LinkedList
<String
>();
311 String regex
= ".*" + name
.name() + "[_a-zA-Za]*\\.properties$";
313 for (String res
: TransBundle_ResourceList
.getResources(Pattern
315 String resource
= res
;
316 int index
= resource
.lastIndexOf('/');
317 if (index
>= 0 && index
< (resource
.length() - 1))
318 resource
= resource
.substring(index
+ 1);
319 if (resource
.startsWith(name
.name())) {
320 resource
= resource
.substring(0, resource
.length()
321 - ".properties".length());
322 resource
= resource
.substring(name
.name().length());
323 if (resource
.startsWith("_")) {
324 resource
= resource
.substring(1);
325 resources
.add(resource
);