Skip to content

Commit

Permalink
New class Translator\Loader\PhpMemoryArray which loads translations d…
Browse files Browse the repository at this point in the history
…irectly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does
  • Loading branch information
poisa committed Feb 13, 2014
1 parent a28d82b commit 04e28ef
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
53 changes: 53 additions & 0 deletions library/Zend/I18n/Translator/Loader/PhpMemoryArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\I18n\Translator\Loader;

use Zend\I18n\Exception;
use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\I18n\Translator\TextDomain;

/**
* PHP array loader.
*/
class PhpMemoryArray extends AbstractFileLoader
{
/**
* load(): defined by FileLoaderInterface.
*
* @see FileLoaderInterface::load()
* @param string $locale
* @param array $messages
* @return TextDomain|null
* @throws Exception\InvalidArgumentException
*/
public function load($locale, $messages)
{
if (!is_array($messages)) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected an array, but received %s',
gettype($messages)
));
}

$textDomain = new TextDomain($messages);

if (array_key_exists('', $textDomain)) {
if (isset($textDomain['']['plural_forms'])) {
$textDomain->setPluralRule(
PluralRule::fromString($textDomain['']['plural_forms'])
);
}

unset($textDomain['']);
}

return $textDomain;
}
}
75 changes: 75 additions & 0 deletions tests/ZendTest/I18n/Translator/Loader/PhpMemoryArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\I18n\Translator\Loader;

use PHPUnit_Framework_TestCase as TestCase;
use Locale;
use Zend\I18n\Translator\Loader\PhpmemoryArray as PhpMemoryArrayLoader;

class PhpMemoryArrayTest extends TestCase
{
protected $testFilesDir;
protected $originalLocale;
protected $originalIncludePath;

public function setUp()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('ext/intl not enabled');
}

$this->originalLocale = Locale::getDefault();
Locale::setDefault('en_EN');

$this->testFilesDir = realpath(__DIR__ . '/../_files');
}

public function tearDown()
{
if (extension_loaded('intl')) {
Locale::setDefault($this->originalLocale);
}
}

public function testLoaderFailsToLoadNonArray()
{
$loader = new PhpMemoryArrayLoader();
$this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException',
'Expected an array, but received');
$loader->load('en_EN', 'foo');
}

public function testLoaderLoadsEmptyArray()
{
$loader = new PhpMemoryArrayLoader();
$textDomain = $loader->load('en_EN', include $this->testFilesDir . '/translation_empty.php');
$this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $textDomain);
}

public function testLoaderReturnsValidTextDomain()
{
$loader = new PhpMemoryArrayLoader();
$textDomain = $loader->load('en_EN', include $this->testFilesDir . '/translation_en.php');

$this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
$this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
}

public function testLoaderLoadsPluralRules()
{
$loader = new PhpMemoryArrayLoader();
$textDomain = $loader->load('en_EN', include $this->testFilesDir . '/translation_en.php');

$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(0));
$this->assertEquals(0, $textDomain->getPluralRule()->evaluate(1));
$this->assertEquals(1, $textDomain->getPluralRule()->evaluate(2));
$this->assertEquals(2, $textDomain->getPluralRule()->evaluate(10));
}
}

0 comments on commit 04e28ef

Please sign in to comment.