Added a mock translation handler and translation factories.
authorGunnar Wrobel <p@rdus.de>
Mon, 18 Oct 2010 12:36:17 +0000 (14:36 +0200)
committerGunnar Wrobel <p@rdus.de>
Mon, 18 Oct 2010 12:49:21 +0000 (14:49 +0200)
Creating new translation handlers within the classes that use
translated texts breaks the idea of dependency injection.
Providing such classes with a factory that delivers the
translation handler on demand seems to be more flexible to me.

Horde_Translation_Factory_Gettext also distinguishes between
repository checkout and installed PEAR package. In both
situations the locale directories are placed into different
locations.

framework/Translation/lib/Horde/Translation/Factory.php [new file with mode: 0644]
framework/Translation/lib/Horde/Translation/Factory/Gettext.php [new file with mode: 0644]
framework/Translation/lib/Horde/Translation/Factory/Mock.php [new file with mode: 0644]
framework/Translation/lib/Horde/Translation/Mock.php [new file with mode: 0644]
framework/Translation/package.xml
framework/Translation/test/Horde/Translation/Autoload.php [new file with mode: 0644]
framework/Translation/test/Horde/Translation/FactoryTest.php [new file with mode: 0644]
framework/Translation/test/Horde/Translation/GettextTest.php
framework/Translation/test/Horde/Translation/phpunit.xml [new file with mode: 0644]

diff --git a/framework/Translation/lib/Horde/Translation/Factory.php b/framework/Translation/lib/Horde/Translation/Factory.php
new file mode 100644 (file)
index 0000000..d8d128d
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Interface describing a translation factory.
+ *
+ * PHP version 5
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+
+/**
+ * Interface describing a translation factory.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+interface Horde_Translation_Factory
+{
+    /**
+     * Returns a translation handler. The relative path to the domains locale
+     * data will be preferred if the absolute path indicates that it is unset.
+     *
+     * @param string $domain   The domain of the translation handler.
+     * @param string $absolute The absolute path to the locale data for
+     *                         this handler.
+     * @param string $relative The relative path to the locale data for
+     *                         this handler.
+     *
+     * @return Horde_Translation The translation handler.
+     */
+    public function createTranslation($domain, $absolute, $relative);
+}
diff --git a/framework/Translation/lib/Horde/Translation/Factory/Gettext.php b/framework/Translation/lib/Horde/Translation/Factory/Gettext.php
new file mode 100644 (file)
index 0000000..2b81047
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Allows to create the gettext based translation handlers.
+ *
+ * PHP version 5
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+
+/**
+ * Allows to create the gettext based translation handlers.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+class Horde_Translation_Factory_Gettext implements Horde_Translation_Factory
+{
+    /**
+     * Returns a translation handler. The relative path to the domains locale
+     * data will be preferred if the absolute path indicates that it is unset.
+     *
+     * @param string $domain   The domain of the translation handler.
+     * @param string $absolute The absolute path to the locale data for
+     *                         this handler.
+     * @param string $relative The relative path to the locale data for
+     *                         this handler.
+     *
+     * @return Horde_Translation The translation handler.
+     */
+    public function createTranslation($domain, $absolute, $relative)
+    {
+        return new Horde_Translation_Gettext(
+            $domain,
+            strpos($absolute, '@data_dir') === 0 ? $relative : $absolute
+        );
+    }
+}
diff --git a/framework/Translation/lib/Horde/Translation/Factory/Mock.php b/framework/Translation/lib/Horde/Translation/Factory/Mock.php
new file mode 100644 (file)
index 0000000..3f11c88
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Generates a mock translation handler.
+ *
+ * PHP version 5
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+
+/**
+ * Generates a mock translation handler.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+class Horde_Translation_Factory_Mock implements Horde_Translation_Factory
+{
+    /**
+     * Returns a translation handler. The relative path to the domains locale
+     * data will be preferred if the absolute path indicates that it is unset.
+     *
+     * @param string $domain   The domain of the translation handler.
+     * @param string $absolute The absolute path to the locale data for
+     *                         this handler.
+     * @param string $relative The relative path to the locale data for
+     *                         this handler.
+     *
+     * @return Horde_Translation The translation handler.
+     */
+    public function createTranslation($domain, $absolute, $relative)
+    {
+        return new Horde_Translation_Mock();
+    }
+}
diff --git a/framework/Translation/lib/Horde/Translation/Mock.php b/framework/Translation/lib/Horde/Translation/Mock.php
new file mode 100644 (file)
index 0000000..506d13b
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/**
+ * A mock translation that does not translate anything.
+ *
+ * PHP version 5
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+
+/**
+ * A mock translation that does not translate anything.
+ *
+ * @category  Horde
+ * @package   Translation
+ * @author    Gunnar Wrobel <wrobel@pardus.de>
+ * @license   http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link      http://pear.horde.org/index.php?package=Translation
+ */
+class Horde_Translation_Mock implements Horde_Translation
+{
+    /**
+     * Returns the translation of a message.
+     *
+     * @param string $message  The string to translate.
+     *
+     * @return string  The string translation, or the original string if no
+     *                 translation exists.
+     */
+    public function t($message)
+    {
+        return $message;
+    }
+
+    /**
+     * Returns the plural translation of a message.
+     *
+     * @param string $singular  The singular version to translate.
+     * @param string $plural    The plural version to translate.
+     * @param integer $number   The number that determines singular vs. plural.
+     *
+     * @return string  The string translation, or the original string if no
+     *                 translation exists.
+     */
+    public function ngettext($singular, $plural, $number)
+    {
+        return $number > 1 ? $plural : $singular;
+    }
+}
index 6ab343e..714b763 100644 (file)
@@ -10,8 +10,8 @@
   <email>jan@horde.org</email>
   <active>yes</active>
  </lead>
