Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.3] [Translator] fixed inconsistency in Translator #7100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testTransWithoutCaching()
{
$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'));
Expand All @@ -59,7 +59,7 @@ public function testTransWithCaching()
// 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'));
Expand All @@ -71,7 +71,7 @@ public function testTransWithCaching()
$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'));
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testSetGetLocale()
$this->assertEquals('fr', $translator->getLocale());
}

public function testSetFallbackLocale()
public function testSetFallbackLocales()
{
$translator = new Translator('en', new MessageSelector());
$translator->addLoader('array', new ArrayLoader());
Expand All @@ -37,11 +37,11 @@ public function testSetFallbackLocale()
// 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());
Expand All @@ -51,7 +51,7 @@ public function testSetFallbackLocaleMultiple()
// 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'));
}

Expand All @@ -62,7 +62,7 @@ public function testTransWithFallbackLocale()
$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'));
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testTransWithFallbackLocaleTer()
$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'));
Expand All @@ -122,7 +122,7 @@ public function testTransWithFallbackLocaleTer()
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'));
}
Expand Down Expand Up @@ -246,7 +246,7 @@ public function getTransChoiceTests()
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');

Expand All @@ -256,7 +256,7 @@ public function testTransChoiceFallback()
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');

Expand All @@ -269,7 +269,7 @@ public function testTransChoiceFallbackBis()
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)));
Expand Down
28 changes: 27 additions & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,40 @@ public function getLocale()
*
* @param string|array $locales The fallback locale(s)
*
* @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should you trigger an error ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, we don't trigger errors for things that are deprecated in 3.0. We will do that but only just before 3.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. Is this documented somewhere ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*
* @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;
}

/**
Expand Down