X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FTransBundle.java;h=7b2edb1ca919b4bc9f31e85d968ca8ee771d538f;hb=67b8ce74c4277c894aba234a7a493a93f6225a69;hp=125075a8bbd8268ca6c2310f2e3b40387132e2ac;hpb=e9ca6bb892b5358a273807bff834945ffe7b0cee;p=nikiroo-utils.git
diff --git a/src/be/nikiroo/utils/resources/TransBundle.java b/src/be/nikiroo/utils/resources/TransBundle.java
index 125075a..7b2edb1 100644
--- a/src/be/nikiroo/utils/resources/TransBundle.java
+++ b/src/be/nikiroo/utils/resources/TransBundle.java
@@ -17,6 +17,9 @@ import java.util.regex.Pattern;
*
DUMMY will return "[DUMMY]" (maybe with a suffix and/or "NOUTF")
*
*
+ * @param
+ * the enum to use to get values out of this class
+ *
* @author niki
*/
public class TransBundle> extends Bundle {
@@ -33,8 +36,7 @@ public class TransBundle> extends Bundle {
* the name of the {@link Bundles}
*/
public TransBundle(Class type, Enum> name) {
- super(type, name);
- setLanguage(null);
+ this(type, name, (Locale) null);
}
/**
@@ -46,11 +48,27 @@ public class TransBundle> extends Bundle {
* @param name
* the name of the {@link Bundles}
* @param language
- * the language to use
+ * the language to use, can be NULL for default
*/
public TransBundle(Class type, Enum> name, String language) {
- super(type, name);
- setLanguage(language);
+ super(type, name, null);
+ setLocale(language);
+ }
+
+ /**
+ * Create a translation service for the given language (will fall back to
+ * the default one i not found).
+ *
+ * @param type
+ * a runtime instance of the class of E
+ * @param name
+ * the name of the {@link Bundles}
+ * @param language
+ * the language to use, can be NULL for default
+ */
+ public TransBundle(Class type, Enum> name, Locale language) {
+ super(type, name, null);
+ setLocale(language);
}
/**
@@ -119,15 +137,19 @@ public class TransBundle> extends Bundle {
} else if ("DUMMY".equals(id.name().toUpperCase())) {
result = "[" + key.toLowerCase() + "]";
} else if (containsKey(key)) {
- result = getString(key);
+ result = getString(key, null);
+ if (result == null) {
+ result = getMetaDef(id.name());
+ }
} else {
result = null;
}
- if (values != null && values.length > 0 && result != null)
+ if (values != null && values.length > 0 && result != null) {
return String.format(locale, result, values);
- else
- return result;
+ }
+
+ return result;
}
/**
@@ -150,13 +172,36 @@ public class TransBundle> extends Bundle {
}
/**
- * Return all the languages known by the program.
- *
+ * Return all the languages known by the program for this bundle.
*
* @return the known language codes
*/
public List getKnownLanguages() {
- return getKnownLanguages(name);
+ return getKnownLanguages(keyType);
+ }
+
+ /**
+ * The current language (which can be the default one, but NOT NULL).
+ *
+ * @return the language, not NULL
+ */
+ public Locale getLocale() {
+ return locale;
+ }
+
+ /**
+ * The current language (which can be the default one, but NOT NULL).
+ *
+ * @return the language, not NULL, in a display format (fr-BE, en-GB, es,
+ * de...)
+ */
+ public String getLocaleString() {
+ String lang = locale.getLanguage();
+ String country = locale.getCountry();
+ if (country != null && !country.isEmpty()) {
+ return lang + "-" + country;
+ }
+ return lang;
}
/**
@@ -166,15 +211,31 @@ public class TransBundle> extends Bundle {
* the language to initialise, in the form "en-GB" or "fr" for
* instance
*/
- private void setLanguage(String language) {
- defaultLocale = (language == null || language.length() == 0);
- locale = getLocaleFor(language);
- setBundle(name, locale, false);
+ private void setLocale(String language) {
+ setLocale(getLocaleFor(language));
+ }
+
+ /**
+ * Initialise the translation mappings for the given language.
+ *
+ * @param language
+ * the language to initialise, or NULL for default
+ */
+ private void setLocale(Locale language) {
+ if (language != null) {
+ defaultLocale = false;
+ locale = language;
+ } else {
+ defaultLocale = true;
+ locale = Locale.getDefault();
+ }
+
+ setBundle(keyType, locale, false);
}
@Override
public void reload(boolean resetToDefault) {
- setBundle(name, locale, resetToDefault);
+ setBundle(keyType, locale, resetToDefault);
}
@Override
@@ -197,26 +258,26 @@ public class TransBundle> extends Bundle {
@Override
public void updateFile(String path) throws IOException {
String prev = locale.getLanguage();
- Object status = takeChangesSnapshot();
+ Object status = takeSnapshot();
// default locale
- setLanguage(null);
- if (prev.equals(getLocaleFor(null).getLanguage())) {
+ setLocale((Locale) null);
+ if (prev.equals(Locale.getDefault().getLanguage())) {
// restore snapshot if default locale = current locale
- restoreChanges(status);
+ restoreSnapshot(status);
}
super.updateFile(path);
for (String lang : getKnownLanguages()) {
- setLanguage(lang);
+ setLocale(lang);
if (lang.equals(prev)) {
- restoreChanges(status);
+ restoreSnapshot(status);
}
super.updateFile(path);
}
- setLanguage(prev);
- restoreChanges(status);
+ setLocale(prev);
+ restoreSnapshot(status);
}
@Override
@@ -224,10 +285,10 @@ public class TransBundle> extends Bundle {
String code = locale.toString();
File file = null;
if (!defaultLocale && code.length() > 0) {
- file = new File(path, name.name() + "_" + code + ".properties");
+ file = new File(path, keyType.name() + "_" + code + ".properties");
} else {
// Default properties file:
- file = new File(path, name.name() + ".properties");
+ file = new File(path, keyType.name() + ".properties");
}
return file;
@@ -267,8 +328,12 @@ public class TransBundle> extends Bundle {
String name = id.name() + "_NOUTF";
if (containsKey(name)) {
- String value = getString(name);
- writeValue(writer, name, value);
+ String value = getString(name, null);
+ if (value == null) {
+ value = getMetaDef(id.name());
+ }
+ boolean set = isSet(id, false);
+ writeValue(writer, name, value, set);
}
}
@@ -279,29 +344,28 @@ public class TransBundle> extends Bundle {
* the language to initialise, in the form "en-GB" or "fr" for
* instance
*
- * @return the corresponding {@link Locale} or the default {@link Locale} if
- * it is not known
+ * @return the corresponding {@link Locale} or NULL if it is not known
*/
static private Locale getLocaleFor(String language) {
Locale locale;
- if (language == null) {
- locale = Locale.getDefault();
- } else {
- language = language.replaceAll("_", "-");
- String lang = language;
- String country = null;
- if (language.contains("-")) {
- lang = language.split("-")[0];
- country = language.split("-")[1];
- }
+ if (language == null || language.trim().isEmpty()) {
+ return null;
+ }
- if (country != null)
- locale = new Locale(lang, country);
- else
- locale = new Locale(lang);
+ language = language.replaceAll("_", "-");
+ String lang = language;
+ String country = null;
+ if (language.contains("-")) {
+ lang = language.split("-")[0];
+ country = language.split("-")[1];
}
+ if (country != null)
+ locale = new Locale(lang, country);
+ else
+ locale = new Locale(lang);
+
return locale;
}