Index: lutinutil/src/java/org/codelutin/i18n/I18n.java diff -u lutinutil/src/java/org/codelutin/i18n/I18n.java:1.16 lutinutil/src/java/org/codelutin/i18n/I18n.java:1.17 --- lutinutil/src/java/org/codelutin/i18n/I18n.java:1.16 Sun Mar 23 06:02:24 2008 +++ lutinutil/src/java/org/codelutin/i18n/I18n.java Sun Mar 23 21:08:16 2008 @@ -23,15 +23,16 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.16 $ + * @version $Revision: 1.17 $ * - * Mise a jour: $Date: 2008-03-23 06:02:24 $ + * Mise a jour: $Date: 2008-03-23 21:08:16 $ * par : $Author: tchemit $ */ package org.codelutin.i18n; import org.codelutin.i18n.bundle.I18nBundleManager; +import org.codelutin.util.ConverterUtil; import java.net.URL; import java.text.MessageFormat; @@ -65,17 +66,20 @@ public static final String UTF_8_ENCONDING = "UTF-8"; - public static final Locale DEFAULT_LOCALE = Locale.UK; - public static final String DEFAULT_ENCODING = ISO_8859_1_ENCONDING; + public static final Locale DEFAULT_LOCALE = Locale.UK; + /** la classe responsable du chargement des ressources */ static I18nLoader loader; + /** la gestionnaire de bundle */ + static I18nBundleManager bundleManager; + /** Filtre a appliquer avant de retourner les chaines */ protected static I18nFilter filter; - /** Indique le chamin du fichier dans lequel ecrire les entrees non trouvees */ + /** Indique le chemin du fichier dans lequel ecrire les entrees non trouvees */ protected static String recordFilePath; /** @@ -83,41 +87,40 @@ *

* Note: use this before call init(...) methods */ - protected static URL[] extraURL; + static URL[] extraURL; - /** Initialise la librairie avec encoing par defaut et locale par defaut */ + /** Initialise la librairie avec encoding par defaut et locale par defaut */ public static void init() { - init(null, null, DEFAULT_ENCODING); + init(null, null, null); } /** - * Initialize the library for given localeISO-8859-1 enconding + * Initialize the library for given locale + * This method should be called to reset all caches (languages, bundles,...) + */ public static void close() { if (loader != null) { loader.close(); loader = null; } + if (bundleManager != null) { + bundleManager.close(); + bundleManager = null; + } } /** @@ -268,25 +284,62 @@ return filter; } - public static URL[] getExtraURL() { - return extraURL == null ? new URL[0] : extraURL; + public static synchronized I18nBundleManager getBundleManager() { + if (bundleManager == null) { + bundleManager = new I18nBundleManager(DEFAULT_LOCALE); + } + return bundleManager; } + /** + * Get the i18n loader with good encoding. + *

+ * If no loader found, then instanciate a new one. + *

+ * If a previous loader is detected, then it is closed. + *

+ * If encoding is null, then we use default one {@link #DEFAULT_ENCODING}. + * + * @param encoding encoding to use for loader + * @return the required loader with given encoding + */ protected static synchronized I18nLoader getLoader(String encoding) { if (encoding == null) { encoding = DEFAULT_ENCODING; } if (loader == null) { - loader = new I18nLoader(encoding, DEFAULT_LOCALE); + loader = new I18nLoader(encoding); } else { if (!loader.getEncoding().equals(encoding)) { - // close previous loader + // close previous loader but not the bundle manager loader.close(); // open a new loader - loader = new I18nLoader(encoding, DEFAULT_LOCALE); + loader = new I18nLoader(encoding); } } return loader; } + public static Locale newLocale(String str) { + if (str == null) { + // get use locale + return newLocale(null, null); + } + try { + return ConverterUtil.convert(Locale.class, str); + } catch (Exception e) { + Logger.getLogger("org.codelutin.i18n.I18n").warning("could not load locale '"+str+" for reason : "+e.getMessage()); + // use default locale + return DEFAULT_LOCALE; + } + } + + public static Locale newLocale(String language, String country) { + if (language == null) { + // get user locale + language = System.getProperty("user.language", DEFAULT_LOCALE.getLanguage()); + country = System.getProperty("user.country", DEFAULT_LOCALE.getCountry()); + } + return newLocale(language + (country == null ? "" : '_' + country)); + } } //I18n \ No newline at end of file Index: lutinutil/src/java/org/codelutin/i18n/I18nLoader.java diff -u lutinutil/src/java/org/codelutin/i18n/I18nLoader.java:1.2 lutinutil/src/java/org/codelutin/i18n/I18nLoader.java:1.3 --- lutinutil/src/java/org/codelutin/i18n/I18nLoader.java:1.2 Sun Mar 23 06:02:24 2008 +++ lutinutil/src/java/org/codelutin/i18n/I18nLoader.java Sun Mar 23 21:08:16 2008 @@ -17,141 +17,103 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.i18n.bundle.I18nBundleManager; -import org.codelutin.util.ConverterUtil; import java.util.ArrayList; import java.util.List; import java.util.Locale; /** - * Classe responsalbe du chargement du systeme i18n. + * Classe responsable of loading of I18n system. *

- * L'invarian de ce chargeur est l'eecoding. + * The invariant of class is property {@link #encoding}. *

- * Contient les différents languages deja charges par le systeme ainsi que le language courant + * Contains the current used {@link #language} (can be null, if not set), and the list of already loaded {@link #languages}. + *

+ *

+ * Note: Init methods are package acces and should not be used alone, but within {@link I18n} class init(XXX) methods. * * @author chemit */ public class I18nLoader { - /** to use log facility, just put in your code: log.info(\"...\"); */ private static final Log log = LogFactory.getLog(I18nLoader.class); /** 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; - /** le language actuellement utilise */ protected Language language; /** le cache de languages deja charges */ protected List languages; - - public I18nLoader(String encoding, Locale defaultLocale) { + public I18nLoader(String encoding) { this.encoding = encoding; - this.defaultLocale = defaultLocale; } + /** @return the encoding used by this loader, this an invariant */ public String getEncoding() { return encoding; } - public Locale getDefaultLocale() { - return defaultLocale; - } - /** - * @return le language actuellement utilise (ou null si aucun language - * positionne ) + * @return current language loaded or null, if no language was load */ public Language getLanguage() { return language; } - /** - * Initialise la librairie - * - */ - protected void init() { - init(defaultLocale); - } - - /** - * Initialise la librairie - * - * @param newLocale la locale a charger - */ - protected void init(Locale newLocale) { - if (newLocale == null) { - init(null, null); + /** @return le cache de language avec instanciation paresseuse */ + public List getLanguages() { + if (languages == null) { + languages = new ArrayList(); } - setLanguage(newLocale); - } - - - /** - * Initialise la librairie - * - * @param language une chaine representant la langue à utiliser fr, en, ... - */ - protected void init(String language) { - init(language, null); + return languages; } /** - * Initialise la librairie + * Set a new language in loader, given a locale. Bundle entries to load in language are stored in + * bundleManager. * - * @param language une chaine representant la langue à utiliser fr, en, ... - * @param country une chaine representant le pays à utiliser FR, GB, ... + * @param locale la locale du language requis + * @param bundleManager bundle manager to used */ - protected void init(String language, String country) { - 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); - if (newLocale == null) { - newLocale = defaultLocale; + synchronized void setLanguage(Locale locale, I18nBundleManager bundleManager) { + if (log.isDebugEnabled()) { + log.debug("locale: " + locale); } - setLanguage(newLocale); - } - - public static Locale newLocale(String str) { - try { - return ConverterUtil.convert(Locale.class, str); - } catch (Exception e) { - return null; + Language result = getLanguage(locale); + if (result == null) { + result = addLanguage(locale, bundleManager); + } else { + log.debug("using cached language : " + result); } + language = result; } /** - * Positionne un nouveau language. Si celui n'existe pas déjà dans le cache, - * alors il est crée. - * - * @param locale la locale du language requis + * Close loader and release cache ofg language. + *

+ * Current language will be also clean. */ - public synchronized void setLanguage(Locale locale) { - - Language result = getLanguage(locale); - if (result == null) { - result = addLanguage(locale); - } else { - log.debug("using cached " + result); + void close() { + if (languages != null) { + log.info("nb languages loaded : " + languages.size()); + for (Language language1 : languages) { + language1.close(); + } + languages.clear(); + languages = null; } - language = result; + language = null; } /** * @param locale la locale du language recherche * @return le language trouve dans le cache, ou null. */ - public Language getLanguage(Locale locale) { + Language getLanguage(Locale locale) { if (!(languages == null || languages.isEmpty())) { for (Language language : languages) { @@ -163,30 +125,10 @@ return null; } - public void close() { - if (languages != null) { - log.info("nb languages: "+languages.size()); - for (Language language1 : languages) { - language1.close(); - } - languages.clear(); - languages = null; - } - I18nBundleManager.getInstance().close(); - } - - - /** @return le cache de language avec instanciation paresseuse */ - protected List getLanguages() { - if (languages == null) { - languages = new ArrayList(); - } - return languages; - } - - protected Language addLanguage(Locale locale) { + Language addLanguage(Locale locale, I18nBundleManager bundleManager) { Language result; - result = Language.newLanguage(locale, encoding); + result = new Language(locale, encoding); + result.load(bundleManager); if (log.isDebugEnabled()) { log.debug(result); }