X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2Fbundles%2FTransBundle.java;fp=src%2Fbe%2Fnikiroo%2Fjvcard%2Fresources%2Fbundles%2FTransBundle.java;h=0000000000000000000000000000000000000000;hp=5acd704402887bcb2e570b8f3c810c756db6e23e;hb=f06c81000632cfb5f525ca458f719338f55f9f66;hpb=a73a906356c971b080c36368e71a15d87e8b8d31 diff --git a/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java b/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java deleted file mode 100644 index 5acd704..0000000 --- a/src/be/nikiroo/jvcard/resources/bundles/TransBundle.java +++ /dev/null @@ -1,230 +0,0 @@ -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; - -/** - * This class manages the translation of {@link TransBundle.StringId}s into - * user-understandable text. - * - * @author niki - * - */ -public class TransBundle extends Bundle { - private boolean utf = true; - private Locale locale; - private boolean defaultLocale = false; - - /** - * Create a translation service with the default language. - */ - public TransBundle() { - new Bundles().super(StringId.class, Target.resources); - setLanguage(null); - } - - /** - * Create a translation service for the given language. (Will fall back to - * the default one i not found.) - * - * @param language - * the language to use - */ - public TransBundle(String language) { - new Bundles().super(StringId.class, Target.resources); - - setLanguage(language); - } - - /** - * Translate the given {@link StringId} into user text. - * - * @param stringId - * the ID to translate - * @param values - * the values to insert instead of the place holders in the - * translation - * - * @return the translated text with the given value where required - */ - public String getString(StringId stringId, Object... values) { - StringId id = stringId; - String result = ""; - - if (!isUnicode()) { - try { - id = StringId.valueOf(stringId.name() + "_NOUTF"); - } catch (IllegalArgumentException iae) { - // no special _NOUTF version found - } - } - - if (id == StringId.NULL) { - result = ""; - } else if (id == StringId.DUMMY) { - result = "[dummy]"; - } else if (map.containsKey(id.name())) { - result = map.getString(id.name()); - } else { - result = id.toString(); - } - - if (values != null && values.length > 0) - return String.format(locale, result, values); - else - return result; - } - - /** - * Check if unicode characters should be used. - * - * @return TRUE to allow unicode - */ - public boolean isUnicode() { - return utf; - } - - /** - * Allow or disallow unicode characters in the program. - * - * @param utf - * TRUE to allow unuciode, FALSE to only allow ASCII characters - */ - public void setUnicode(boolean utf) { - this.utf = utf; - } - - /** - * Initialise the translation mappings for the given language. - * - * @param language - * the language to initialise, in the form "en-GB" or "fr" for - * instance - */ - private void setLanguage(String language) { - defaultLocale = (language == null || language.length() == 0); - locale = getLocaleFor(language); - map = getBundle(Target.resources, locale); - } - - @Override - public String getString(StringId id) { - return getString(id, (Object[]) null); - } - - @Override - protected File getUpdateFile(String path) { - String code = locale.toString(); - File file = null; - if (!defaultLocale && code.length() > 0) { - file = new File(path, name.name() + "_" + code + ".properties"); - } else { - // Default properties file: - file = new File(path, name.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 + ")"; - } - - 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. - * - * @param language - * the language to initialise, in the form "en-GB" or "fr" for - * instance - * - * @return the corresponding {@link Locale} or the default {@link Locale} if - * it is not known - */ - static private Locale getLocaleFor(String language) { - Locale locale; - - if (language == null) { - locale = Locale.getDefault(); - } else { - 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. - * - * @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; - } -}