Index: lutinutil/src/java/org/codelutin/i18n/I18nLoader.java diff -u /dev/null lutinutil/src/java/org/codelutin/i18n/I18nLoader.java:1.1 --- /dev/null Sat Mar 22 23:27:13 2008 +++ lutinutil/src/java/org/codelutin/i18n/I18nLoader.java Sat Mar 22 23:27:08 2008 @@ -0,0 +1,118 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.i18n; + +import org.codelutin.util.ConverterUtil; + +import java.net.URL; +import java.util.Locale; + +/** + * Classe responsalbe du chargement du systeme i18n. + *
+ * L'invarian de ce chargeur est l'eecoding + * + * @author chemit + */ +public class I18nLoader { + + /** l'encoding a utiliser pour charger les traductions */ + protected final String encoding; + + /** la locale a utiliser si aucune n'est preciser pendant l'init */ + protected final Locale defaultLocale; + + public I18nLoader(String encoding, Locale defaultLocale) { + this.encoding = encoding; + this.defaultLocale = defaultLocale; + } + + public String getEncoding() { + return encoding; + } + + public Locale getDefaultLocale() { + return defaultLocale; + } + + public Language getLanguage() { + return LanguageManager.getLanguage(); + } + + /** + * Initialise la librairie + * + * @param extraUrl some extra urls where to find bundles + */ + protected void init(URL... extraUrl) { + init(defaultLocale, extraUrl); + } + + /** + * Initialise la librairie + * + * @param newLocale la locale a charger + * @param extraUrl some extra urls where to find bundles + */ + protected void init(Locale newLocale, URL... extraUrl) { + if (newLocale == null) { + newLocale = defaultLocale; + } + setLanguage(newLocale, encoding, extraUrl); + } + + /** + * Initialise la librairie + * + * @param language une chaine representant la langue à utiliser fr, en, ... + * @param extraUrl some extra urls where to find bundles + */ + protected void init(String language, URL... extraUrl) { + init(language, null, 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 extraUrl some extra urls where to find bundles + */ + protected void init(String language, String country, URL... extraUrl) { + if (language == null) { + language = System.getProperty("user.language", defaultLocale.getLanguage()); + } + if (country == null) { + country = System.getProperty("user.country", defaultLocale.getCountry()); + } + Locale newLocale = newLocale(language + "_" + country); + + setLanguage(newLocale, encoding, extraUrl); + } + + protected void setLanguage(Locale newLocale, String toEncoding, URL... extraUrl) { + // delegate to LanguageManager + LanguageManager.setLanguage(newLocale, toEncoding, extraUrl); + } + + public static Locale newLocale(String str) { + return ConverterUtil.convert(Locale.class, str); + } + + public void close() { + //TODO + } +}