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;
import be.nikiroo.jvcard.resources.Bundles;
import be.nikiroo.jvcard.resources.Bundles.Bundle;
import be.nikiroo.jvcard.resources.Bundles.Target;
+import be.nikiroo.jvcard.resources.ResourceList;
import be.nikiroo.jvcard.resources.enums.StringId;
/**
public class TransBundle extends Bundle<StringId> {
private boolean utf = true;
private Locale locale;
+ private boolean defaultLocale = false;
/**
* Create a translation service with the default language.
}
if (values != null && values.length > 0)
- return String.format(locale, result, (Object[]) values);
+ return String.format(locale, result, values);
else
return result;
}
* instance
*/
private void setLanguage(String language) {
+ defaultLocale = (language == null || language.length() == 0);
locale = getLocaleFor(language);
map = getBundle(Target.resources, locale);
}
protected File getUpdateFile(String path) {
String code = locale.toString();
File file = null;
- if (code.length() > 0) {
+ if (!defaultLocale && code.length() > 0) {
file = new File(path, name.name() + "_" + code + ".properties");
} else {
// Default properties file:
StringId.writeHeader(writer, name);
}
+ @Override
+ protected void writeValue(Writer writer, StringId id) throws IOException {
+ super.writeValue(writer, id);
+
+ String name = id.name() + "_NOUTF";
+ if (map.containsKey(name)) {
+ String value = map.getString(name).trim();
+ writeValue(writer, name, value);
+ }
+ }
+
/**
* Return the {@link Locale} representing the given language.
*
return locale;
}
+ /**
+ * Return all the languages known by the program.
+ *
+ * @return the known language codes
+ */
+ static public List<String> getKnownLanguages() {
+ List<String> resources = new LinkedList<String>();
+
+ String regex = ".*" + Target.resources.name()
+ + "[_a-zA-Za]*\\.properties$";
+
+ for (String res : ResourceList.getResources(Pattern.compile(regex))) {
+ String resource = res;
+ int index = resource.lastIndexOf('/');
+ if (index >= 0 && index < (resource.length() - 1))
+ resource = resource.substring(index + 1);
+ if (resource.startsWith(Target.resources.name())) {
+ resource = resource.substring(0, resource.length()
+ - ".properties".length());
+ resource = resource.substring(Target.resources.name().length());
+ if (resource.startsWith("_")) {
+ resource = resource.substring(1);
+ resources.add(resource);
+ }
+ }
+ }
+
+ return resources;
+ }
}