- <date>2010-10-05</date>
- <time>13:12:56</time>
+ <date>2010-10-18</date>
+ <time>11:41:56</time>
  <version>
   <release>0.1.0</release>
   <api>0.1.0</api>
    <dir name="lib">
     <dir name="Horde">
      <dir name="Translation">
+      <dir name="Factory">
+       <file name="Gettext.php" role="php" />
+       <file name="Mock.php" role="php" />
+      </dir> <!-- /lib/Horde/Translation/Factory -->
+      <file name="Factory.php" role="php" />
       <file name="Gettext.php" role="php" />
-     </dir> <!-- //lib/Horde/Translation -->
+      <file name="Mock.php" role="php" />
+     </dir> <!-- /lib/Horde/Translation -->
      <file name="Translation.php" role="php" />
-    </dir> <!-- //lib/Horde -->
-   </dir> <!-- //lib -->
+    </dir> <!-- /lib/Horde -->
+   </dir> <!-- /lib -->
+   <dir name="test">
+    <dir name="Horde">
+     <dir name="Translation">
+      <dir name="locale">
+       <dir name="de">
+        <dir name="LC_MESSAGES">
+         <file name="Horde_Other.mo" role="test" />
+         <file name="Horde_Other.po" role="test" />
+         <file name="Horde_Translation.mo" role="test" />
+         <file name="Horde_Translation.po" role="test" />
+        </dir> <!-- /test/Horde/Translation/locale/de/LC_MESSAGES -->
+       </dir> <!-- /test/Horde/Translation/locale/de -->
+      </dir> <!-- /test/Horde/Translation/locale -->
+      <file name="AllTests.php" role="test" />
+      <file name="Autoload.php" role="test" />
+      <file name="FactoryTest.php" role="test" />
+      <file name="GettextTest.php" role="test" />
+     </dir> <!-- /test/Horde/Translation -->
+    </dir> <!-- /test/Horde -->
+   </dir> <!-- /test -->
   </dir> <!-- / -->
  </contents>
  <dependencies>
  <phprelease>
   <filelist>
    <install as="Horde/Translation.php" name="lib/Horde/Translation.php" />
+   <install as="Horde/Translation/Factory.php" name="lib/Horde/Translation/Factory.php" />
    <install as="Horde/Translation/Gettext.php" name="lib/Horde/Translation/Gettext.php" />
