From 29bb2aca756f91a2be5c14d86a4c7239dd2b13d9 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Mon, 18 Oct 2010 14:36:17 +0200 Subject: [PATCH] Added a mock translation handler and translation factories. 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. --- .../Translation/lib/Horde/Translation/Factory.php | 43 ++++++++++++++++ .../lib/Horde/Translation/Factory/Gettext.php | 49 ++++++++++++++++++ .../lib/Horde/Translation/Factory/Mock.php | 46 +++++++++++++++++ .../Translation/lib/Horde/Translation/Mock.php | 57 ++++++++++++++++++++ framework/Translation/package.xml | 50 +++++++++++++++--- .../test/Horde/Translation/Autoload.php | 23 +++++++++ .../test/Horde/Translation/FactoryTest.php | 60 ++++++++++++++++++++++ .../test/Horde/Translation/GettextTest.php | 5 ++ .../Translation/test/Horde/Translation/phpunit.xml | 8 +++ 9 files changed, 335 insertions(+), 6 deletions(-) create mode 100644 framework/Translation/lib/Horde/Translation/Factory.php create mode 100644 framework/Translation/lib/Horde/Translation/Factory/Gettext.php create mode 100644 framework/Translation/lib/Horde/Translation/Factory/Mock.php create mode 100644 framework/Translation/lib/Horde/Translation/Mock.php create mode 100644 framework/Translation/test/Horde/Translation/Autoload.php create mode 100644 framework/Translation/test/Horde/Translation/FactoryTest.php create mode 100644 framework/Translation/test/Horde/Translation/phpunit.xml diff --git a/framework/Translation/lib/Horde/Translation/Factory.php b/framework/Translation/lib/Horde/Translation/Factory.php new file mode 100644 index 000000000..d8d128dc7 --- /dev/null +++ b/framework/Translation/lib/Horde/Translation/Factory.php @@ -0,0 +1,43 @@ + + * @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 + * @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 index 000000000..2b810470b --- /dev/null +++ b/framework/Translation/lib/Horde/Translation/Factory/Gettext.php @@ -0,0 +1,49 @@ + + * @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 + * @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 index 000000000..3f11c8877 --- /dev/null +++ b/framework/Translation/lib/Horde/Translation/Factory/Mock.php @@ -0,0 +1,46 @@ + + * @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 + * @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 index 000000000..506d13bc0 --- /dev/null +++ b/framework/Translation/lib/Horde/Translation/Mock.php @@ -0,0 +1,57 @@ + + * @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 + * @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; + } +} diff --git a/framework/Translation/package.xml b/framework/Translation/package.xml index 6ab343ecd..714b763cf 100644 --- a/framework/Translation/package.xml +++ b/framework/Translation/package.xml @@ -10,8 +10,8 @@ jan@horde.org yes - 2010-10-05 - + 2010-10-18 + 0.1.0 0.1.0 @@ -29,11 +29,37 @@ + + + + + - + + - - + + + + + + + + + + + + + + + + + + + + + + @@ -54,7 +80,19 @@ + + + + + + + + + + + + @@ -67,7 +105,7 @@ beta beta - 2010-10-05 + 2010-10-18 LGPL * Initial release. diff --git a/framework/Translation/test/Horde/Translation/Autoload.php b/framework/Translation/test/Horde/Translation/Autoload.php new file mode 100644 index 000000000..fdc7723f7 --- /dev/null +++ b/framework/Translation/test/Horde/Translation/Autoload.php @@ -0,0 +1,23 @@ + + * @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 index 000000000..2f3938cca --- /dev/null +++ b/framework/Translation/test/Horde/Translation/FactoryTest.php @@ -0,0 +1,60 @@ + + * @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 + * @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 diff --git a/framework/Translation/test/Horde/Translation/GettextTest.php b/framework/Translation/test/Horde/Translation/GettextTest.php index bc3c327a5..f79f72467 100644 --- a/framework/Translation/test/Horde/Translation/GettextTest.php +++ b/framework/Translation/test/Horde/Translation/GettextTest.php @@ -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 index 000000000..502d3c9b8 --- /dev/null +++ b/framework/Translation/test/Horde/Translation/phpunit.xml @@ -0,0 +1,8 @@ + + + + + ../../../lib + + + -- 2.11.0