Index: lutinutil/src/java/org/codelutin/i18n/I18n.java diff -u lutinutil/src/java/org/codelutin/i18n/I18n.java:1.14 lutinutil/src/java/org/codelutin/i18n/I18n.java:1.15 --- lutinutil/src/java/org/codelutin/i18n/I18n.java:1.14 Thu Mar 20 01:29:42 2008 +++ lutinutil/src/java/org/codelutin/i18n/I18n.java Sat Mar 22 23:29:24 2008 @@ -23,15 +23,14 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.14 $ + * @version $Revision: 1.15 $ * - * Mise a jour: $Date: 2008-03-20 01:29:42 $ + * Mise a jour: $Date: 2008-03-22 23:29:24 $ * par : $Author: tchemit $ */ package org.codelutin.i18n; -import java.io.InputStream; import java.net.URL; import java.text.MessageFormat; import java.util.Arrays; @@ -64,10 +63,25 @@ static final String UTF_8_ENCONDING = "UTF-8"; - static final LocaleEnum DEFAULT_LOCALE = LocaleEnum.en; + static final Locale DEFAULT_LOCALE = Locale.UK; public static final String DEFAULT_ENCODING = ISO_8859_1_ENCONDING; + static I18nLoader loader; + + protected static I18nLoader getLoader(String encoding) { + if (loader == null) { + loader = new I18nLoader(encoding, DEFAULT_LOCALE); + } else { + if (!loader.getEncoding().equals(encoding)) { + // close previous loader + loader.close(); + loader = new I18nLoader(encoding, DEFAULT_LOCALE); + } + } + return loader; + } + /** Filtre a appliquer avant de retourner les chaines */ protected static I18nFilter filter; @@ -75,24 +89,27 @@ protected static String recordFilePath; /** - * Initialize the library for given localeISO-8859-1 enconding + * Initialise la librairie avec encoing par defaut et locale par defaut * - * @param locale language to use - * @param extraUrl + * @param extraUrl some extra urls where to find bundles */ - public static void initISO88591(LocaleEnum locale, URL... extraUrl) { - setLanguage(locale, ISO_8859_1_ENCONDING, extraUrl); + public static void init(URL... extraUrl) { + init(DEFAULT_LOCALE, DEFAULT_ENCODING, extraUrl); } + //=========================================================== + //== With default encoding init + //=========================================================== + /** * Initialize the library for given localeUTF-8 enconding + * default encoding says ISO-8859-1 enconding * - * @param locale language to use + * @param locale language to use + * @param extraUrl some extra urls where to find bundles */ - public static void initUTF8(LocaleEnum locale) { - setLanguage(locale, UTF_8_ENCONDING); + public static void init(Locale locale, URL... extraUrl) { + init(locale, DEFAULT_ENCODING, extraUrl); } /** @@ -100,101 +117,147 @@ * * @param language une chaine representant la langue à utiliser fr, en, ... * @param country une chaine representant le pays à utiliser FR, GB, ... - * @deprecated voir le bug sur {@link I18nFileReader#load(InputStream, String)} - */ - public static void init(String language, String country) { - init(language, country, null); - } - - /** - * Initialize the library for given language and given - * country with ISO-8859-1 enconding - * - * @param language language to use - * @param country country to use + * @param extraUrl some extra urls where to find bundles */ - public static void initISO88591(String language, String country) { - init(language, country, ISO_8859_1_ENCONDING); + public static void init(String language, String country, URL... extraUrl) { + init(language, country, DEFAULT_ENCODING, extraUrl); } /** - * Initialize the library for given language and - * given country with UTF-81 enconding - * - * @param language language to use - * @param country country to use - */ - public static void initUTF8(String language, String country) { - init(language, country, UTF_8_ENCONDING); - } - - /** - * Initialize the library for given language with - * ISO-8859-1 enconding - * - * @param language language to use - */ - public static void initISO88591(String language) { - initISO88591(language, null); - } - - /** - * Initialize the library for given language with - * UTF-8 enconding + * Initialise la librairie. * - * @param language language to use - */ - public static void initUTF8(String language) { - initUTF8(language, null); - } - - /** - * Initialize the library with default locale {@link #DEFAULT_LOCALE} - * with ISO-8859-1 enconding + * @param language une chaine representant la langue à utiliser fr, en, ... + * @param extraUrl some extra urls where to find bundles + * @deprecated */ - public static void initISO88591() { - initISO88591(null); + public static void init(String language, URL... extraUrl) { + init(language, null, DEFAULT_ENCODING, extraUrl); } - /** - * Initialize the library with default locale {@link #DEFAULT_LOCALE} - * with UTF-8 enconding - */ - public static void initUTF8() { - initUTF8((String) null); - } +// //=========================================================== +// //== With ISO-8859-1 encoding init +// //=========================================================== +// +// /** +// * Initialize the library with default locale {@link #DEFAULT_LOCALE} +// * with ISO-8859-1 enconding +// * +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initISO88591(URL... extraUrl) { +// init((Locale) null, ISO_8859_1_ENCONDING, extraUrl); +// } +// +// /** +// * Initialize the library for given localeISO-8859-1 enconding +// * +// * @param locale language to use +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initISO88591(Locale locale, URL... extraUrl) { +// init(locale, ISO_8859_1_ENCONDING, extraUrl); +// } +// +// /** +// * Initialize the library for given language and given +// * country with ISO-8859-1 enconding +// * +// * @param language language to use +// * @param country country to use +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initISO88591(String language, String country, URL... extraUrl) { +// init(language, country, ISO_8859_1_ENCONDING, extraUrl); +// } +// +// /** +// * Initialize the library for given language with +// * ISO-8859-1 enconding +// * +// * @param language language to use +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initISO88591(String language, URL... extraUrl) { +// init(language,null, ISO_8859_1_ENCONDING, extraUrl); +// } +// +// //=========================================================== +// //== With UTF-8 encoding init +// //=========================================================== +// +// /** +// * Initialize the library with default locale {@link #DEFAULT_LOCALE} +// * with UTF-8 enconding +// * +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initUTF8(URL... extraUrl) { +// init((String) null, UTF_8_ENCONDING, extraUrl); +// } +// +// /** +// * Initialize the library for given localeUTF-8 enconding +// * +// * @param locale language to use +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initUTF8(Locale locale, URL... extraUrl) { +// init(locale, UTF_8_ENCONDING, extraUrl); +// } +// +// /** +// * Initialize the library for given language and +// * given country with UTF-81 enconding +// * +// * @param language language to use +// * @param country country to use +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initUTF8(String language, String country, URL... extraUrl) { +// init(language, country, UTF_8_ENCONDING, extraUrl); +// } +// +// /** +// * Initialize the library for given language with +// * UTF-8 enconding +// * +// * @param language language to use +// * @param extraUrl some extra urls where to find bundles +// */ +// public static void initUTF8(String language, URL... extraUrl) { +// init(language, null, UTF_8_ENCONDING, extraUrl); +// } + + //=========================================================== + //== With given encoding init + //=========================================================== /** - * Initialise la librairie. + * Initialise la librairie * * @param language une chaine representant la langue à utiliser fr, en, ... - * @deprecated + * @param country une chaine representant le pays à utiliser FR, GB, ... + * @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise + * @param extraUrl some extra urls where to find bundles */ - public static void init(String language) { - init(language, null, DEFAULT_ENCODING); + public static void init(String language, String country, String encoding, URL... extraUrl) { + getLoader(encoding).init(language, country, extraUrl); } /** * Initialise la librairie * - * @param language une chaine representant la langue à utiliser fr, en, ... - * @param country une chaine representant le pays à utiliser FR, GB, ... - * @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise + * @param newLocale la locale a charger + * @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise + * @param extraUrl some extra urls where to find bundles */ - protected static void init(String language, String country, String encoding) { - if (language == null) { - language = System.getProperty("user.language", DEFAULT_LOCALE.getLangage().toString()); - } - if (country == null) { - country = System.getProperty("user.country", DEFAULT_LOCALE.getCountry().toString()); - } - setLanguage( - LanguageEnum.valueOf(language, DEFAULT_LOCALE.getLangage()), - CountryEnum.valueOf(country, DEFAULT_LOCALE.getCountry()), - encoding == null ? DEFAULT_ENCODING : encoding - ); + public static void init(Locale newLocale, String encoding, URL... extraUrl) { + getLoader(encoding).init(newLocale, extraUrl); } + /** * Retourne la chaine traduite si possible. * @@ -203,10 +266,10 @@ * sinon. */ public static String _(String message) { - if (getLanguage() == null) { + if (loader.getLanguage() == null) { return applyFilter(message); } - return applyFilter(getLanguage().translate(message)); + return applyFilter(loader.getLanguage().translate(message)); } /** @@ -219,7 +282,7 @@ */ public static String _(String message, Object... args) { String result = message; - Language language = getLanguage(); + Language language = loader.getLanguage(); if (language != null) { result = language.translate(message); } @@ -278,10 +341,6 @@ return message; } - protected static Language getLanguage() { - return LanguageManager.getLanguage(); - } - protected static I18nFilter getFilter() { return filter; } @@ -290,17 +349,14 @@ return recordFilePath; } - protected static void setLanguage(LocaleEnum locale, String toEncoding, URL... extraUrl) { - Locale newLocale = locale.getLocale(); - // delegate to LanguageManager - LanguageManager.setLanguage(newLocale, toEncoding, extraUrl); - } + /*protected static Language getLanguage() { + return LanguageManager.getLanguage(); + }*/ - protected static void setLanguage(LanguageEnum language, CountryEnum country, String toEncoding, URL... extraUrl) { - Locale newLocale = newLocale(language, country); + /*protected static void setLanguage(Locale newLocale, String toEncoding, URL... extraUrl) { // delegate to LanguageManager LanguageManager.setLanguage(newLocale, toEncoding, extraUrl); - } + }*/ /** * Change le filtre des chaines traduites @@ -315,11 +371,8 @@ I18n.recordFilePath = recordFilePath; } - protected static Locale newLocale(LanguageEnum language, CountryEnum country) { - if (country == null) { - return new Locale(language.toString()); - } - return new Locale(language.toString(), country.toString()); - } + /*protected static Locale newLocale(String str) { + return ConverterUtil.convert(Locale.class, str); + } */ } //I18n \ No newline at end of file