X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2Fbundles%2FTransBundle.java;h=5acd704402887bcb2e570b8f3c810c756db6e23e;hp=7199158bba8d227322e4cc00e4d3bc52af0dbcf2;hb=e27d1404e9222796fb6097ab41cff873148b65c7;hpb=71ac69a01a1051b24386ac67124c0aee657f3225 diff --git a/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java b/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java index 7199158..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. @@ -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; + } }