+   <install as="Horde/Translation/Mock.php" name="lib/Horde/Translation/Mock.php" />
+   <install as="Horde/Translation/Factory/Gettext.php" name="lib/Horde/Translation/Factory/Gettext.php" />
+   <install as="Horde/Translation/Factory/Mock.php" name="lib/Horde/Translation/Factory/Mock.php" />
+   <install as="Horde/Translation/AllTests.php" name="test/Horde/Translation/AllTests.php" />
+   <install as="Horde/Translation/Autoload.php" name="test/Horde/Translation/Autoload.php" />
+   <install as="Horde/Translation/FactoryTest.php" name="test/Horde/Translation/FactoryTest.php" />
+   <install as="Horde/Translation/GettextTest.php" name="test/Horde/Translation/GettextTest.php" />
+   <install as="Horde/Translation/locale/de/LC_MESSAGES/Horde_Other.mo" name="test/Horde/Translation/locale/de/LC_MESSAGES/Horde_Other.mo" />
+   <install as="Horde/Translation/locale/de/LC_MESSAGES/Horde_Other.po" name="test/Horde/Translation/locale/de/LC_MESSAGES/Horde_Other.po" />
+   <install as="Horde/Translation/locale/de/LC_MESSAGES/Horde_Translation.mo" name="test/Horde/Translation/locale/de/LC_MESSAGES/Horde_Translation.mo" />
+   <install as="Horde/Translation/locale/de/LC_MESSAGES/Horde_Translation.po" name="test/Horde/Translation/locale/de/LC_MESSAGES/Horde_Translation.po" />
   </filelist>
  </phprelease>
  <changelog>
     <release>beta</release>
     <api>beta</api>
    </stability>
-   <date>2010-10-05</date>
+   <date>2010-10-18</date>
    <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
    <notes>
 * Initial release.
diff --git a/framework/Translation/test/Horde/Translation/Autoload.php b/framework/Translation/test/Horde/Translation/Autoload.php
new file mode 100644 (file)
index 0000000..fdc7723
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * Copyright 2009-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category   Horde
+ * @package    Translation
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link       http://pear.horde.org/index.php?package=Translation
+ */
+
+require_once 'Horde/Test/Autoload.php';
+
+/** Catch strict standards */
+error_reporting(E_ALL | E_STRICT);
diff --git a/framework/Translation/test/Horde/Translation/FactoryTest.php b/framework/Translation/test/Horde/Translation/FactoryTest.php
new file mode 100644 (file)
index 0000000..2f3938c
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Test the translation factory.
+ *
+ * PHP version 5
+ *
+ * @category   Horde
+ * @package    Translation
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link       http://pear.horde.org/index.php?package=Translation
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
+/**
+ * Test the translation factory.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category   Horde
+ * @package    Translation
+ * @subpackage UnitTests
+ * @author     Gunnar Wrobel <wrobel@pardus.de>
+ * @license    http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link       http://pear.horde.org/index.php?package=Translation
+ */
+class Horde_Translation_FactoryTest
+extends PHPUnit_Framework_TestCase
+{
+    public function testFactoryCreatesTranslationInstance()
+    {
+        $factory = new Horde_Translation_Factory_Gettext();
+        $this->assertType(
+            'Horde_Translation_Gettext',
+            $factory->createTranslation(
+                'Test',
+                '@data_dir@',
+                dirname(__FILE__) . '/locale'
+            )
+        );
+    }
+
+    public function testMockFactoryCreatesMockTranslationInstance()
+    {
+        $factory = new Horde_Translation_Factory_Mock();
+        $this->assertType(
+            'Horde_Translation_Mock',
+            $factory->createTranslation('Test', '', '')
+        );
+    }
+}
\ No newline at end of file
index bc3c327..f79f724 100644 (file)
@@ -7,6 +7,11 @@
  * @subpackage UnitTests
  */
 
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
 class Horde_Translation_GettextTest extends PHPUnit_Framework_TestCase
 {
     private $_dict;
diff --git a/framework/Translation/test/Horde/Translation/phpunit.xml b/framework/Translation/test/Horde/Translation/phpunit.xml
new file mode 100644 (file)
index 0000000..502d3c9
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit>
+  <filter>
+    <whitelist>
+      <directory suffix=".php">../../../lib</directory>
+    </whitelist>
+  </filter>
+</phpunit>