X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2Fbundles%2FTransBundle.java;h=5acd704402887bcb2e570b8f3c810c756db6e23e;hb=e27d1404e9222796fb6097ab41cff873148b65c7;hp=c79b0b69b62734d7b308ed07a50247fa15b7188e;hpb=88eb81220498dc5b3199bad2d1de6970b55ceaed;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java b/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java index c79b0b6..5acd704 100644 --- a/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java +++ b/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java @@ -3,11 +3,15 @@ package be.nikiroo.jvcard.resources.bundles; 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; /** @@ -20,6 +24,7 @@ import be.nikiroo.jvcard.resources.enums.StringId; public class TransBundle extends Bundle { private boolean utf = true; private Locale locale; + private boolean defaultLocale = false; /** * Create a translation service with the default language. @@ -76,7 +81,7 @@ public class TransBundle extends Bundle { } if (values != null && values.length > 0) - return String.format(locale, result, (Object[]) values); + return String.format(locale, result, values); else return result; } @@ -108,6 +113,7 @@ public class TransBundle extends Bundle { * instance */ private void setLanguage(String language) { + defaultLocale = (language == null || language.length() == 0); locale = getLocaleFor(language); map = getBundle(Target.resources, locale); } @@ -121,7 +127,7 @@ public class TransBundle extends Bundle { 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: @@ -148,6 +154,17 @@ public class TransBundle extends Bundle { 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. * @@ -181,4 +198,33 @@ public class TransBundle extends Bundle { return locale; } + /** + * Return all the languages known by the program. + * + * @return the known language codes + */ + static public List getKnownLanguages() { + List resources = new LinkedList(); + + 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; + } }