Skip to content

Commit

Permalink
Update Translator class to match changes in Aura.Intl 3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jan 20, 2017
1 parent ece8fe5 commit 3b4345e
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/I18n/Translator.php
Expand Up @@ -21,6 +21,7 @@
namespace Cake\I18n;

use Aura\Intl\FormatterInterface;
use Aura\Intl\Package;
use Aura\Intl\TranslatorInterface;

/**
Expand Down Expand Up @@ -52,28 +53,28 @@ class Translator implements TranslatorInterface
protected $locale;

/**
* The message keys and translations.
* The Package containing keys and translations.
*
* @var array
* @var \Aura\Intl\Package
*/
protected $messages = [];
protected $package;

/**
* Constructor
*
* @param string $locale The locale being used.
* @param array $messages The message keys and translations.
* @param \Aura\Intl\Package $package The Package containing keys and translations.
* @param \Aura\Intl\FormatterInterface $formatter A message formatter.
* @param \Aura\Intl\TranslatorInterface|null $fallback A fallback translator.
*/
public function __construct(
$locale,
array $messages,
Package $package,
FormatterInterface $formatter,
TranslatorInterface $fallback = null
) {
$this->locale = $locale;
$this->messages = $messages;
$this->package = $package;
$this->formatter = $formatter;
$this->fallback = $fallback;
}
Expand All @@ -86,17 +87,20 @@ public function __construct(
*/
protected function getMessage($key)
{
if (isset($this->messages[$key])) {
return $this->messages[$key];
$message = $this->package->getMessage($key);
if ($message) {
return $message;
}

if ($this->fallback) {
// get the message from the fallback translator
$message = $this->fallback->getMessage($key);
// speed optimization: retain locally
$this->messages[$key] = $message;
// done!
return $message;
if ($message) {
// speed optimization: retain locally
$this->package->addMessage($key, $message);
// done!
return $message;
}
}

// no local message, no fallback
Expand Down Expand Up @@ -149,4 +153,14 @@ public function translate($key, array $tokensValues = [])

return $this->formatter->format($this->locale, $message, $tokensValues);
}

/**
* An object of type Package
*
* @return \Aura\Intl\Package
*/
public function getPackage()
{
return $this->package;
}
}

0 comments on commit 3b4345e

Please sign in to comment.