From b6049beca235eece6466d6ec612b3d11cb5c788e Mon Sep 17 00:00:00 2001 From: cgonzalez Date: Thu, 24 Feb 2011 13:53:42 +0100 Subject: [PATCH 1/3] [Translation] Added search to FallbackLocale Catalogue. When the current locale catalogue doesn't contain the id searched, the code doesn't search in the fallbacklocale catalogue (as is explained in the documentation). Added the search to the fallbacklocale catalogue to the translation function. --- src/Symfony/Component/Translation/Translator.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index b4c3f69187..287245531d 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -112,6 +112,15 @@ class Translator implements TranslatorInterface $this->loadCatalogue($locale); } + if(!$this->catalogues[$locale]->has($id, $domain)) { + + $locale = $this->fallbackLocale; + + if (!isset($this->catalogues[$locale])) { + $this->loadCatalogue($locale); + } + } + return strtr($this->catalogues[$locale]->get($id, $domain), $parameters); } From 381d1e2da1410ddf8c9e2da07b4e4b8e1344adb7 Mon Sep 17 00:00:00 2001 From: cgonzalez Date: Fri, 25 Feb 2011 13:56:27 +0100 Subject: [PATCH 2/3] [Translation] Fixed the addition of the fallbackLocale catalogue to the current locale catalogue. When loading a catalogue the function "optimizeCatalogue" add the fallback catalogue to the current locale catalogue. This should be done by first adding the language catalogue and finally adding the fallbacklocale catalogue specified in the configuration. This subsequent additions are done by the "loadCatalogue" function. The problem is that in the "loadCatalogue" function exists an if statement that checks if the resource of a given locale exists before loading it. If not, the function simply returns. This return implies that the subsequent addition of the fallbacklocale wouldn't be done. This has been fixed by simply replacing the current if statement and adding a new one that, if the resource exists, then executes the process of resource loading. Finally, the function continue calling the "optimizeCatalogue" function. --- .../Component/Translation/Translator.php | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 287245531d..b11edb7d6a 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -112,15 +112,6 @@ class Translator implements TranslatorInterface $this->loadCatalogue($locale); } - if(!$this->catalogues[$locale]->has($id, $domain)) { - - $locale = $this->fallbackLocale; - - if (!isset($this->catalogues[$locale])) { - $this->loadCatalogue($locale); - } - } - return strtr($this->catalogues[$locale]->get($id, $domain), $parameters); } @@ -143,15 +134,15 @@ class Translator implements TranslatorInterface protected function loadCatalogue($locale) { $this->catalogues[$locale] = new MessageCatalogue($locale); - if (!isset($this->resources[$locale])) { - return; - } - foreach ($this->resources[$locale] as $resource) { - if (!isset($this->loaders[$resource[0]])) { - throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0])); + if (isset($this->resources[$locale])) { + + foreach ($this->resources[$locale] as $resource) { + if (!isset($this->loaders[$resource[0]])) { + throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0])); + } + $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2])); } - $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2])); } $this->optimizeCatalogue($locale); From db1ea0d2e1147631d21a87f2eb58c6e7a00334a0 Mon Sep 17 00:00:00 2001 From: Cristian Gonzalez Date: Sun, 27 Feb 2011 12:12:45 +0100 Subject: [PATCH 3/3] [Translation] Modified Translation unit test "testTransWithFallbackLocale" Now the test shows the behavior when is requested a translation to an undefined locale catalogue and, therefore, the fallbacklocale catalogue must be queried. The original test function only checks the step to reach the language catalog not to the fallbacklocale one (even the fallbacklocale isn't set). This test gives error in the current version of symfony/symfony. --- tests/Symfony/Tests/Component/Translation/TranslatorTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Symfony/Tests/Component/Translation/TranslatorTest.php b/tests/Symfony/Tests/Component/Translation/TranslatorTest.php index c45f1df90e..4cddae8a18 100644 --- a/tests/Symfony/Tests/Component/Translation/TranslatorTest.php +++ b/tests/Symfony/Tests/Component/Translation/TranslatorTest.php @@ -43,11 +43,13 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase public function testTransWithFallbackLocale() { - $translator = new Translator('en_US', new MessageSelector()); + $translator = new Translator('fr_FR', new MessageSelector()); $translator->addLoader('array', new ArrayLoader()); $translator->addResource('array', array('foo' => 'foofoo'), 'en_US'); $translator->addResource('array', array('bar' => 'foobar'), 'en'); + $translator->setFallbackLocale('en'); + $this->assertEquals('foobar', $translator->trans('bar')); }