← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18268: i18n-db translation for web-api, wip

 

------------------------------------------------------------
revno: 18268
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-02-16 10:09:23 +0700
message:
  i18n-db translation for web-api, wip
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/I18nService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Options.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.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-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nService.java	2015-02-16 03:09:23 +0000
@@ -35,7 +35,14 @@
 import org.hisp.dhis.translation.TranslationService;
 import org.hisp.dhis.user.UserSettingService;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
 
 import static org.hisp.dhis.system.util.ReflectionUtils.*;
 
@@ -55,7 +62,7 @@
     {
         this.translationService = translationService;
     }
-    
+
     private I18nLocaleService localeService;
 
     public void setLocaleService( I18nLocaleService localeService )
@@ -69,7 +76,7 @@
     {
         this.userSettingService = userSettingService;
     }
-    
+
     // -------------------------------------------------------------------------
     // Internationalise
     // -------------------------------------------------------------------------
@@ -161,7 +168,7 @@
         {
             return null;
         }
-        
+
         List<String> properties = getObjectPropertyNames( object );
 
         Map<String, String> translations = new HashMap<>();
@@ -181,12 +188,12 @@
         {
             return null;
         }
-        
+
         if ( !(object instanceof IdentifiableObject) )
         {
             throw new IllegalArgumentException( "I18n object must be identifiable: " + object );
         }
-        
+
         if ( object instanceof DataElement )
         {
             return Arrays.asList( DataElement.I18N_PROPERTIES );
@@ -318,7 +325,7 @@
     /**
      * Returns a map for a collection of Translations where the key is the
      * translation property and the value is the translation value.
-     * 
+     *
      * @param translations the Collection of translations.
      * @return Map containing translations.
      */

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/I18nService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/I18nService.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/I18nService.java	2015-02-16 03:09:23 +0000
@@ -78,7 +78,7 @@
     // -------------------------------------------------------------------------
 
     Locale getCurrentLocale();
-    
+
     boolean currentLocaleIsBase();
     
     List<Locale> getAvailableLocales();

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Options.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Options.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/Options.java	2015-02-16 03:09:23 +0000
@@ -28,10 +28,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.springframework.util.StringUtils;
+
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 /**
@@ -136,6 +139,10 @@
 
     protected boolean assumeTrue;
 
+    protected boolean translate;
+
+    protected String locale;
+
     //--------------------------------------------------------------------------
     // Constructors
     //--------------------------------------------------------------------------
@@ -144,6 +151,8 @@
     {
         this.options = options;
         this.assumeTrue = options.get( "assumeTrue" ) == null || options.get( "assumeTrue" ).equalsIgnoreCase( "true" );
+        this.locale = options.get( "locale" );
+        this.translate = isEnabled( "translate" ) || !StringUtils.isEmpty( locale );
     }
 
     public Options()
@@ -248,6 +257,21 @@
         this.assumeTrue = assumeTrue;
     }
 
+    public boolean isTranslate()
+    {
+        return translate;
+    }
+
+    public boolean defaultLocale()
+    {
+        return StringUtils.isEmpty( locale );
+    }
+
+    public Locale getLocale()
+    {
+        return Locale.forLanguageTag( locale );
+    }
+
     //--------------------------------------------------------------------------
     // Getters for standard options
     //--------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-02-10 09:04:29 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-02-16 03:09:23 +0000
@@ -42,11 +42,13 @@
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.metadata.ImportService;
 import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
+import org.hisp.dhis.dxf2.metadata.Options;
 import org.hisp.dhis.dxf2.objectfilter.ObjectFilterService;
 import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
+import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.node.Node;
 import org.hisp.dhis.node.NodeUtils;
@@ -125,6 +127,9 @@
     @Autowired
     protected ContextService contextService;
 
+    @Autowired
+    protected I18nService i18nService;
+
     //--------------------------------------------------------------------------
     // GET
     //--------------------------------------------------------------------------
@@ -148,11 +153,11 @@
 
         boolean hasPaging = options.hasPaging();
 
-        List<T> entityList;
+        List<T> entities;
 
         if ( filters.isEmpty() )
         {
-            entityList = getEntityList( metaData, options, filters );
+            entities = getEntityList( metaData, options, filters );
             hasPaging = false;
         }
         else
@@ -186,12 +191,12 @@
                     Pager pager = new Pager( options.getPage(), count, options.getPageSize() );
                     metaData.setPager( pager );
 
-                    entityList = Lists.newArrayList( manager.getBetweenLikeName( getEntityClass(), name, pager.getOffset(), pager.getPageSize() ) );
+                    entities = Lists.newArrayList( manager.getBetweenLikeName( getEntityClass(), name, pager.getOffset(), pager.getPageSize() ) );
                     hasPaging = false;
                 }
                 else
                 {
-                    entityList = Lists.newArrayList( manager.getLikeName( getEntityClass(), name ) );
+                    entities = Lists.newArrayList( manager.getLikeName( getEntityClass(), name ) );
                 }
             }
             else
@@ -207,29 +212,30 @@
                     }
                 }
 
-                entityList = getEntityList( metaData, options, filters );
+                entities = getEntityList( metaData, options, filters );
             }
         }
 
         Pager pager = metaData.getPager();
 
-        entityList = objectFilterService.filter( entityList, filters );
+        entities = objectFilterService.filter( entities, filters );
+        translate( entities, options );
 
         if ( hasPaging )
         {
-            pager = new Pager( options.getPage(), entityList.size(), options.getPageSize() );
-            entityList = PagerUtils.pageCollection( entityList, pager );
+            pager = new Pager( options.getPage(), entities.size(), options.getPageSize() );
+            entities = PagerUtils.pageCollection( entities, pager );
         }
 
-        postProcessEntities( entityList );
-        postProcessEntities( entityList, options, rpParameters );
+        postProcessEntities( entities );
+        postProcessEntities( entities, options, rpParameters );
 
         if ( fields.contains( "access" ) )
         {
             options.getOptions().put( "viewClass", "sharing" );
         }
 
-        handleLinksAndAccess( options, entityList, false );
+        handleLinksAndAccess( options, entities, false );
 
         linkService.generatePagerLinks( pager, getEntityClass() );
 
@@ -241,7 +247,7 @@
             rootNode.addChild( NodeUtils.createPager( pager ) );
         }
 
-        rootNode.addChild( fieldFilterService.filter( getEntityClass(), entityList, fields ) );
+        rootNode.addChild( fieldFilterService.filter( getEntityClass(), entities, fields ) );
 
         return rootNode;
     }
@@ -328,11 +334,27 @@
         serialize( request, response, summary );
     }
 
+    protected void translate( List<T> entities, Options options )
+    {
+        if ( options.isTranslate() )
+        {
+            if ( options.defaultLocale() )
+            {
+                i18nService.internationalise( entities );
+            }
+            else
+            {
+                i18nService.internationalise( entities, options.getLocale() );
+            }
+        }
+    }
+
     private RootNode getObjectInternal( String uid, Map<String, String> parameters,
         List<String> filters, List<String> fields ) throws Exception
     {
         WebOptions options = new WebOptions( parameters );
         List<T> entities = getEntity( uid, options );
+        translate( entities, options );
 
         if ( entities.isEmpty() )
         {