← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9694: refactored i18n* to allow for loading with package name only (old version would strip of end of c...

 

------------------------------------------------------------
revno: 9694
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-02-03 14:37:12 +0700
message:
  refactored i18n* to allow for loading with package name only (old version would strip of end of clazzName, which means that packages could not be used directly. Also only Class<?> was allowed, not strings).
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18n.java
  dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/DefaultI18nManager.java
  dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nManager.java
  dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/DefaultResourceBundleManager.java
  dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/ResourceBundleManager.java
  dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/util/PathUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18n.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18n.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/I18n.java	2013-02-03 07:37:12 +0000
@@ -59,22 +59,22 @@
 
     /**
      * Get a translated String for a given key for the currently selected locale
-     * 
+     *
      * @param key the key for a given translation
-     * @return a translated String for a given key, or the key if no translation 
-     *  is found
+     * @return a translated String for a given key, or the key if no translation
+     *         is found
      */
     public String getString( String key )
     {
         String translation = key;
-        
+
         if ( specificResourceBundle != null )
         {
             try
             {
                 translation = specificResourceBundle.getString( key );
             }
-            catch ( MissingResourceException e )
+            catch ( MissingResourceException ignored )
             {
             }
         }
@@ -85,11 +85,11 @@
             {
                 translation = globalResourceBundle.getString( key );
             }
-            catch ( MissingResourceException e )
+            catch ( MissingResourceException ignored )
             {
             }
         }
-        
+
         return translation;
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/DefaultI18nManager.java'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/DefaultI18nManager.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/DefaultI18nManager.java	2013-02-03 07:37:12 +0000
@@ -27,13 +27,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.Locale;
-import java.util.ResourceBundle;
-
 import org.hisp.dhis.i18n.locale.LocaleManager;
 import org.hisp.dhis.i18n.resourcebundle.ResourceBundleManager;
 import org.hisp.dhis.i18n.resourcebundle.ResourceBundleManagerException;
 
+import java.util.Locale;
+import java.util.ResourceBundle;
+
 /**
  * @author Pham Thi Thuy
  * @author Nguyen Dang Quang
@@ -67,7 +67,20 @@
     public I18n getI18n( Class<?> clazz )
         throws I18nManagerException
     {
-        return new I18n( getGlobalResourceBundle(), getSpecificResourceBundle( clazz ) );
+        return new I18n( getGlobalResourceBundle(), getSpecificResourceBundle( clazz.getName() ) );
+    }
+
+    /**
+     * Used to send a specific clazzName (path) to the resource bundler.
+     *
+     * @param clazzName Path to use for getting i18n prop file
+     * @return I18n object
+     * @throws I18nManagerException
+     */
+    @Override
+    public I18n getI18n( String clazzName ) throws I18nManagerException
+    {
+        return new I18n( getGlobalResourceBundle(), getSpecificResourceBundle( clazzName ) );
     }
 
     public I18nFormat getI18nFormat()
@@ -97,6 +110,11 @@
         }
     }
 
+    private ResourceBundle getSpecificResourceBundle( String clazzName )
+    {
+        return resourceBundleManager.getSpecificResourceBundle( clazzName, getCurrentLocale() );
+    }
+
     private ResourceBundle getSpecificResourceBundle( Class<?> clazz )
     {
         return resourceBundleManager.getSpecificResourceBundle( clazz, getCurrentLocale() );

=== modified file 'dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nManager.java'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nManager.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/I18nManager.java	2013-02-03 07:37:12 +0000
@@ -38,5 +38,7 @@
 
     I18n getI18n( Class<?> clazz ) throws I18nManagerException;
 
+    I18n getI18n( String clazzName ) throws I18nManagerException;
+
     I18nFormat getI18nFormat() throws I18nManagerException;
 }

=== modified file 'dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/DefaultResourceBundleManager.java'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/DefaultResourceBundleManager.java	2012-03-19 14:33:10 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/DefaultResourceBundleManager.java	2013-02-03 07:37:12 +0000
@@ -27,6 +27,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.i18n.locale.LocaleManager;
+import org.hisp.dhis.i18n.util.PathUtils;
+
 import java.io.File;
 import java.io.FilenameFilter;
 import java.io.IOException;
@@ -45,9 +48,6 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.hisp.dhis.i18n.locale.LocaleManager;
-import org.hisp.dhis.i18n.util.PathUtils;
-
 /**
  * @author Torgeir Lorange Ostby
  * @author Pham Thi Thuy
@@ -82,11 +82,17 @@
     // ResourceBundleManager implementation
     // -------------------------------------------------------------------------
 
+    @Override
     public ResourceBundle getSpecificResourceBundle( Class<?> clazz, Locale locale )
     {
-        String path = PathUtils.getClassPath( clazz.getName() );
-
-        for ( String dir = PathUtils.getParent( path ); dir != null; dir = PathUtils.getParent( dir ) )
+        return getSpecificResourceBundle( clazz.getName(), locale );
+    }
+
+    public ResourceBundle getSpecificResourceBundle( String clazzName, Locale locale )
+    {
+        String path = PathUtils.getClassPath( clazzName );
+
+        for ( String dir = path; dir != null; dir = PathUtils.getParent( dir ) )
         {
             String baseName = PathUtils.addChild( dir, specificResourceBundleName );
 
@@ -96,6 +102,7 @@
             }
             catch ( MissingResourceException e )
             {
+                System.err.println( "MissingResource: " + e.getMessage() );
             }
         }
 

=== modified file 'dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/ResourceBundleManager.java'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/ResourceBundleManager.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/resourcebundle/ResourceBundleManager.java	2013-02-03 07:37:12 +0000
@@ -42,6 +42,8 @@
 
     ResourceBundle getSpecificResourceBundle( Class<?> clazz, Locale locale );
 
+    ResourceBundle getSpecificResourceBundle( String clazzName, Locale locale );
+
     ResourceBundle getGlobalResourceBundle( Locale locale ) throws ResourceBundleManagerException;
 
     Collection<Locale> getAvailableLocales() throws ResourceBundleManagerException;

=== modified file 'dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/util/PathUtils.java'
--- dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/util/PathUtils.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-services/dhis-service-i18n/src/main/java/org/hisp/dhis/i18n/util/PathUtils.java	2013-02-03 07:37:12 +0000
@@ -41,9 +41,14 @@
     //
     // -------------------------------------------------------------------------
 
-    public static String getClassPath( String packagE )
-    {
-        return packagE.replace( PACKAGE_SEPARATOR, FILE_SEPARATOR );
+    public static String getClassPath( Class<?> clazz )
+    {
+        return clazz.getName().replace( PACKAGE_SEPARATOR, FILE_SEPARATOR );
+    }
+
+    public static String getClassPath( String clazzName )
+    {
+        return clazzName.replace( PACKAGE_SEPARATOR, FILE_SEPARATOR );
     }
 
     public static String getParent( String path )