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);
writeValue(writer, name, value);
}
}
/**
* 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