X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FTransBundle.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FTransBundle.java;h=28fa2802e215eab074ff51405d7488b733f84459;hp=0000000000000000000000000000000000000000;hb=d46b7b96f94e88a776bcd2dfd756549ffb300cc9;hpb=c9994f27667bc421bcd448d39e55774fddf5c431 diff --git a/src/be/nikiroo/utils/resources/TransBundle.java b/src/be/nikiroo/utils/resources/TransBundle.java new file mode 100644 index 0000000..28fa280 --- /dev/null +++ b/src/be/nikiroo/utils/resources/TransBundle.java @@ -0,0 +1,398 @@ +package be.nikiroo.utils.resources; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.regex.Pattern; + +/** + * This class manages a translation-dedicated Bundle. + *
+ * Two special cases are handled for the used enum: + *
+ * Note: this method is NOT thread-safe.
+ *
+ * @param path
+ * the path where the .properties files are
+ *
+ * @throws IOException
+ * in case of IO errors
+ */
+ @Override
+ public void updateFile(String path) throws IOException {
+ String prev = locale.getLanguage();
+ Object status = takeSnapshot();
+
+ // default locale
+ setLocale((Locale) null);
+ if (prev.equals(Locale.getDefault().getLanguage())) {
+ // restore snapshot if default locale = current locale
+ restoreSnapshot(status);
+ }
+ super.updateFile(path);
+
+ for (String lang : getKnownLanguages()) {
+ setLocale(lang);
+ if (lang.equals(prev)) {
+ restoreSnapshot(status);
+ }
+ super.updateFile(path);
+ }
+
+ setLocale(prev);
+ restoreSnapshot(status);
+ }
+
+ @Override
+ protected File getUpdateFile(String path) {
+ String code = locale.toString();
+ File file = null;
+ if (!defaultLocale && code.length() > 0) {
+ file = new File(path, keyType.name() + "_" + code + ".properties");
+ } else {
+ // Default properties file:
+ file = new File(path, keyType.name() + ".properties");
+ }
+
+ return file;
+ }
+
+ @Override
+ protected void writeHeader(Writer writer) throws IOException {
+ String code = locale.toString();
+ String name = locale.getDisplayCountry(locale);
+
+ if (name.length() == 0) {
+ name = locale.getDisplayLanguage(locale);
+ }
+
+ if (name.length() == 0) {
+ name = "default";
+ }
+
+ if (code.length() > 0) {
+ name = name + " (" + code + ")";
+ }
+
+ name = (name + " " + getBundleDisplayName()).trim();
+
+ writer.write("# " + name + " translation file (UTF-8)\n");
+ writer.write("# \n");
+ writer.write("# Note that any key can be doubled with a _NOUTF suffix\n");
+ writer.write("# to use when the NOUTF env variable is set to 1\n");
+ writer.write("# \n");
+ writer.write("# Also, the comments always refer to the key below them.\n");
+ writer.write("# \n");
+ }
+
+ @Override
+ protected void writeValue(Writer writer, E id) throws IOException {
+ super.writeValue(writer, id);
+
+ String name = id.name() + "_NOUTF";
+ if (containsKey(name)) {
+ String value = getString(name, null);
+ boolean set = isSet(id, false);
+ writeValue(writer, name, value, set);
+ }
+ }
+
+ /**
+ * Return the {@link Locale} representing the given language.
+ *
+ * @param language
+ * the language to initialise, in the form "en-GB" or "fr" for
+ * instance
+ *
+ * @return the corresponding {@link Locale} or NULL if it is not known
+ */
+ static private Locale getLocaleFor(String language) {
+ Locale locale;
+
+ if (language == null || language.trim().isEmpty()) {
+ return null;
+ }
+
+ 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;
+ }
+
+ /**
+ * Return all the languages known by the program.
+ *
+ * @param name
+ * the enumeration on which we translate
+ *
+ * @return the known language codes
+ */
+ static protected List