Skip to content

Commit

Permalink
add ability to set a custom fallback translator
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-griniuk committed Aug 13, 2016
1 parent 88e2665 commit ddabfa7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/I18n/I18n.php
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions src/I18n/TranslatorRegistry.php
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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')
Expand Down
32 changes: 31 additions & 1 deletion tests/TestCase/I18n/I18nTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit ddabfa7

Please sign in to comment.