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

Skip to content

Commit 1a0b38a

Browse files
committed
merged branch cedric-g/patch_translator (PR #8310)
This PR was submitted for the 2.3 branch but it was merged into the 2.2 branch instead (closes #8310). Discussion ---------- [FrameworkBundle] Fixed variable name used in translation cache This simply fixes the `$catalogueXXX` variable name used in the translation cache files in case the user use locales such as `en-US`, the generated variable's name was `$catalogueEn-Us`, with this fix it will be `$catalogueEnUs`. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7824 | License | MIT | Doc PR | - Commits ------- e50399c [FrameworkBundle] Fixed variable name used in translation cache
2 parents c875d0a + b272419 commit 1a0b38a

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,45 @@ public function testTransWithoutCaching()
4545
{
4646
$translator = $this->getTranslator($this->getLoader());
4747
$translator->setLocale('fr');
48-
$translator->setFallbackLocale(array('en', 'es'));
48+
$translator->setFallbackLocale(array('en', 'es', 'pt-PT', 'pt_BR'));
4949

5050
$this->assertEquals('foo (FR)', $translator->trans('foo'));
5151
$this->assertEquals('bar (EN)', $translator->trans('bar'));
5252
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
5353
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
5454
$this->assertEquals('no translation', $translator->trans('no translation'));
55+
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
56+
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
5557
}
5658

5759
public function testTransWithCaching()
5860
{
5961
// prime the cache
6062
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
6163
$translator->setLocale('fr');
62-
$translator->setFallbackLocale(array('en', 'es'));
64+
$translator->setFallbackLocale(array('en', 'es', 'pt-PT', 'pt_BR'));
6365

6466
$this->assertEquals('foo (FR)', $translator->trans('foo'));
6567
$this->assertEquals('bar (EN)', $translator->trans('bar'));
6668
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
6769
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
6870
$this->assertEquals('no translation', $translator->trans('no translation'));
71+
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
72+
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
6973

7074
// do it another time as the cache is primed now
7175
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
7276
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
7377
$translator->setLocale('fr');
74-
$translator->setFallbackLocale(array('en', 'es'));
78+
$translator->setFallbackLocale(array('en', 'es', 'pt-PT', 'pt_BR'));
7579

7680
$this->assertEquals('foo (FR)', $translator->trans('foo'));
7781
$this->assertEquals('bar (EN)', $translator->trans('bar'));
7882
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
7983
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
8084
$this->assertEquals('no translation', $translator->trans('no translation'));
85+
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
86+
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
8187
}
8288

8389
public function testGetLocale()
@@ -155,6 +161,20 @@ protected function getLoader()
155161
'foobar' => 'foobar (ES)',
156162
))))
157163
;
164+
$loader
165+
->expects($this->at(3))
166+
->method('load')
167+
->will($this->returnValue($this->getCatalogue('pt-PT', array(
168+
'foobarfoo' => 'foobarfoo (PT-PT)',
169+
))))
170+
;
171+
$loader
172+
->expects($this->at(4))
173+
->method('load')
174+
->will($this->returnValue($this->getCatalogue('pt_BR', array(
175+
'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)',
176+
))))
177+
;
158178

159179
return $loader;
160180
}
@@ -183,6 +203,8 @@ public function getTranslator($loader, $options = array())
183203
$translator->addResource('loader', 'foo', 'fr');
184204
$translator->addResource('loader', 'foo', 'en');
185205
$translator->addResource('loader', 'foo', 'es');
206+
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
207+
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
186208

187209
return $translator;
188210
}

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,20 @@ protected function loadCatalogue($locale)
9898
$fallbackContent = '';
9999
$current = '';
100100
foreach ($this->computeFallbackLocales($locale) as $fallback) {
101+
$fallbackSuffix = ucfirst(str_replace('-', '_', $fallback));
102+
101103
$fallbackContent .= sprintf(<<<EOF
102104
\$catalogue%s = new MessageCatalogue('%s', %s);
103105
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
104106
105107
106108
EOF
107109
,
108-
ucfirst($fallback),
110+
$fallbackSuffix,
109111
$fallback,
110112
var_export($this->catalogues[$fallback]->all(), true),
111-
ucfirst($current),
112-
ucfirst($fallback)
113+
ucfirst(str_replace('-', '_', $current)),
114+
$fallbackSuffix
113115
);
114116
$current = $fallback;
115117
}

0 commit comments

Comments
 (0)