Skip to content
Permalink
Browse files

add i18n::useTranslationFallback()

Refs #6812
  • Loading branch information...
Schlaefer committed Jun 15, 2015
1 parent 8b49355 commit 99b5c7d9e29ee28e110701646ff3d9107d963dc5
Showing with 72 additions and 38 deletions.
  1. +11 −0 src/I18n/I18n.php
  2. +31 −19 src/I18n/TranslatorRegistry.php
  3. +30 −19 tests/TestCase/I18n/I18nTest.php
@@ -266,6 +266,17 @@ public static function defaultFormatter($name = null)
return static::translators()->defaultFormatter($name);
}
/**
* Set if the default domain fallback is used.
*
* @param bool $enable flag to enable or disable fallback
* @return void
*/
public static function useTranslationFallback($enable = true)
{
static::translators()->useTranslationFallback($enable);
}
/**
* Destroys all translator instances and creates a new empty translations
* collection.
@@ -43,16 +43,19 @@ class TranslatorRegistry extends TranslatorLocator
protected $_defaultFormatter = 'default';
/**
* Domain used used as fallback if translation isn't found in specified domain.
* Domain used as fallback if translation isn't found in original domain
*
* - true: 'default'
* - false: disable translation fallback
* - string: fallback domain name
*
* @var string|bool
* @var string
*/
protected $_defaultFallbackDomain = 'default';
/**
* Enable translation fallback
*
* @var bool
*/
protected $_fallback = true;
/**
* A CacheEngine object that is used to remember translator across
* requests.
@@ -183,6 +186,17 @@ public function defaultFallbackDomain($name = null)
return $this->_defaultFallbackDomain = $name;
}
/**
* Set if the default domain fallback is used.
*
* @param bool $enable flag to enable or disable fallback
* @return void
*/
public function useTranslationFallback($enable = true)
{
$this->_fallback = $enable;
}
/**
* Returns a new translator instance for the given name and locale
* based of conventions.
@@ -248,27 +262,25 @@ protected function _getFromLoader($name, $locale)
}
/**
* set lookup fallback for loader
* Set domain fallback for loader.
*
* @param string $name The name of the fallback domain
* @param callable $loader invokable loader
* @return callable loader
*/
public function setLoaderFallback($name, callable $loader)
{
if ($name !== $this->_defaultFallbackDomain) {
$loader = function () use ($loader) {
$package = $loader();
if ($this->_defaultFallbackDomain !== false && !$package->getFallback()) {
$fallback = $this->_defaultFallbackDomain;
if ($fallback === true) {
$fallback = 'default';
}
$package->setFallback($fallback);
}
return $package;
};
if (!$this->_fallback || $name === $this->_defaultFallbackDomain) {
return $loader;
}
$loader = function () use ($loader) {
$package = $loader();
if (!$package->getFallback()) {
$package->setFallback($this->_defaultFallbackDomain);
}
return $package;
};
return $loader;
}
}
@@ -514,48 +514,59 @@ public function testFallbackTranslator()
}
/**
* Test that the fallback translators can be disabled
* Test that the translation fallback can be disabled
*
* @return void
*/
public function testFallbackTranslatorFallbackDisabled()
public function testFallbackTranslatorDisabled()
{
I18n::defaultFallbackDomain(false);
I18n::translator('default', 'en_US', function () {
I18n::useTranslationFallback(false);
I18n::translator('default', 'fr_FR', function () {
$package = new Package('default');
$package->setMessages(['Dog' => 'bark']);
$package->setMessages(['Dog' => 'Le bark']);
return $package;
});
I18n::translator('custom', 'en_US', function () {
I18n::translator('custom', 'fr_FR', function () {
$package = new Package('default');
$package->setMessages([]);
$package->setMessages(['Cow' => 'Le moo']);
return $package;
});
$result = __d('custom', 'Dog');
$this->assertEquals('Dog', $result);
$translator = I18n::translator('custom', 'fr_FR');
$this->assertEquals('Le moo', $translator->translate('Cow'));
$this->assertEquals('Dog', $translator->translate('Dog'));
}
/**
* Test that an arbitrary fallback domain can be set
* Test that a different fallback domain can be set
*
* @return void
*/
public function testFallbackTranslatorCustomDomain()
public function testFallbackTranslatorSetFallbackDomain()
{
$customDomain = 'foo';
I18n::defaultFallbackDomain($customDomain);
I18n::translator($customDomain, 'en_US', function () {
$this->assertEquals('default', I18n::defaultFallbackDomain());
$fallbackDomain = 'foo';
I18n::defaultFallbackDomain($fallbackDomain);
$this->assertEquals($fallbackDomain, I18n::defaultFallbackDomain());
I18n::translator($fallbackDomain, 'fr_FR', function () {
$package = new Package('default');
$package->setMessages(['Dog' => 'bark']);
$package->setMessages(['Dog' => 'Le bark']);
return $package;
});
I18n::translator('custom', 'en_US', function () {
I18n::translator('custom', 'fr_FR', function () {
$package = new Package('default');
$package->setMessages([]);
$package->setMessages(['Cow' => 'Le moo']);
return $package;
});
$result = __d('custom', 'Dog');
$this->assertEquals('bark', $result);
$translator = I18n::translator('custom', 'fr_FR');
$this->assertEquals('Le moo', $translator->translate('Cow'));
$this->assertEquals('Le bark', $translator->translate('Dog'));
}
/**

0 comments on commit 99b5c7d

Please sign in to comment.
You can’t perform that action at this time.