[Translator] fixed inconsistency in Translator
This commit is contained in:
parent
f66e109d14
commit
0ef08f5848
@ -79,6 +79,11 @@ UPGRADE FROM 2.x to 3.0
|
||||
$route->setSchemes('https');
|
||||
```
|
||||
|
||||
### Translator
|
||||
|
||||
* The `Translator::setFallbackLocale()` method has been removed in favor of
|
||||
`Translator::setFallbackLocales()`.
|
||||
|
||||
### Twig Bridge
|
||||
|
||||
* The `render` tag is deprecated in favor of the `render` function.
|
||||
|
@ -531,7 +531,7 @@ class FrameworkExtension extends Extension
|
||||
// Use the "real" translator instead of the identity default
|
||||
$container->setAlias('translator', 'translator.default');
|
||||
$translator = $container->findDefinition('translator.default');
|
||||
$translator->addMethodCall('setFallbackLocale', array($config['fallback']));
|
||||
$translator->addMethodCall('setFallbackLocales', array($config['fallback']));
|
||||
|
||||
// Discover translation directories
|
||||
$dirs = array();
|
||||
|
@ -45,7 +45,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$translator = $this->getTranslator($this->getLoader());
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocale(array('en', 'es'));
|
||||
$translator->setFallbackLocales(array('en', 'es'));
|
||||
|
||||
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
||||
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
||||
@ -59,7 +59,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
// prime the cache
|
||||
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocale(array('en', 'es'));
|
||||
$translator->setFallbackLocales(array('en', 'es'));
|
||||
|
||||
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
||||
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
||||
@ -71,7 +71,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
|
||||
$translator->setLocale('fr');
|
||||
$translator->setFallbackLocale(array('en', 'es'));
|
||||
$translator->setFallbackLocales(array('en', 'es'));
|
||||
|
||||
$this->assertEquals('foo (FR)', $translator->trans('foo'));
|
||||
$this->assertEquals('bar (EN)', $translator->trans('bar'));
|
||||
|
@ -1,6 +1,12 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
2.3.0
|
||||
-----
|
||||
|
||||
* added Translator::getFallbackLocales()
|
||||
* deprecated Translator::setFallbackLocale() in favor of the new Translator::setFallbackLocales() method
|
||||
|
||||
2.2.0
|
||||
-----
|
||||
|
||||
|
@ -9,7 +9,7 @@ translated strings from these including support for pluralization.
|
||||
use Symfony\Component\Translation\Loader\ArrayLoader;
|
||||
|
||||
$translator = new Translator('fr_FR', new MessageSelector());
|
||||
$translator->setFallbackLocale('fr');
|
||||
$translator->setFallbackLocales(array('fr'));
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
$translator->addResource('array', array(
|
||||
'Hello World!' => 'Bonjour',
|
||||
|
@ -27,7 +27,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('fr', $translator->getLocale());
|
||||
}
|
||||
|
||||
public function testSetFallbackLocale()
|
||||
public function testSetFallbackLocales()
|
||||
{
|
||||
$translator = new Translator('en', new MessageSelector());
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
@ -37,11 +37,11 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
// force catalogue loading
|
||||
$translator->trans('bar');
|
||||
|
||||
$translator->setFallbackLocale('fr');
|
||||
$translator->setFallbackLocales(array('fr'));
|
||||
$this->assertEquals('foobar', $translator->trans('bar'));
|
||||
}
|
||||
|
||||
public function testSetFallbackLocaleMultiple()
|
||||
public function testSetFallbackLocalesMultiple()
|
||||
{
|
||||
$translator = new Translator('en', new MessageSelector());
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
@ -51,7 +51,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
// force catalogue loading
|
||||
$translator->trans('bar');
|
||||
|
||||
$translator->setFallbackLocale(array('fr_FR', 'fr'));
|
||||
$translator->setFallbackLocales(array('fr_FR', 'fr'));
|
||||
$this->assertEquals('bar (fr)', $translator->trans('bar'));
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
|
||||
$translator->addResource('array', array('bar' => 'foobar'), 'en');
|
||||
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocales(array('en'));
|
||||
|
||||
$this->assertEquals('foobar', $translator->trans('bar'));
|
||||
}
|
||||
@ -113,7 +113,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US');
|
||||
$translator->addResource('array', array('bar' => 'bar (en)'), 'en');
|
||||
|
||||
$translator->setFallbackLocale(array('en_US', 'en'));
|
||||
$translator->setFallbackLocales(array('en_US', 'en'));
|
||||
|
||||
$this->assertEquals('foo (en_US)', $translator->trans('foo'));
|
||||
$this->assertEquals('bar (en)', $translator->trans('bar'));
|
||||
@ -122,7 +122,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testTransNonExistentWithFallback()
|
||||
{
|
||||
$translator = new Translator('fr', new MessageSelector());
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocales(array('en'));
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
$this->assertEquals('non-existent', $translator->trans('non-existent'));
|
||||
}
|
||||
@ -246,7 +246,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testTransChoiceFallback()
|
||||
{
|
||||
$translator = new Translator('ru', new MessageSelector());
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocales(array('en'));
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
$translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
|
||||
|
||||
@ -256,7 +256,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testTransChoiceFallbackBis()
|
||||
{
|
||||
$translator = new Translator('ru', new MessageSelector());
|
||||
$translator->setFallbackLocale(array('en_US', 'en'));
|
||||
$translator->setFallbackLocales(array('en_US', 'en'));
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
$translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US');
|
||||
|
||||
@ -269,7 +269,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testTransChoiceFallbackWithNoTranslation()
|
||||
{
|
||||
$translator = new Translator('ru', new MessageSelector());
|
||||
$translator->setFallbackLocale('en');
|
||||
$translator->setFallbackLocales(array('en'));
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
|
||||
$this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
|
||||
|
@ -122,14 +122,40 @@ class Translator implements TranslatorInterface
|
||||
*
|
||||
* @param string|array $locales The fallback locale(s)
|
||||
*
|
||||
* @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setFallbackLocale($locales)
|
||||
{
|
||||
$this->setFallbackLocales(is_array($locales) ? $locales : array($locales))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fallback locales.
|
||||
*
|
||||
* @param array $locales The fallback locales
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setFallbackLocales(array $locales)
|
||||
{
|
||||
// needed as the fallback locales are linked to the already loaded catalogues
|
||||
$this->catalogues = array();
|
||||
|
||||
$this->fallbackLocales = is_array($locales) ? $locales : array($locales);
|
||||
$this->fallbackLocales = $locales;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fallback locales.
|
||||
*
|
||||
* @return array $locales The fallback locales
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getFallbackLocales()
|
||||
{
|
||||
return $this->fallbackLocales;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user