Skip to content

Commit

Permalink
Starting to implement translations caching
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 27, 2014
1 parent db6db46 commit 7b61c06
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/I18n/I18n.php
Expand Up @@ -55,7 +55,7 @@ public static function translators() {
return static::$_collection;
}

return static::$_collection = new TranslatorLocator(
return static::$_collection = new TranslatorRegistry(
new PackageLocator,
new FormatterLocator([
'sprintf' => function() {
Expand Down
33 changes: 33 additions & 0 deletions src/I18n/TranslatorRegistry.php
@@ -0,0 +1,33 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n;

use Aura\Intl\TranslatorLocator;
use Serializable;

/**
*
*/
class TranslatorRegistry extends TranslatorLocator implements Serializable {

public function serialize() {
return serialize($this->registry);
}

public function unserialize($data) {
$this->registry = unserialize($data);
}

}
22 changes: 22 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -265,4 +265,26 @@ public function testBasicContextFunction() {
);
}

public function testTranslatorsCache() {
I18n::translator('custom', 'en_US', function() {
$package = new Package();
$package->setMessages([
'Cow' => 'Le moo',
'Foo' => 'Bar'
]);
return $package;
});

$default = I18n::translator();
$this->assertEquals('%d is 1 (po translated)', $default->translate('%d = 1'));

$translators = I18n::translators();
$serialized = serialize($translators);
$restored = unserialize($serialized);

$this->assertEquals(
$restored->get('default', 'en_US')->translate('%d = 1'),
$default->translate('%d = 1')
);
}
}

0 comments on commit 7b61c06

Please sign in to comment.