Index: lutinutil/src/java/org/codelutin/i18n/LanguageManager.java diff -u lutinutil/src/java/org/codelutin/i18n/LanguageManager.java:1.2 lutinutil/src/java/org/codelutin/i18n/LanguageManager.java:1.3 --- lutinutil/src/java/org/codelutin/i18n/LanguageManager.java:1.2 Thu Feb 21 23:11:49 2008 +++ lutinutil/src/java/org/codelutin/i18n/LanguageManager.java Mon Mar 3 13:17:42 2008 @@ -1,6 +1,5 @@ /* -* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, -* Tony Chemit, Gabriel Landais +* ##% Copyright (C) 2007, 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 @@ -62,17 +61,15 @@ public synchronized static void setLanguage(Locale locale, String encoding) { Language result = getLanguage(locale, encoding); - if (result == null) { - result = addLanguage(locale, encoding); } LanguageManager.language = result; } /** - * @param locale la locale dulanguage recherche - * @param encoding l'encondign du language recherche + * @param locale la locale du language recherche + * @param encoding l'enconding du language recherche * @return le language trouve dans le cache, ou null. */ public static Language getLanguage(Locale locale, String encoding) { @@ -107,7 +104,7 @@ protected static Language addLanguage(Locale locale, String encoding) { Language result; - result = Language.valueOf(locale, encoding); + result = Language.newLanguage(locale, encoding); if (log.isDebugEnabled()) { log.debug(result); } Index: lutinutil/src/java/org/codelutin/i18n/Language.java diff -u lutinutil/src/java/org/codelutin/i18n/Language.java:1.9 lutinutil/src/java/org/codelutin/i18n/Language.java:1.10 --- lutinutil/src/java/org/codelutin/i18n/Language.java:1.9 Thu Feb 21 23:11:49 2008 +++ lutinutil/src/java/org/codelutin/i18n/Language.java Mon Mar 3 13:17:42 2008 @@ -10,137 +10,133 @@ * 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. *##%%**/ -/** +/** * Language.java */ package org.codelutin.i18n; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.i18n.bundle.I18nBundleManager; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.net.URL; import java.net.URLClassLoader; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.Properties; -import org.codelutin.util.Resource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - /** * This class is used byte the i18n class. It encapsulates the translation - * resource for one language + * resource for one language. + *

+ * The class offers a public static factory method : + * {@link #newLanguage(Locale, String)}. + *

+ * Note : You SHOULD always use the factory method, since constructor does + * not load i18n bundle resources. */ public class Language { /** to use log facility, just put in your code: log.info(\"...\"); */ private static final Log log = LogFactory.getLog(Language.class); + /** + * Instancie un nouveau language et l'initialie avec ses traductions pour + * la locale sous-jacente. + *

+ * Note : ne pas utilisé directement le constructeur, sinon la langue n'est + * pas initilisé avec les traductions des bundles. + * + * @param l la locale choisie + * @param toEncoding l'encoding choisi + * @return l'instance du language correspondant, initialisé + */ + public static Language newLanguage(Locale l, String toEncoding) { + Language language = new Language(l, toEncoding); + language.init(); + return language; + } + + public static URLClassLoader getLoader() { + ClassLoader loader = Language.class.getClassLoader(); + if (loader instanceof URLClassLoader) { + return (URLClassLoader) loader; + } + return null; + } + + /** toutes les traductions pour cette langue */ protected Properties resource; + + /** la locale de la langue */ protected Locale locale; + + /** l'encoding */ protected String encoding; - public static Language valueOf(Locale l,String toEncoding) { - return new Language(l,toEncoding); + protected Language(Locale l, String toEncoding) { + this.locale = l; + this.encoding = toEncoding; } - public Language(Locale l,String toEncoding) { - this.locale=l; - this.encoding=toEncoding; - String[] filenames = getFilenames(l); - for (String filename : filenames) { - try { - resource = new Properties(); - List urls; - ClassLoader loader = getClass().getClassLoader(); - if (loader instanceof URLClassLoader) { - urls = Resource.getURLs(filename, (URLClassLoader) loader); - } - else { - urls = Resource.getURLs(filename); - } - for (URL url : urls) { - log.info(this+" " + url); - I18nFileReader fileReader = new I18nFileReader(); - fileReader.load2(url.openStream(), toEncoding); - resource.putAll(fileReader); - } - } catch (Exception eee) { - log.warn("Unable to load language file: " + filename); - resource = null; - continue; - } - break; - } - } - - public Language(Locale l) { - this(l,"UTF-8"); - } - - /** - * Retourne le nom des fichiers à rechercher pour la traduction dans le - * bonne ordre de recherche. - * - * @param l - * la locale a utilise pour trouver le nom des fichiers - * @return un tableau de nom de fichier - */ - String[] getFilenames(Locale l) { - String prefix = ".*i18n/.+"; - String postfix = "\\.properties"; - java.util.List result = new ArrayList(); - if (!"".equals(l.getLanguage())) { - if (!"".equals(l.getCountry())) { - result.add(prefix + "-" + l.getLanguage() + "_" - + l.getCountry() + postfix); - } - result.add(prefix + "-" + l.getLanguage() + postfix); - } - result.add(prefix + postfix); - return result.toArray(new String[result.size()]); - } - - /** - * translate takes a sentence and returns its translation if found, the very - * same string otherwise. + protected Language(Locale l) { + this(l, I18n.UTF_8_ENCONDING); + } + + /** + * charge les traductions de la langue. + *

+ * recherche dans un premier temps, les urls des bundles puis les charge. + */ + protected void init() { + // make sure the bundlemanager is init + I18nBundleManager.init(); + + // load resources from bundles + resource = new Properties(); + + // get bundles for this locale + I18nBundleManager.getInstance().load(this, resource); + } + + /** + * translate takes a sentence and returns its translation if found, the very + * same string otherwise. + * * @param sentence sentence to translate * @return translated sentence */ - public String translate(String sentence) { - if (resource == null) { - recordNotFound(sentence); - return sentence; + public String translate(String sentence) { + if (resource == null) { + recordNotFound(sentence); + return sentence; } - try { - String result = resource.getProperty(sentence); - if (null != result && !"".equals(result)) - return result; + try { + String result = resource.getProperty(sentence); + if (null != result && !"".equals(result)) + return result; recordNotFound(sentence); - return sentence; - } catch (MissingResourceException eee) { - log.warn("Resource " + sentence + " unavailable"); - //Logger.getLogger("org.codelutin.i18n.Language.translate").info("Resource " + sentence + " unavailable"); - return sentence; - } catch (Exception eee) { - log.error("Unexpected error while translating : " + eee); - //Logger.getLogger("org.codelutin.i18n.Language.translate").severe("Unexpected error while translating : " + eee); return sentence; - } - } + } catch (MissingResourceException eee) { + log.warn("Resource " + sentence + " unavailable"); + return sentence; + } catch (Exception eee) { + log.error("Unexpected error while translating : " + eee); + return sentence; + } + } private void recordNotFound(String key) { if (I18n.recordFilePath != null && key != null && !"".equals(key)) { @@ -164,31 +160,32 @@ } } - /** - * untranslate takes a translated sentence and returns the original one if - * found, the very same string otherwise. + /** + * untranslate takes a translated sentence and returns the original one if + * found, the very same string otherwise. + * * @param sentence sentence to untranslate * @return untranslated sentence */ - public String untranslate(String sentence) { - if (resource == null) - return sentence; - try { - Enumeration e = resource.propertyNames(); - // Look for the given sentence through all translations - while (e.hasMoreElements()) { - String key = (String) e.nextElement(); - String translation = resource.getProperty(key); - // If found returns the corresponding key - if (sentence.equals(translation)) - return key; - } - } catch (MissingResourceException eee) { - // Well, this can't happen... - } - // No such translated sentence in our resourceBundle - return sentence; - } + public String untranslate(String sentence) { + if (resource == null) + return sentence; + try { + Enumeration e = resource.propertyNames(); + // Look for the given sentence through all translations + while (e.hasMoreElements()) { + String key = (String) e.nextElement(); + String translation = resource.getProperty(key); + // If found returns the corresponding key + if (sentence.equals(translation)) + return key; + } + } catch (MissingResourceException eee) { + // Well, this can't happen... + } + // No such translated sentence in our resourceBundle + return sentence; + } public Locale getLocale() { return locale; @@ -198,6 +195,10 @@ return encoding; } + public int size() { + return resource == null ? 0 : resource.size(); + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -215,8 +216,8 @@ return result; } - @Override + @Override public String toString() { - return "Language "; + return "Language "; } } \ No newline at end of file Index: lutinutil/src/java/org/codelutin/i18n/I18n.java diff -u lutinutil/src/java/org/codelutin/i18n/I18n.java:1.10 lutinutil/src/java/org/codelutin/i18n/I18n.java:1.11 --- lutinutil/src/java/org/codelutin/i18n/I18n.java:1.10 Thu Feb 21 23:11:49 2008 +++ lutinutil/src/java/org/codelutin/i18n/I18n.java Mon Mar 3 13:17:42 2008 @@ -23,9 +23,9 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.10 $ + * @version $Revision: 1.11 $ * - * Mise a jour: $Date: 2008-02-21 23:11:49 $ + * Mise a jour: $Date: 2008-03-03 13:17:42 $ * par : $Author: tchemit $ */ @@ -64,7 +64,7 @@ static final LocaleEnum DEFAULT_LOCALE = LocaleEnum.en; - static final String DEFAULT_ENCODING = ISO_8859_1_ENCONDING; + public static final String DEFAULT_ENCODING = ISO_8859_1_ENCONDING; /** Filtre a appliquer avant de retourner les chaines */ protected static I18nFilter filter;