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 String
getString(E id
) {
179 return getString(id
, (Object
[]) null);
183 * Create/update the .properties files for each supported language and for
184 * the default language.
186 * Note: this method is <b>NOT</b> thread-safe.
189 * the path where the .properties files are
191 * @throws IOException
192 * in case of IO errors
195 public void updateFile(String path
) throws IOException
{
196 String prev
= locale
.getLanguage();
198 setLanguage(null); // default locale
199 super.updateFile(path
);
201 for (String lang
: getKnownLanguages()) {
203 super.updateFile(path
);
210 protected File
getUpdateFile(String path
) {
211 String code
= locale
.toString();
213 if (!defaultLocale
&& code
.length() > 0) {
214 file
= new File(path
, name
.name() + "_" + code
+ ".properties");
216 // Default properties file:
217 file
= new File(path
, name
.name() + ".properties");
224 protected void writeHeader(Writer writer
) throws IOException
{
225 String code
= locale
.toString();
226 String name
= locale
.getDisplayCountry(locale
);
228 if (name
.length() == 0) {
229 name
= locale
.getDisplayLanguage(locale
);
232 if (name
.length() == 0) {
236 if (code
.length() > 0) {
237 name
= name
+ " (" + code
+ ")";
240 name
= (name
+ " " + getBundleDisplayName()).trim();
242 writer
.write("# " + name
+ " translation file (UTF-8)\n");
243 writer
.write("# \n");
244 writer
.write("# Note that any key can be doubled with a _NOUTF suffix\n");
245 writer
.write("# to use when the NOUTF env variable is set to 1\n");
246 writer
.write("# \n");
247 writer
.write("# Also, the comments always refer to the key below them.\n");
248 writer
.write("# \n");
252 protected void writeValue(Writer writer
, E id
) throws IOException
{
253 super.writeValue(writer
, id
);
255 String name
= id
.name() + "_NOUTF";
256 if (containsKey(name
)) {
257 String value
= getString(name
);
258 writeValue(writer
, name
, value
);
263 * Return the {@link Locale} representing the given language.
266 * the language to initialise, in the form "en-GB" or "fr" for
269 * @return the corresponding {@link Locale} or the default {@link Locale} if
272 static private Locale
getLocaleFor(String language
) {
275 if (language
== null) {
276 locale
= Locale
.getDefault();
278 language
= language
.replaceAll("_", "-");
279 String lang
= language
;
280 String country
= null;
281 if (language
.contains("-")) {
282 lang
= language
.split("-")[0];
283 country
= language
.split("-")[1];
287 locale
= new Locale(lang
, country
);
289 locale
= new Locale(lang
);
296 * Return all the languages known by the program.
299 * the enumeration on which we translate
301 * @return the known language codes
303 static protected List
<String
> getKnownLanguages(Enum
<?
> name
) {
304 List
<String
> resources
= new LinkedList
<String
>();
306 String regex
= ".*" + name
.name() + "[_a-zA-Za]*\\.properties$";
308 for (String res
: TransBundle_ResourceList
.getResources(Pattern
310 String resource
= res
;
311 int index
= resource
.lastIndexOf('/');
312 if (index
>= 0 && index
< (resource
.length() - 1))
313 resource
= resource
.substring(index
+ 1);
314 if (resource
.startsWith(name
.name())) {
315 resource
= resource
.substring(0, resource
.length()
316 - ".properties".length());
317 resource
= resource
.substring(name
.name().length());
318 if (resource
.startsWith("_")) {
319 resource
= resource
.substring(1);
320 resources
.add(resource
);