X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2Fbundles%2FTransBundle.java;h=5acd704402887bcb2e570b8f3c810c756db6e23e;hb=e27d1404e9222796fb6097ab41cff873148b65c7;hp=6ff63bffeb9e3eebd17e6346d06a037d28258f98;hpb=47d06cf312c7f7a86b5861bb9a762c8856c4f58e;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java b/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java index 6ff63bf..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: @@ -191,4 +197,34 @@ 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; + } }