Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'feature/remote-translation-loader' of https://github.co…

…m/DASPRiD/zf2

Conflicts:
	README.md
  • Loading branch information...
commit 8ecd6628af387d514a61ee61c858a07a77f44fba 2 parents 15d9c28 + 3befeac
@weierophinney weierophinney authored
View
8 README.md
@@ -25,6 +25,14 @@ XX August 2012
- You can now use concrete hydrator instances with the factory.
- You may now set the CSRF validator class and/or options to use on the Csrf
element
+ - Zend\I18n\Translator
+ - Loader\LoaderInterface was splitted into Loader\FileLoaderInterface and
+ Loader\RemoteLoaderInterface. The latter one will be used in ZF 2.1 for
+ a database loader.
+ - Translator::addTranslationPattern() and the option "translation_patterns"
+ were renamed to Translator::addTranslationFilePattern and
+ "translation_file_patterns".
+ - A new method Translator::addRemoteTranslations() was added.
- Zend\Mvc
- Application no longer defines the "application" identifier for its composed
EventManager instance. If you had listeners listening on that context,
View
8 ...y/Zend/I18n/Translator/Loader/LoaderInterface.php → ...nd/I18n/Translator/Loader/FileLoaderInterface.php
@@ -11,20 +11,20 @@
namespace Zend\I18n\Translator\Loader;
/**
- * Loader interface.
+ * File loader interface.
*
* @category Zend
* @package Zend_I18n
* @subpackage Translator
*/
-interface LoaderInterface
+interface FileLoaderInterface
{
/**
* Load translations from a file.
*
- * @param string $filename
* @param string $locale
+ * @param string $filename
* @return \Zend\I18n\Translator\TextDomain|null
*/
- public function load($filename, $locale);
+ public function load($locale, $filename);
}
View
10 library/Zend/I18n/Translator/Loader/Gettext.php
@@ -22,7 +22,7 @@
* @package Zend_I18n
* @subpackage Translator
*/
-class Gettext implements LoaderInterface
+class Gettext implements FileLoaderInterface
{
/**
* Current file pointer.
@@ -39,15 +39,15 @@ class Gettext implements LoaderInterface
protected $littleEndian;
/**
- * load(): defined by LoaderInterface.
+ * load(): defined by FileLoaderInterface.
*
- * @see LoaderInterface::load()
- * @param string $filename
+ * @see FileLoaderInterface::load()
* @param string $locale
+ * @param string $filename
* @return TextDomain
* @throws Exception\InvalidArgumentException
*/
- public function load($filename, $locale)
+ public function load($locale, $filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(sprintf(
View
10 library/Zend/I18n/Translator/Loader/PhpArray.php
@@ -21,18 +21,18 @@
* @package Zend_I18n
* @subpackage Translator
*/
-class PhpArray implements LoaderInterface
+class PhpArray implements FileLoaderInterface
{
/**
- * load(): defined by LoaderInterface.
+ * load(): defined by FileLoaderInterface.
*
- * @see LoaderInterface::load()
- * @param string $filename
+ * @see FileLoaderInterface::load()
* @param string $locale
+ * @param string $filename
* @return TextDomain|null
* @throws Exception\InvalidArgumentException
*/
- public function load($filename, $locale)
+ public function load($locale, $filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(sprintf(
View
30 library/Zend/I18n/Translator/Loader/RemoteLoaderInterface.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_I18n
+ */
+
+namespace Zend\I18n\Translator\Loader;
+
+/**
+ * Remote loader interface.
+ *
+ * @category Zend
+ * @package Zend_I18n
+ * @subpackage Translator
+ */
+interface RemoteLoaderInterface
+{
+ /**
+ * Load translations from a remote source.
+ *
+ * @param string $locale
+ * @param string $textDomain
+ * @return \Zend\I18n\Translator\TextDomain|null
+ */
+ public function load($locale, $textDomain);
+}
View
17 library/Zend/I18n/Translator/LoaderPluginManager.php
@@ -16,9 +16,9 @@
/**
* Plugin manager implementation for translation loaders.
*
- * Enforces that filters retrieved are either callbacks or instances of
- * Loader\LoaderInterface. Additionally, it registers a number of default
- * loaders.
+ * Enforces that loaders retrieved are either instances of
+ * Loader\FileLoaderInterface or Loader\RemoteLoaderInterface. Additionally,
+ * it registers a number of default loaders.
*
* @category Zend
* @package Zend_I18n
@@ -27,7 +27,7 @@
class LoaderPluginManager extends AbstractPluginManager
{
/**
- * Default set of loaders
+ * Default set of loaders.
*
* @var array
*/
@@ -37,9 +37,10 @@ class LoaderPluginManager extends AbstractPluginManager
);
/**
- * Validate the plugin
+ * Validate the plugin.
*
- * Checks that the filter loaded is an instance of Loader\LoaderInterface.
+ * Checks that the filter loaded is an instance of
+ * Loader\FileLoaderInterface or Loader\RemoteLoaderInterface.
*
* @param mixed $plugin
* @return void
@@ -47,13 +48,13 @@ class LoaderPluginManager extends AbstractPluginManager
*/
public function validatePlugin($plugin)
{
- if ($plugin instanceof Loader\LoaderInterface) {
+ if ($plugin instanceof Loader\FileLoaderInterface || $plugin instanceof Loader\RemoteLoaderInterface) {
// we're okay
return;
}
throw new Exception\RuntimeException(sprintf(
- 'Plugin of type %s is invalid; must implement %s\Loader\LoaderInterface',
+ 'Plugin of type %s is invalid; must implement %s\Loader\FileLoaderInterface or %s\Loader\RemoteLoaderInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
View
106 library/Zend/I18n/Translator/Translator.php
@@ -15,6 +15,8 @@
use Zend\Cache;
use Zend\Cache\Storage\StorageInterface as CacheStorage;
use Zend\I18n\Exception;
+use Zend\I18n\Translator\Loader\FileLoaderInterface;
+use Zend\I18n\Translator\Loader\RemoteLoaderInterface;
use Zend\Stdlib\ArrayUtils;
/**
@@ -48,6 +50,13 @@ class Translator
protected $patterns = array();
/**
+ * Remote locations for loading messages.
+ *
+ * @var array
+ */
+ protected $remote = array();
+
+ /**
* Default locale.
*
* @var string
@@ -105,16 +114,16 @@ public static function factory($options)
}
}
- // patterns
- if (isset($options['translation_patterns'])) {
- if (!is_array($options['translation_patterns'])) {
+ // file patterns
+ if (isset($options['translation_file_patterns'])) {
+ if (!is_array($options['translation_file_patterns'])) {
throw new Exception\InvalidArgumentException(
'"translation_patterns" should be an array'
);
}
$requiredKeys = array('type', 'base_dir', 'pattern');
- foreach ($options['translation_patterns'] as $pattern) {
+ foreach ($options['translation_file_patterns'] as $pattern) {
foreach ($requiredKeys as $key) {
if (!isset($pattern[$key])) {
throw new Exception\InvalidArgumentException(
@@ -159,6 +168,31 @@ public static function factory($options)
}
}
+ // remote
+ if (isset($options['remote_translation'])) {
+ if (!is_array($options['remote_translation'])) {
+ throw new Exception\InvalidArgumentException(
+ '"remote_translation" should be an array'
+ );
+ }
+
+ $requiredKeys = array('type');
+ foreach ($options['remote_translation'] as $remote) {
+ foreach ($requiredKeys as $key) {
+ if (!isset($remote[$key])) {
+ throw new Exception\InvalidArgumentException(
+ "'{$key}' is missing for remote translation options"
+ );
+ }
+ }
+
+ $translator->addRemoteTranslations(
+ $remote['type'],
+ isset($remote['text_domain']) ? $remote['text_domain'] : 'default'
+ );
+ }
+ }
+
// cache
if (isset($options['cache'])) {
if ($options['cache'] instanceof CacheStorage) {
@@ -403,7 +437,7 @@ public function addTranslationFile(
}
/**
- * Add multiple translations with a pattern.
+ * Add multiple translations with a file pattern.
*
* @param string $type
* @param string $baseDir
@@ -411,7 +445,7 @@ public function addTranslationFile(
* @param string $textDomain
* @return Translator
*/
- public function addTranslationPattern(
+ public function addTranslationFilePattern(
$type,
$baseDir,
$pattern,
@@ -431,6 +465,24 @@ public function addTranslationPattern(
}
/**
+ * Add remote translations.
+ *
+ * @param string $type
+ * @param string $textDomain
+ * @return Translator
+ */
+ public function addRemoteTranslations($type, $textDomain = 'default')
+ {
+ if (!isset($this->remote[$textDomain])) {
+ $this->remote[$textDomain] = array();
+ }
+
+ $this->remote[$textDomain][] = $type;
+
+ return $this;
+ }
+
+ /**
* Load messages for a given language and domain.
*
* @param string $textDomain
@@ -452,15 +504,34 @@ protected function loadMessages($textDomain, $locale)
}
}
+ // Try to load from remote sources
+ if (isset($this->remote[$textDomain])) {
+ foreach ($this->remote[$textDomain] as $loaderType) {
+ $loader = $this->getPluginManager()->get($loaderType);
+
+ if (!$loader instanceof RemoteLoaderInterface) {
+ throw new Exception\RuntimeException('Specified loader is not a remote loader');
+ }
+
+ $this->messages[$textDomain][$locale] = $loader->load($locale, $textDomain);
+ return;
+ }
+ }
+
// Try to load from pattern
if (isset($this->patterns[$textDomain])) {
foreach ($this->patterns[$textDomain] as $pattern) {
- $filename = $pattern['baseDir']
- . '/' . sprintf($pattern['pattern'], $locale);
+ $filename = $pattern['baseDir'] . '/' . sprintf($pattern['pattern'], $locale);
+
if (is_file($filename)) {
- $this->messages[$textDomain][$locale] = $this->getPluginManager()
- ->get($pattern['type'])
- ->load($filename, $locale);
+ $loader = $this->getPluginManager()->get($pattern['type']);
+
+ if (!$loader instanceof FileLoaderInterface) {
+ throw new Exception\RuntimeException('Specified loader is not a file loader');
+ }
+
+ $this->messages[$textDomain][$locale] = $loader->load($locale, $filename);
+ return;
}
}
}
@@ -471,12 +542,17 @@ protected function loadMessages($textDomain, $locale)
continue;
}
- $file = $this->files[$textDomain][$currentLocale];
- $this->messages[$textDomain][$locale] = $this->getPluginManager()
- ->get($file['type'])
- ->load($file['filename'], $locale);
+ $file = $this->files[$textDomain][$currentLocale];
+ $loader = $this->getPluginManager()->get($file['type']);
+
+ if (!$loader instanceof FileLoaderInterface) {
+ throw new Exception\RuntimeException('Specified loader is not a file loader');
+ }
+
+ $this->messages[$textDomain][$locale] = $loader->load($locale, $file['filename']);
unset($this->files[$textDomain][$currentLocale]);
+ return;
}
// Cache the loaded text domain
View
13 tests/ZendTest/I18n/Translator/Loader/GettextTest.php
@@ -12,7 +12,6 @@
use PHPUnit_Framework_TestCase as TestCase;
use Locale;
-use Zend\I18n\Translator\Translator;
use Zend\I18n\Translator\Loader\Gettext as GettextLoader;
class GettextTest extends TestCase
@@ -37,7 +36,7 @@ public function testLoaderFailsToLoadMissingFile()
{
$loader = new GettextLoader();
$this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException', 'Could not open file');
- $loader->load('missing', 'en_EN');
+ $loader->load('en_EN', 'missing');
}
public function testLoaderFailsToLoadBadFile()
@@ -45,27 +44,27 @@ public function testLoaderFailsToLoadBadFile()
$loader = new GettextLoader();
$this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException',
'is not a valid gettext file');
- $loader->load($this->testFilesDir . '/failed.mo', 'en_EN');
+ $loader->load('en_EN', $this->testFilesDir . '/failed.mo');
}
public function testLoaderLoadsEmptyFile()
{
$loader = new GettextLoader();
- $domain = $loader->load($this->testFilesDir . '/translation_empty.mo', 'en_EN');
+ $domain = $loader->load('en_EN', $this->testFilesDir . '/translation_empty.mo');
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $domain);
}
public function testLoaderLoadsBigEndianFile()
{
$loader = new GettextLoader();
- $domain = $loader->load($this->testFilesDir . '/translation_bigendian.mo', 'en_EN');
+ $domain = $loader->load('en_EN', $this->testFilesDir . '/translation_bigendian.mo');
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $domain);
}
public function testLoaderReturnsValidTextDomain()
{
$loader = new GettextLoader();
- $textDomain = $loader->load($this->testFilesDir . '/translation_en.mo', 'en_EN');
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en.mo');
$this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
$this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
@@ -74,7 +73,7 @@ public function testLoaderReturnsValidTextDomain()
public function testLoaderLoadsPluralRules()
{
$loader = new GettextLoader();
- $textDomain = $loader->load($this->testFilesDir . '/translation_en.mo', 'en_EN');
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en.mo');
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $textDomain->getPluralRule()->evaluate(1));
View
11 tests/ZendTest/I18n/Translator/Loader/PhpArrayTest.php
@@ -12,7 +12,6 @@
use PHPUnit_Framework_TestCase as TestCase;
use Locale;
-use Zend\I18n\Translator\Translator;
use Zend\I18n\Translator\Loader\PhpArray as PhpArrayLoader;
class PhpArrayTest extends TestCase
@@ -37,7 +36,7 @@ public function testLoaderFailsToLoadMissingFile()
{
$loader = new PhpArrayLoader();
$this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException', 'Could not open file');
- $loader->load('missing', 'en_EN');
+ $loader->load('en_EN', 'missing');
}
public function testLoaderFailsToLoadNonArray()
@@ -45,20 +44,20 @@ public function testLoaderFailsToLoadNonArray()
$loader = new PhpArrayLoader();
$this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException',
'Expected an array, but received');
- $loader->load($this->testFilesDir . '/failed.php', 'en_EN');
+ $loader->load('en_EN', $this->testFilesDir . '/failed.php');
}
public function testLoaderLoadsEmptyArray()
{
$loader = new PhpArrayLoader();
- $textDomain = $loader->load($this->testFilesDir . '/translation_empty.php', 'en_EN');
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_empty.php');
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $textDomain);
}
public function testLoaderReturnsValidTextDomain()
{
$loader = new PhpArrayLoader();
- $textDomain = $loader->load($this->testFilesDir . '/translation_en.php', 'en_EN');
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en.php');
$this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
$this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
@@ -67,7 +66,7 @@ public function testLoaderReturnsValidTextDomain()
public function testLoaderLoadsPluralRules()
{
$loader = new PhpArrayLoader();
- $textDomain = $loader->load($this->testFilesDir . '/translation_en.php', 'en_EN');
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en.php');
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $textDomain->getPluralRule()->evaluate(1));
View
4 tests/ZendTest/I18n/Translator/TestAsset/Loader.php
@@ -10,7 +10,7 @@
namespace ZendTest\I18n\Translator\TestAsset;
-use Zend\I18n\Translator\Loader\LoaderInterface;
+use Zend\I18n\Translator\Loader\FileLoaderInterface;
/**
* Test loader.
@@ -19,7 +19,7 @@
* @package ZendTest_I18n
* @subpackage Translator
*/
-class Loader implements LoaderInterface
+class Loader implements FileLoaderInterface
{
public $textDomain;
View
2  tests/ZendTest/I18n/Validator/FloatTest.php
@@ -32,7 +32,7 @@ class FloatTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->locale = Locale::getDefault();
- $this->validator = new FloatValidator();
+ $this->validator = new FloatValidator(array('locale' => 'en'));
}
public function tearDown()
View
2  tests/ZendTest/Validator/TestAsset/ArrayTranslator.php
@@ -12,7 +12,7 @@
use Zend\I18n\Translator;
-class ArrayTranslator implements Translator\Loader\LoaderInterface
+class ArrayTranslator implements Translator\Loader\FileLoaderInterface
{
public $translations;
View
2  tests/ZendTest/View/Helper/TestAsset/ArrayTranslator.php
@@ -12,7 +12,7 @@
use Zend\I18n\Translator;
-class ArrayTranslator implements Translator\Loader\LoaderInterface
+class ArrayTranslator implements Translator\Loader\FileLoaderInterface
{
public $translations;
Please sign in to comment.
Something went wrong with that request. Please try again.