forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New class Translator\Loader\PhpMemoryArray which loads translations d…
…irectly from an already existing array as opposed to an array in a php file like \Loader\PhpArray does
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
tests/ZendTest/I18n/Translator/Loader/PhpMemoryArrayTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |