Author: fred
Date: 2007-07-21 11:55:27 +0000 (Sat, 21 Jul 2007)
New Revision: 14224

Modified:
   trunk/plugins/Echo/build.xml
   trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java
Log:
Translate in the default language when the key is not found in the selected 
language

Modified: trunk/plugins/Echo/build.xml
===================================================================
--- trunk/plugins/Echo/build.xml        2007-07-21 11:35:59 UTC (rev 14223)
+++ trunk/plugins/Echo/build.xml        2007-07-21 11:55:27 UTC (rev 14224)
@@ -29,6 +29,7 @@
                <tstamp/>
                <mkdir dir="${build.dir}"/>
                <mkdir dir="${build.dir}/xml" />
+               <mkdir dir="${build.dir}/i18n" />
                <mkdir dir="${dist.dir}"/>
                <mkdir dir="${javadoc.dir}"/>
        </target>
@@ -48,6 +49,13 @@
                <copy todir="${build.dir}/xml/">
                        <fileset dir="${src.dir}/xml/" />
                </copy>
+
+               <copy todir="${build.dir}/i18n">
+                       <fileset dir="${src.dir}/plugins/echo/i18n/">
+                               <include name="*.properties" />
+                       </fileset>
+               </copy>
+               
                <copy todir="${build.dir}">
                        <fileset dir="${src.dir}/resources/" />
                        <fileset dir="${images.dir}" />

Modified: trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java  2007-07-21 11:35:59 UTC 
(rev 14223)
+++ trunk/plugins/Echo/src/plugins/echo/i18n/I18n.java  2007-07-21 11:55:27 UTC 
(rev 14224)
@@ -17,9 +17,14 @@
 public class I18n {

        public static final String[] AVAILABLE_LANGUAGES = { "en", "fr" };
+       public static final String DEFAULT_LANGUAGE = "en"; 
+       public static final String PREFIX = "echo.i18n.";
+       public static final String SUFFIX = ".properties";

+       private String selectedLanguage;
        private Properties translation;
-       
+       private Properties defaultTranslation = null;
+
        public I18n(String language) throws FileNotFoundException, IOException {

                translation = new Properties();
@@ -32,21 +37,39 @@
        *       @param language the ISO code of the language 
        */
        public void setLanguage(String language) throws FileNotFoundException, 
IOException {
+               
+               translation.load(getClass().getResourceAsStream("/i18n/" + 
PREFIX + language + SUFFIX));
+               
+               if(! language.equals(DEFAULT_LANGUAGE)) {
+                       if(defaultTranslation == null)
+                               defaultTranslation = new Properties();
+                       
defaultTranslation.load(getClass().getResourceAsStream("/i18n/" + PREFIX + 
DEFAULT_LANGUAGE + SUFFIX));
+               }
+               selectedLanguage = language;

-               // TODO : getRessourceAsStream()
-               translation.load(new 
FileInputStream("/home/fred/prog/soc/trunk/plugins/Echo/src/plugins/echo/i18n/echo.i18n."
 + language + ".properties"));
-       
        }

        /**
        *       Return the translation of the key       
        *       @param key
-       *       @return the translated String or null if the key dont exists    
        
+       *       @return the translated String in the selected language, the 
translated String in the default language or the key itself if the key is not 
found in the default language         
        */
        public String getString(String key) {

-               return translation.getProperty(key);
-       
+               String str = translation.getProperty(key);
+               if(str != null)
+                       return str;
+               else {
+                       if(selectedLanguage.equals(DEFAULT_LANGUAGE))
+                               return key;
+                       
+                       str = defaultTranslation.getProperty(key);
+                       if(str != null)
+                               return str;
+                       else
+                               return key;
+               }       
+               
        }

        /**


Reply via email to