Skip to content
Permalink
Browse files

add ability to set a custom fallback translator

  • Loading branch information...
andrej-griniuk committed Aug 13, 2016
1 parent 88e2665 commit ddabfa7abd7c07422a8d0428ba1a43f6e408534a
Showing with 65 additions and 1 deletion.
  1. +12 −0 src/I18n/I18n.php
  2. +22 −0 src/I18n/TranslatorRegistry.php
  3. +31 −1 tests/TestCase/I18n/I18nTest.php
@@ -200,6 +200,18 @@ public static function config($name, callable $loader)
static::translators()->registerLoader($name, $loader);
}
/**
* Registers a custom fallback translator loader
*
* @param callable $loader A callable object that should return a Package
* instance to be used for assembling a new translator.
* @return void
*/
public static function fallbackConfig(callable $loader)
{
static::translators()->setCustomFallbackLoader($loader);
}
/**
* Sets the default locale to use for future translator instances.
* This also affects the `intl.default_locale` PHP setting.
@@ -35,6 +35,13 @@ class TranslatorRegistry extends TranslatorLocator
*/
protected $_loaders;
/**
* Custom fallback loader
*
* @var callable
*/
protected $_customFallbackLoader;
/**
* The name of the default formatter to use for newly created
* translators from the fallback loader
@@ -146,6 +153,17 @@ public function registerLoader($name, callable $loader)
$this->_loaders[$name] = $loader;
}
/**
* Set a custom fallback loader function
*
* @param callable $loader A callable object that should return a Package
* @return void
*/
public function setCustomFallbackLoader(callable $loader)
{
$this->_customFallbackLoader = $loader;
}
/**
* Sets the name of the default messages formatter to use for future
* translator instances.
@@ -185,6 +203,10 @@ public function useFallback($enable = true)
*/
protected function _fallbackLoader($name, $locale)
{
if ($this->_customFallbackLoader) {
return ($this->_customFallbackLoader)($name, $locale);
}
$chain = new ChainMessagesLoader([
new MessagesFileLoader($name, $locale, 'mo'),
new MessagesFileLoader($name, $locale, 'po')
@@ -469,7 +469,7 @@ public function testTranslatorCache()
*
* @return void
*/
public function testloaderFactory()
public function testLoaderFactory()
{
I18n::config('custom', function ($name, $locale) {
$this->assertEquals('custom', $name);
@@ -510,6 +510,36 @@ public function testloaderFactory()
$this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
}
/**
* Tests that it is possible to register a fallback translators factory
*
* @return void
*/
public function testFallbackLoaderFactory()
{
I18n::fallbackConfig(function ($name) {
$package = new Package('default');
if ($name == 'custom') {
$package->setMessages([
'Cow' => 'Le Moo custom',
]);
} else {
$package->setMessages([
'Cow' => 'Le Moo default',
]);
}
return $package;
});
$translator = I18n::translator('custom');
$this->assertEquals('Le Moo custom', $translator->translate('Cow'));
$translator = I18n::translator();
$this->assertEquals('Le Moo default', $translator->translate('Cow'));
}
/**
* Tests that missing translations will get fallbacked to the default translator
*

0 comments on commit ddabfa7

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