Skip to content

Commit

Permalink
Making Cache optional in I18n by allowing the instance to be cached
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 11, 2015
1 parent e6bb8bb commit 5b24ff2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/I18n/I18n.php
Expand Up @@ -17,6 +17,7 @@
use Aura\Intl\FormatterLocator;
use Aura\Intl\PackageLocator;
use Aura\Intl\TranslatorFactory;
use Cake\Cache\Cache;
use Cake\I18n\Formatter\IcuFormatter;
use Cake\I18n\Formatter\SprintfFormatter;
use Locale;
Expand Down Expand Up @@ -54,7 +55,7 @@ public static function translators()
return static::$_collection;
}

return static::$_collection = new TranslatorRegistry(
static::$_collection = new TranslatorRegistry(
new PackageLocator,
new FormatterLocator([
'sprintf' => function () {
Expand All @@ -67,6 +68,11 @@ public static function translators()
new TranslatorFactory,
static::locale()
);

if (class_exists('Cake\Cache\Cache')) {
static::$_collection->setCacher(Cache::engine('_cake_core_'));
}
return static::$_collection;
}

/**
Expand Down
70 changes: 55 additions & 15 deletions src/I18n/TranslatorRegistry.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\I18n;

use Aura\Intl\TranslatorLocator;
use Cake\Cache\Cache;
use Cake\Cache\CacheEngine;

/**
* Constructs and stores instances of translators that can be
Expand All @@ -42,6 +42,25 @@ class TranslatorRegistry extends TranslatorLocator
*/
protected $_defaultFormatter = 'default';

/**
* A CacheEngine object that is used to remember translator across
* requests.
*
* @var \Cake\Cache\CacheEngine
*/
protected $cacher;

/**
* Sets the CacheEngine instance used to remember translators across
* requests.
*
* @param \Cake\Cache\CacheEngine $cacher The cacher instance.
* @return void
*/
public function setCacher(CacheEngine $cacher) {
$this->_cacher = $cacher;
}

/**
* Gets a translator from the registry by package for a locale.
*
Expand All @@ -62,25 +81,46 @@ public function get($name, $locale = null)
$locale = $this->getLocale();
}

if (!isset($this->registry[$name][$locale])) {
$key = "translations.$name.$locale";
$translator = Cache::remember($key, function () use ($name, $locale) {
try {
return parent::get($name, $locale);
} catch (\Aura\Intl\Exception $e) {
}
if (isset($this->registry[$name][$locale])) {
return $this->registry[$name][$locale];
}

if (!isset($this->_loaders[$name])) {
$this->registerLoader($name, $this->_partialLoader());
}
if (!$this->_cacher) {
return $this->registry[$name][$locale] = $this->_getTranslator($name, $locale);
}

$key = "translations.$name.$locale";
$translator = $this->_cacher->read($key);
if (!$translator) {
$translator = $this->_getTranslator($name, $locale);
$this->_cacher->write($key, $translator);
}

return $this->_getFromLoader($name, $locale);
}, '_cake_core_');
return $this->registry[$name][$locale] = $translator;
}

/**
* Gets a translator from the registry by package for a locale.
*
* @param string $name The translator package to retrieve.
* @param string|null $locale The locale to use; if empty, uses the default
* locale.
* @return \Aura\Intl\TranslatorInterface A translator object.
* @throws \Aura\Intl\Exception If no translator with that name could be found
* for the given locale.
*/
protected function _getTranslator($name, $locale)
{
try {
return parent::get($name, $locale);
} catch (\Aura\Intl\Exception $e) {
}

return $this->registry[$name][$locale] = $translator;
if (!isset($this->_loaders[$name])) {
$this->registerLoader($name, $this->_partialLoader());
}

return $this->registry[$name][$locale];
return $this->_getFromLoader($name, $locale);
}

/**
Expand Down

0 comments on commit 5b24ff2

Please sign in to comment.