fb9f29068171f12e8ffc7cc6ce8beebfefdae8b2
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
;
12 * This class manages a translation-dedicated Bundle.
14 * Two special cases are handled for the used enum:
16 * <li>NULL will always will return an empty {@link String}</li>
17 * <li>DUMMY will return "[DUMMY]" (maybe with a suffix and/or "NOUTF")</li>
21 * the enum to use to get values out of this class
25 public class TransBundle
<E
extends Enum
<E
>> extends Bundle
<E
> {
26 private boolean utf
= true;
27 private Locale locale
;
28 private boolean defaultLocale
= false;
31 * Create a translation service with the default language.
34 * a runtime instance of the class of E
36 * the name of the {@link Bundles}
38 public TransBundle(Class
<E
> type
, Enum
<?
> name
) {
39 this(type
, name
, (Locale
) null);
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}
51 * the language to use, can be NULL for default
53 public TransBundle(Class
<E
> type
, Enum
<?
> name
, String language
) {
54 super(type
, name
, null);
59 * Create a translation service for the given language (will fall back to
60 * the default one i not found).
63 * a runtime instance of the class of E
65 * the name of the {@link Bundles}
67 * the language to use, can be NULL for default
69 public TransBundle(Class
<E
> type
, Enum
<?
> name
, Locale language
) {
70 super(type
, name
, null);
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
getString(E stringId
, Object
... values
) {
87 return getStringX(stringId
, "", values
);
91 * Translate the given id into user text.
96 * the values to insert instead of the place holders in the
99 * @return the translated text with the given value where required or NULL
100 * if not found (not present in the resource file)
102 public String
getStringNOUTF(E stringId
, Object
... values
) {
103 return getStringX(stringId
, "NOUTF", values
);
107 * Translate the given id suffixed with the runtime value "_suffix" (that
108 * is, "_" and suffix) into user text.
111 * the ID to translate
113 * the values to insert instead of the place holders in the
118 * @return the translated text with the given value where required or NULL
119 * if not found (not present in the resource file)
121 public String
getStringX(E stringId
, String suffix
, Object
... values
) {
125 String key
= id
.name()
126 + ((suffix
== null || suffix
.isEmpty()) ?
"" : "_"
127 + suffix
.toUpperCase());
130 if (containsKey(key
+ "_NOUTF")) {
135 if ("NULL".equals(id
.name().toUpperCase())) {
137 } else if ("DUMMY".equals(id
.name().toUpperCase())) {
138 result
= "[" + key
.toLowerCase() + "]";
139 } else if (containsKey(key
)) {
140 result
= getString(key
);
145 if (values
!= null && values
.length
> 0 && result
!= null) {
146 return String
.format(locale
, result
, values
);
153 * Check if unicode characters should be used.
155 * @return TRUE to allow unicode
157 public boolean isUnicode() {
162 * Allow or disallow unicode characters in the program.
165 * TRUE to allow unuciode, FALSE to only allow ASCII characters
167 public void setUnicode(boolean utf
) {
172 * Return all the languages known by the program.
175 * @return the known language codes
177 public List
<String
> getKnownLanguages() {
178 return getKnownLanguages(keyType
);
182 * The current language (which can be the default one, but NOT NULL).
184 * @return the language, not NULL
186 public Locale
getLocale() {
191 * The current language (which can be the default one, but NOT NULL).
193 * @return the language, not NULL, in a display format (fr-BE, en-GB, es,
196 public String
getLocaleString() {
197 String lang
= locale
.getLanguage();
198 String country
= locale
.getCountry();
199 if (country
!= null && !country
.isEmpty()) {
200 return lang
+ "-" + country
;
206 * Initialise the translation mappings for the given language.
209 * the language to initialise, in the form "en-GB" or "fr" for
212 private void setLocale(String language
) {
213 setLocale(getLocaleFor(language
));
217 * Initialise the translation mappings for the given language.
220 * the language to initialise, or NULL for default
222 private void setLocale(Locale language
) {
223 if (language
!= null) {
224 defaultLocale
= false;
227 defaultLocale
= true;
228 locale
= Locale
.getDefault();
231 setBundle(keyType
, locale
, false);
235 public void reload(boolean resetToDefault
) {
236 setBundle(keyType
, locale
, resetToDefault
);
240 public String
getString(E id
) {
241 return getString(id
, (Object
[]) null);
245 * Create/update the .properties files for each supported language and for
246 * the default language.
248 * Note: this method is <b>NOT</b> thread-safe.
251 * the path where the .properties files are
253 * @throws IOException
254 * in case of IO errors
257 public void updateFile(String path
) throws IOException
{
258 String prev
= locale
.getLanguage();
259 Object status
= takeSnapshot();
262 setLocale((Locale
) null);
263 if (prev
.equals(Locale
.getDefault().getLanguage())) {
264 // restore snapshot if default locale = current locale
265 restoreSnapshot(status
);
267 super.updateFile(path
);
269 for (String lang
: getKnownLanguages()) {
271 if (lang
.equals(prev
)) {
272 restoreSnapshot(status
);
274 super.updateFile(path
);
278 restoreSnapshot(status
);
282 protected File
getUpdateFile(String path
) {
283 String code
= locale
.toString();
285 if (!defaultLocale
&& code
.length() > 0) {
286 file
= new File(path
, keyType
.name() + "_" + code
+ ".properties");
288 // Default properties file:
289 file
= new File(path
, keyType
.name() + ".properties");
296 protected void writeHeader(Writer writer
) throws IOException
{
297 String code
= locale
.toString();
298 String name
= locale
.getDisplayCountry(locale
);
300 if (name
.length() == 0) {
301 name
= locale
.getDisplayLanguage(locale
);
304 if (name
.length() == 0) {
308 if (code
.length() > 0) {
309 name
= name
+ " (" + code
+ ")";
312 name
= (name
+ " " + getBundleDisplayName()).trim();
314 writer
.write("# " + name
+ " translation file (UTF-8)\n");
315 writer
.write("# \n");
316 writer
.write("# Note that any key can be doubled with a _NOUTF suffix\n");
317 writer
.write("# to use when the NOUTF env variable is set to 1\n");
318 writer
.write("# \n");
319 writer
.write("# Also, the comments always refer to the key below them.\n");
320 writer
.write("# \n");
324 protected void writeValue(Writer writer
, E id
) throws IOException
{
325 super.writeValue(writer
, id
);
327 String name
= id
.name() + "_NOUTF";
328 if (containsKey(name
)) {
329 String value
= getString(name
);
330 writeValue(writer
, name
, value
);
335 * Return the {@link Locale} representing the given language.
338 * the language to initialise, in the form "en-GB" or "fr" for
341 * @return the corresponding {@link Locale} or NULL if it is not known
343 static private Locale
getLocaleFor(String language
) {
346 if (language
== null || language
.trim().isEmpty()) {
350 language
= language
.replaceAll("_", "-");
351 String lang
= language
;
352 String country
= null;
353 if (language
.contains("-")) {
354 lang
= language
.split("-")[0];
355 country
= language
.split("-")[1];
359 locale
= new Locale(lang
, country
);
361 locale
= new Locale(lang
);
367 * Return all the languages known by the program.
370 * the enumeration on which we translate
372 * @return the known language codes
374 static protected List
<String
> getKnownLanguages(Enum
<?
> name
) {
375 List
<String
> resources
= new LinkedList
<String
>();
377 String regex
= ".*" + name
.name() + "[_a-zA-Za]*\\.properties$";
379 for (String res
: TransBundle_ResourceList
.getResources(Pattern
381 String resource
= res
;
382 int index
= resource
.lastIndexOf('/');
383 if (index
>= 0 && index
< (resource
.length() - 1))
384 resource
= resource
.substring(index
+ 1);
385 if (resource
.startsWith(name
.name())) {
386 resource
= resource
.substring(0, resource
.length()
387 - ".properties".length());
388 resource
= resource
.substring(name
.name().length());
389 if (resource
.startsWith("_")) {
390 resource
= resource
.substring(1);
391 resources
.add(resource
);