From c0d981999bc85d80ec9ad8cd55bb4816b73ee356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Girard?= Date: Wed, 19 Jun 2013 15:25:31 +0200 Subject: [PATCH 1/3] fixed variable name used in translation cache --- .../Tests/Translation/TranslatorTest.php | 28 +++++++++++++++++-- .../Translation/Translator.php | 24 ++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index 1988def93158c..614f6dbc2d2c7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -45,13 +45,15 @@ public function testTransWithoutCaching() { $translator = $this->getTranslator($this->getLoader()); $translator->setLocale('fr'); - $translator->setFallbackLocales(array('en', 'es')); + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR')); $this->assertEquals('foo (FR)', $translator->trans('foo')); $this->assertEquals('bar (EN)', $translator->trans('bar')); $this->assertEquals('foobar (ES)', $translator->trans('foobar')); $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); $this->assertEquals('no translation', $translator->trans('no translation')); + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); } public function testTransWithCaching() @@ -59,25 +61,29 @@ public function testTransWithCaching() // prime the cache $translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir)); $translator->setLocale('fr'); - $translator->setFallbackLocales(array('en', 'es')); + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR')); $this->assertEquals('foo (FR)', $translator->trans('foo')); $this->assertEquals('bar (EN)', $translator->trans('bar')); $this->assertEquals('foobar (ES)', $translator->trans('foobar')); $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); $this->assertEquals('no translation', $translator->trans('no translation')); + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); // do it another time as the cache is primed now $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir)); $translator->setLocale('fr'); - $translator->setFallbackLocales(array('en', 'es')); + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR')); $this->assertEquals('foo (FR)', $translator->trans('foo')); $this->assertEquals('bar (EN)', $translator->trans('bar')); $this->assertEquals('foobar (ES)', $translator->trans('foobar')); $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); $this->assertEquals('no translation', $translator->trans('no translation')); + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); } public function testGetLocale() @@ -155,6 +161,20 @@ protected function getLoader() 'foobar' => 'foobar (ES)', )))) ; + $loader + ->expects($this->at(3)) + ->method('load') + ->will($this->returnValue($this->getCatalogue('pt-PT', array( + 'foobarfoo' => 'foobarfoo (PT-PT)', + )))) + ; + $loader + ->expects($this->at(4)) + ->method('load') + ->will($this->returnValue($this->getCatalogue('pt_BR', array( + 'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)', + )))) + ; return $loader; } @@ -183,6 +203,8 @@ public function getTranslator($loader, $options = array()) $translator->addResource('loader', 'foo', 'fr'); $translator->addResource('loader', 'foo', 'en'); $translator->addResource('loader', 'foo', 'es'); + $translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese + $translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese return $translator; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index efb40aa4a6bbc..c929d9571d04a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -98,6 +98,8 @@ protected function loadCatalogue($locale) $fallbackContent = ''; $current = ''; foreach ($this->computeFallbackLocales($locale) as $fallback) { + $fallbackSuffix = $this->getCatalogueSuffix($fallback); + $fallbackContent .= sprintf(<<addFallbackCatalogue(\$catalogue%s); @@ -105,11 +107,11 @@ protected function loadCatalogue($locale) EOF , - ucfirst($fallback), + $fallbackSuffix, $fallback, var_export($this->catalogues[$fallback]->all(), true), - ucfirst($current), - ucfirst($fallback) + $this->getCatalogueSuffix($current), + $fallbackSuffix ); $current = $fallback; } @@ -147,4 +149,20 @@ protected function initialize() } } } + + /** + * Returns a catalogue suffix from the given locale for variables used in cache. + * + * @param string $locale + * + * @return string + */ + private function getCatalogueSuffix($locale) + { + $suffix = str_replace(array('-', '_'), ' ', $locale); + $suffix = mb_convert_case($suffix, MB_CASE_TITLE); + $suffix = str_replace(' ', '', $suffix); + + return $suffix; + } } From df9672cb5a85a4bf110348c25036bca87027b4f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Girard?= Date: Thu, 20 Jun 2013 09:12:54 +0200 Subject: [PATCH 2/3] don't use function from mbstring --- src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index c929d9571d04a..82ccabcc242f3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -160,7 +160,7 @@ protected function initialize() private function getCatalogueSuffix($locale) { $suffix = str_replace(array('-', '_'), ' ', $locale); - $suffix = mb_convert_case($suffix, MB_CASE_TITLE); + $suffix = ucwords(strtolower($suffix)); $suffix = str_replace(' ', '', $suffix); return $suffix; From 573c97ce1a332efdecf41d91e4ceb286bed5562a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Girard?= Date: Mon, 24 Jun 2013 08:54:01 +0200 Subject: [PATCH 3/3] simplify transformation --- .../Translation/Translator.php | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index 82ccabcc242f3..1705c1ac06c18 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -98,7 +98,7 @@ protected function loadCatalogue($locale) $fallbackContent = ''; $current = ''; foreach ($this->computeFallbackLocales($locale) as $fallback) { - $fallbackSuffix = $this->getCatalogueSuffix($fallback); + $fallbackSuffix = ucfirst(str_replace('-', '_', $fallback)); $fallbackContent .= sprintf(<<catalogues[$fallback]->all(), true), - $this->getCatalogueSuffix($current), + ucfirst(str_replace('-', '_', $current)), $fallbackSuffix ); $current = $fallback; @@ -149,20 +149,4 @@ protected function initialize() } } } - - /** - * Returns a catalogue suffix from the given locale for variables used in cache. - * - * @param string $locale - * - * @return string - */ - private function getCatalogueSuffix($locale) - { - $suffix = str_replace(array('-', '_'), ' ', $locale); - $suffix = ucwords(strtolower($suffix)); - $suffix = str_replace(' ', '', $suffix); - - return $suffix; - } }