← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12443: Impl I18nLocalePopulator, which will populate the default locales when none exist

 

------------------------------------------------------------
revno: 12443
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-10-06 12:34:59 +0200
message:
  Impl I18nLocalePopulator, which will populate the default locales when none exist
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/I18nLocalePopulator.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/locale/I18nLocale.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nLocaleService.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/i18n/locale/hibernate/I18nLocale.hbm.xml
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/security/login.js


--
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/locale/I18nLocale.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/locale/I18nLocale.java	2013-10-06 07:45:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/i18n/locale/I18nLocale.java	2013-10-06 10:34:59 +0000
@@ -28,8 +28,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Locale;
+
 import org.hisp.dhis.common.BaseIdentifiableObject;
 
+/**
+ * Wrapper for java.util.Locale for persistence purposes.
+ * 
+ * @author larshelg
+ */
 public class I18nLocale
     extends BaseIdentifiableObject
 {    
@@ -45,12 +52,12 @@
         this.locale = "en_GB";
     }
 
-    public I18nLocale( String name, String locale )
+    public I18nLocale( Locale locale )
     {
-        this.name = name;
-        this.locale = locale;
+        this.name = locale.getDisplayName();
+        this.locale = locale.toString();
     }
-
+    
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nLocaleService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nLocaleService.java	2013-10-06 07:45:05 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/i18n/DefaultI18nLocaleService.java	2013-10-06 10:34:59 +0000
@@ -128,13 +128,12 @@
             return false; // Country not valid
         }
         
-        String loc = LocaleUtils.getLocaleString( language, country, null );
-        
-        String name = new Locale( language, country ).toString();
-        
-        I18nLocale locale = new I18nLocale( name, loc );
-        
-        saveI18nLocale( locale );
+        String localeStr = LocaleUtils.getLocaleString( language, country, null );
+        Locale locale = LocaleUtils.getLocale( localeStr );
+        
+        I18nLocale i18nLocale = new I18nLocale( locale );
+        
+        saveI18nLocale( i18nLocale );
         
         return true;
     }

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/I18nLocalePopulator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/I18nLocalePopulator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/I18nLocalePopulator.java	2013-10-06 10:34:59 +0000
@@ -0,0 +1,77 @@
+package org.hisp.dhis.startup;
+
+/*
+ * Copyright (c) 2004-2013, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.i18n.I18nLocaleService;
+import org.hisp.dhis.i18n.locale.I18nLocale;
+import org.hisp.dhis.system.startup.AbstractStartupRoutine;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * Populates default I18nLocales if none exists.
+ * 
+ * @author Lars Helge Overland
+ */
+public class I18nLocalePopulator
+    extends AbstractStartupRoutine
+{
+    private static final Log log = LogFactory.getLog( I18nLocalePopulator.class );
+    
+    @Autowired
+    private I18nLocaleService localeService;
+    
+    private static final List<String> DEFAULT_LOCALES = Arrays.asList( 
+        "af","ar","bi","am","de","dz","en","es","fa","fr","gu","hi","id","it",
+        "km","lo","my","ne","nl","no","ps","pt","ru","rw","sw","tg","vi","zh" );
+    
+    @Override
+    public void execute()
+        throws Exception
+    {
+        int count = localeService.getI18nLocaleCount();
+        
+        if ( count > 0 )
+        {
+            return;
+        }
+        
+        for ( String locale : DEFAULT_LOCALES )
+        {
+            localeService.saveI18nLocale( new I18nLocale( new Locale( locale ) ) );
+        }
+
+        log.info( "Populated default locales" );
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2013-10-06 07:45:05 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2013-10-06 10:34:59 +0000
@@ -867,6 +867,11 @@
     <property name="skipInTests" value="true" />
   </bean>
   
+  <bean id="org.hisp.dhis.startup.I18nLocalePopulator" class="org.hisp.dhis.startup.I18nLocalePopulator">
+    <property name="runlevel" value="7" />
+    <property name="skipInTests" value="true" />
+  </bean>
+  
   <!--<bean id="org.hisp.dhis.user.UserDefaultPopulator" class="org.hisp.dhis.user.UserDefaultPopulator">
     <property name="userService" ref="org.hisp.dhis.user.UserService" />
     <property name="runlevel" value="5" />
@@ -889,6 +894,7 @@
           <ref local="org.hisp.dhis.dataentryform.DataEntryFormUpgrader" />
           <ref local="org.hisp.dhis.startup.ExpressionUpgrader" />
           <ref local="org.hisp.dhis.startup.ConfigurationPopulator" />
+          <ref local="org.hisp.dhis.startup.I18nLocalePopulator" />
           <!--<ref local="org.hisp.dhis.user.UserDefaultPopulator" />-->
         </list>
       </list>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/i18n/locale/hibernate/I18nLocale.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/i18n/locale/hibernate/I18nLocale.hbm.xml	2013-10-01 16:44:42 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/i18n/locale/hibernate/I18nLocale.hbm.xml	2013-10-06 10:34:59 +0000
@@ -17,7 +17,5 @@
 
     <property name="locale" column="locale" length="15" not-null="true" />
 
-	<!-- Form properties -->
-
   </class>
 </hibernate-mapping>

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/security/login.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/security/login.js	2013-10-06 10:15:09 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/security/login.js	2013-10-06 10:34:59 +0000
@@ -16,7 +16,7 @@
     
     var locale = $.cookie( login.localeCookie );
     
-    if ( undefined !== locale && locale != null )
+    if ( undefined !== locale && locale )
     {
     	login.changeLocale( locale );
     	$( '#localeSelect option[value="' + locale + '"]' ).attr( 'selected', 'selected' );