[icinga-checkins] icinga.org: icinga-web/mhein/testing: * Added module based translation and tests (ref #1548)

git at icinga.org git at icinga.org
Thu Jun 16 09:09:32 CEST 2011


Module: icinga-web
Branch: mhein/testing
Commit: a46b9e729459117997d80c1df973206ddc713df1
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=a46b9e729459117997d80c1df973206ddc713df1

Author: Marius Hein <marius.hein at netways.de>
Date:   Wed Jun 15 15:53:56 2011 +0200

* Added module based translation and tests (ref #1548)

---

 app/config/config_handlers.xml                     |    6 ++-
 app/modules/AppKit/config/autoload.xml             |    1 +
 .../AppKitTranslationConfigHandler.class.php       |   42 ++++++++++++++++++++
 .../AppKit/lib/module/AppKitModuleUtil.class.php   |   17 ++++----
 .../translator/AppKitGettextTranslator.class.php   |    3 -
 app/modules/TestDummy/config/module.xml            |    4 ++
 app/modules/TestDummy/config/translation.xml       |   36 +++++++++++++++++
 tests/phpunit/runSuite.sh                          |   32 +++++++++++++-
 tests/phpunit/tests/module/ModuleLoadingTest.php   |   15 +++++++
 9 files changed, 141 insertions(+), 15 deletions(-)

diff --git a/app/config/config_handlers.xml b/app/config/config_handlers.xml
index e69c9e4..8edf90c 100755
--- a/app/config/config_handlers.xml
+++ b/app/config/config_handlers.xml
@@ -1,6 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0" xmlns="http://agavi.org/agavi/config/parts/config_handlers/1.0" parent="%core.system_config_dir%/config_handlers.xml">
-
 	<ae:configuration>
 		<handlers>
 		
@@ -14,6 +13,11 @@
 				<transformation>%core.agavi_dir%/config/xsl/databases.xsl</transformation>
 			</handler>
 			
+			<handler pattern="%core.config_dir%/translation.xml" class="AppKitTranslationConfigHandler">
+				<validation>%core.agavi_dir%/config/xsd/translation.xsd</validation>
+				<transformation>%core.agavi_dir%/config/xsl/translation.xsl</transformation>
+			</handler>
+			
 		</handlers>
 	</ae:configuration>
 </ae:configurations>
diff --git a/app/modules/AppKit/config/autoload.xml b/app/modules/AppKit/config/autoload.xml
index cccc1e1..b37e745 100755
--- a/app/modules/AppKit/config/autoload.xml
+++ b/app/modules/AppKit/config/autoload.xml
@@ -103,6 +103,7 @@
 		<!-- Agavi config file handler -->
 		<autoload name="AppKitRoutingConfigHandler">%core.module_dir%/AppKit/lib/config/AppKitRoutingConfigHandler.class.php</autoload>
 		<autoload name="AppKitDatabaseConfigHandler">%core.module_dir%/AppKit/lib/config/AppKitDatabaseConfigHandler.class.php</autoload>
+		<autoload name="AppKitTranslationConfigHandler">%core.module_dir%/AppKit/lib/config/AppKitTranslationConfigHandler.class.php</autoload>
 	
 	    <!-- Classes for automatic clientside api creation -->
 		<autoload name="AppKitValidatorArgumentExtractor">%core.module_dir%/AppKit/lib/config/AppKitValidatorArgumentExtractor.class.php</autoload>
diff --git a/app/modules/AppKit/lib/config/AppKitTranslationConfigHandler.class.php b/app/modules/AppKit/lib/config/AppKitTranslationConfigHandler.class.php
new file mode 100644
index 0000000..81cda10
--- /dev/null
+++ b/app/modules/AppKit/lib/config/AppKitTranslationConfigHandler.class.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * This file holds the class AppKitTranslationConfigHandler and additional
+ * ressourcen to extending translations on-the-fly
+ */
+
+/**
+ * This class add on-the-fly extension for agavi translation settings. We're using
+ * the module register mechanism to detect additional module-based translation
+ * resourcen and add them with XInclude to the main configuration
+ * @author mhein <marius.hein at netways.de>
+ * @package icinga-web
+ * @subpackage appkit
+ * @since 1.5
+ */
+class AppKitTranslationConfigHandler extends AgaviTranslationConfigHandler {
+    
+    /**
+     * (non-PHPdoc)
+     * @see AgaviTranslationConfigHandler::execute()
+     */
+    public function execute(AgaviXmlConfigDomDocument $document) {
+        
+        $document->setDefaultNamespace(parent::XML_NAMESPACE, 't');
+        
+        $entry_xpath_query = '//ae:configurations/ae:configuration/t:translators[@default_domain=\'icinga.default\']';
+        
+        AppKitXmlUtil::includeXmlFilesToTarget(
+            $document,
+            $entry_xpath_query,
+            'xmlns(ae=http://agavi.org/agavi/config/global/envelope/1.0) xmlns(t='. parent::XML_NAMESPACE. ') xpointer(//ae:configurations/ae:configuration/t:translators/t:translator)',
+            AppKitModuleUtil::getInstance()->getSubConfig('agavi.include_xml.translations')
+        );
+        
+        $document->xinclude();
+        
+        return parent::execute($document);
+    }
+    
+}
+
+?>
\ No newline at end of file
diff --git a/app/modules/AppKit/lib/module/AppKitModuleUtil.class.php b/app/modules/AppKit/lib/module/AppKitModuleUtil.class.php
index 55ebeae..e434da8 100644
--- a/app/modules/AppKit/lib/module/AppKitModuleUtil.class.php
+++ b/app/modules/AppKit/lib/module/AppKitModuleUtil.class.php
@@ -14,16 +14,17 @@ class AppKitModuleUtil extends AppKitSingleton {
     const DATA_ARRAY		= 'array';
 
     protected static $default_config_keys = array(
-            'app.javascript_files'		    => self::DATA_FLAT,
-            'app.javascript_actions'	    => self::DATA_DEFAULT,
-            'app.javascript_dynamic'	    => self::DATA_UNIQUE,
-            'app.css_files'				    => self::DATA_FLAT,
-            'app.meta_tags'				    => self::DATA_DEFAULT,
+            'app.javascript_files'		        => self::DATA_FLAT,
+            'app.javascript_actions'	        => self::DATA_DEFAULT,
+            'app.javascript_dynamic'	        => self::DATA_UNIQUE,
+            'app.css_files'				        => self::DATA_FLAT,
+            'app.meta_tags'				        => self::DATA_DEFAULT,
 
             // Namespaces for XML includes
-            'agavi.include_xml.routing'     => self::DATA_FLAT,
-            'agavi.include_xml.databases'	=> self::DATA_FLAT,
-    		'agavi.include_xml.cronks'      => self::DATA_FLAT
+            'agavi.include_xml.routing'         => self::DATA_FLAT,
+            'agavi.include_xml.databases'	    => self::DATA_FLAT,
+    		'agavi.include_xml.cronks'          => self::DATA_FLAT,
+    		'agavi.include_xml.translations'    => self::DATA_FLAT,
     );
 
     private $modules = null;
diff --git a/app/modules/AppKit/lib/translator/AppKitGettextTranslator.class.php b/app/modules/AppKit/lib/translator/AppKitGettextTranslator.class.php
index 1f03fc8..64fc495 100644
--- a/app/modules/AppKit/lib/translator/AppKitGettextTranslator.class.php
+++ b/app/modules/AppKit/lib/translator/AppKitGettextTranslator.class.php
@@ -1,12 +1,9 @@
 <?php
 
 class AppKitGettextTranslator extends AgaviGettextTranslator {
-
-
     public function getDomainPaths() {
         return $this->domainPaths;
     }
-
 }
 
 ?>
\ No newline at end of file
diff --git a/app/modules/TestDummy/config/module.xml b/app/modules/TestDummy/config/module.xml
index 8d66051..246b4aa 100644
--- a/app/modules/TestDummy/config/module.xml
+++ b/app/modules/TestDummy/config/module.xml
@@ -35,6 +35,10 @@
 					<ae:parameter>%core.module_dir%/TestDummy/config/cronks.xml</ae:parameter>
 				</setting>
 
+				<setting name="appkit_module.agavi.include_xml.translations">
+					<ae:parameter>%core.module_dir%/TestDummy/config/translation.xml</ae:parameter>
+				</setting>
+
 			</settings>
 		</module>
 		
diff --git a/app/modules/TestDummy/config/translation.xml b/app/modules/TestDummy/config/translation.xml
new file mode 100644
index 0000000..83201ec
--- /dev/null
+++ b/app/modules/TestDummy/config/translation.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ae:configurations xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0" xmlns="http://agavi.org/agavi/config/parts/translation/1.0">
+  <ae:configuration>
+  	
+		<translators>
+		
+			<translator domain="testdummy">
+			
+					<message_translator class="AgaviSimpleTranslator">
+						<ae:parameter name="text_simple">
+							<ae:parameter name="en">
+								<ae:parameter name="test1">test1-trans</ae:parameter>
+								<ae:parameter name="test2">test2-trans</ae:parameter>
+							</ae:parameter>
+						</ae:parameter>
+					</message_translator>
+					
+					<translator domain="date_year">
+						<date_formatter>
+							<ae:parameter name="type">date</ae:parameter>
+							<ae:parameter name="format">yyyy</ae:parameter>
+						</date_formatter>
+					</translator>
+					
+					<translator domain="date_month">
+						<date_formatter>
+							<ae:parameter name="type">date</ae:parameter>
+							<ae:parameter name="format">MM</ae:parameter>
+						</date_formatter>
+					</translator>
+					
+			</translator>
+		</translators>
+  	
+  </ae:configuration>
+</ae:configurations>
\ No newline at end of file
diff --git a/tests/phpunit/runSuite.sh b/tests/phpunit/runSuite.sh
index 4b23b6f..3be17cd 100755
--- a/tests/phpunit/runSuite.sh
+++ b/tests/phpunit/runSuite.sh
@@ -1,11 +1,37 @@
-#!/bin/sh
+#!/bin/bash
 PHPUNIT=$(which phpunit)
 BASE=$(readlink -f $(dirname $0))
-if [ -x ${PHPUNIT} ]
-then
+CACHE_DIR=$BASE/../../app/cache
+
+function del_cache {
+	if [ -d $CACHE_DIR/config ]; then
+		echo -n "Deleting config cache ... "
+		rm -rf $CACHE_DIR/config
+		echo "OK"
+	fi
+
+	if [ -d $CACHE_DIR/content ]; then
+		echo -n "Deleting content cache ... "
+		rm -rf $CACHE_DIR/content
+		echo "OK"
+	fi
+}
+
+del_cache
+
+echo "** Test begin **"
+echo ""
+
+if [ -x ${PHPUNIT} ]; then
 	cd $BASE
 	${PHPUNIT} --configuration=suites.xml $@
 else
 	echo "Sorry, PHPUnit not found."
 fi
 
+echo ""
+echo ""
+echo "** Test end **"
+
+del_cache
+
diff --git a/tests/phpunit/tests/module/ModuleLoadingTest.php b/tests/phpunit/tests/module/ModuleLoadingTest.php
index 33f4136..59b4152 100644
--- a/tests/phpunit/tests/module/ModuleLoadingTest.php
+++ b/tests/phpunit/tests/module/ModuleLoadingTest.php
@@ -121,4 +121,19 @@ class ModuleLoadingTest extends PHPUnit_Framework_TestCase {
         
         $this->assertFalse(array_key_exists('dummyTestCronk3', $cronks));
     }
+    
+    public function testModuleTranslations() {
+        $ctx = IcingaWebTestTool::getContext();
+        $tm = $ctx->getTranslationManager();
+        
+        $tm->setLocale('en');
+        
+        $this->assertEquals('en', $tm->getCurrentLocaleIdentifier());
+        
+        $this->assertEquals('test1-trans', $tm->_('test1', 'testdummy.text_simple'));
+        $this->assertEquals('test2-trans', $tm->_('test2', 'testdummy.text_simple'));
+        
+        $this->assertEquals(date('Y'), $tm->_d(date('Y-m-d H:i:s'), 'testdummy.date_year'));
+        $this->assertEquals(date('m'), $tm->_d(date('Y-m-d H:i:s'), 'testdummy.date_month'));
+    }
 }
\ No newline at end of file





More information about the icinga-checkins mailing list