8000 [Translation][Cache] Removed the options from the arguments of Transl… · symfony/symfony@30fed6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 30fed6a

Browse files
committed
[Translation][Cache] Removed the options from the arguments of Translator
Fixed phpdoc Aligned variables and description Removed enableCache and added cache setup in constructor Added tests for locales with . and @ with caching
1 parent 8b2d9a8 commit 30fed6a

File tree

4 files changed

+111
-36
lin 8000 es changed

4 files changed

+111
-36
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class Translator extends BaseTranslator
2525
protected $container;
2626
protected $loaderIds;
2727

28+
protected $options = array(
29+
'cache_dir' => null,
30+
'debug' => false,
31+
);
32+
2833
/**
2934
* Constructor.
3035
*
@@ -45,7 +50,14 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
4550
$this->container = $container;
4651
$this->loaderIds = $loaderIds;
4752

48-
parent::__construct(null, $selector, $options);
53+
// check option names
54+
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
55+
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
56+
}
57+
58+
$this->options = array_merge($this->options, $options);
59+
60+
parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']);
4961
}
5062

5163
/**

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CHANGELOG
33

44
2.6.0
55
-----
6+
67
* added possibility to cache catalogues
78

89
2.5.0

src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testTransWithoutCaching()
5454
{
5555
$translator = $this->getTranslator($this->getLoader());
5656
$translator->setLocale('fr');
57-
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR'));
57+
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
5858

5959
$this->assertEquals('foo (FR)', $translator->trans('foo'));
6060
$this->assertEquals('bar (EN)', $translator->trans('bar'));
@@ -63,14 +63,16 @@ public function testTransWithoutCaching()
6363
$this->assertEquals('no translation', $translator->trans('no translation'));
6464
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
6565
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
66+
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
67+
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
6668
}
6769

6870
public function testTransWithCaching()
6971
{
7072
// prime the cache
71-
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
73+
$translator = $this->getTranslator($this->getLoader(), $this->tmpDir);
7274
$translator->setLocale('fr');
73-
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR'));
75+
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
7476

7577
$this->assertEquals('foo (FR)', $translator->trans('foo'));
7678
$this->assertEquals('bar (EN)', $translator->trans('bar'));
@@ -79,12 +81,14 @@ public function testTransWithCaching()
7981
$this->assertEquals('no translation', $translator->trans('no translation'));
8082
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
8183
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
84+
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
85+
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
8286

8387
// do it another time as the cache is primed now
8488
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
85-
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
89+
$translator = $this->getTranslator($loader, $this->tmpDir);
8690
$translator->setLocale('fr');
87-
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR'));
91+
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
8892

8993
$this->assertEquals('foo (FR)', $translator->trans('foo'));
9094
$this->assertEquals('bar (EN)', $translator->trans('bar'));
@@ -93,6 +97,36 @@ public function testTransWithCaching()
9397
$this->assertEquals('no translation', $translator->trans('no translation'));
9498
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
9599
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
100+
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
101+
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
102+
}
103+
104+
public function testTransWithCachingWithInvalidLocale()
105+
{
106+
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
107+
$translator = $this->getTranslator($loader, $this->tmpDir, 'Symfony\Component\Translation\Tests\TranslatorWithInvalidLocale');
108+
109+
$translator->setLocale('invalid locale');
110+
111+
try {
112+
$translator->trans('foo');
113+
$this->fail();
114+
} catch (\InvalidArgumentException $e) {
115+
$this->assertFalse(file_exists($this->tmpDir.'/catalogue.invalid locale.php'));
116+
}
117+
}
118+
119+
public function testLoadCatalogueWithCachingWithInvalidLocale()
120+
{
121+
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
122+
$translator = $this->getTranslator($loader, $this->tmpDir, 'Symfony\Component\Translation\Tests\TranslatorWithInvalidLocale');
123+
124+
try {
125+
$translator->proxyLoadCatalogue('invalid locale');
126+
$this->fail();
127+
} catch (\InvalidArgumentException $e) {
128+
$this->assertFalse(file_exists($this->tmpDir.'/catalogue.invalid locale.php'));
129+
}
96130
}
97131

98132
protected function getCatalogue($locale, $messages)
@@ -145,25 +179,53 @@ protected function getLoader()
145179
'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)',
146180
))))
147181
;
182+
$loader
183+
->expects($this->at(5))
184+
->method('load')
185+
->will($this->returnValue($this->getCatalogue('fr.UTF-8', array(
186+
'foobarbaz' => 'foobarbaz (fr.UTF-8)',
187+
))))
188+
;
189+
$loader
190+
->expects($this->at(6))
191+
->method('load')
192+
->will($this->returnValue($this->getCatalogue('sr@latin', array(
193+
'foobarbax' => 'foobarbax (sr@latin)',
194+
))))
195+
;
148196

149197
return $loader;
150198
}
151199

152-
public function getTranslator($loader, $options = array())
200+
public function getTranslator($loader, $cacheDir = null, $translatorClass = '\Symfony\Component\Translation\Translator')
153201
{
154-
$translator = new Translator(
155-
$loader,
156-
new MessageSelector(),
157-
$options
158-
);
202+
$translator = new $translatorClass('fr', new MessageSelector(), $cacheDir);
159203

160204
$translator->addLoader('loader', $loader);
161205
$translator->addResource('loader', 'foo', 'fr');
162206
$translator->addResource('loader', 'foo', 'en');
163207
$translator->addResource('loader', 'foo', 'es');
164208
$translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese
165209
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
210+
$translator->addResource('loader', 'foo', 'fr.UTF-8');
211+
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
166212

167213
return $translator;
168214
}
169215
}
216+
217+
class TranslatorWithInvalidLocale extends Translator
218+
{
219+
/**
220+
* {@inheritdoc}
221+
*/
222+
public function setLocale($locale)
223+
{
224+
$this->locale = $locale;
225+
}
226+
227+
public function proxyLoadCatalogue($locale)
228+
{
229+
$this->loadCatalogue($locale);
230+
}
231+
}

src/Symfony/Component/Translation/Translator.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ class Translator implements TranslatorInterface
3434
*/
3535
protected $locale;
3636

37-
/**
38-
* @var array
39-
*/
40-
protected $options = array(
41-
'cache_dir' => null,
42-
'debug' => false,
43-
);
44-
4537
/**
4638
* @var array
4739
*/
@@ -62,28 +54,34 @@ class Translator implements TranslatorInterface
6254
*/
6355
private $selector;
6456

57+
/**
58+
* @var string
59+
*/
60+
private $cacheDir;
61+
62+
/**
63+
* @var bool
64+
*/
65+
private $debug;
66+
6567
/**
6668
* Constructor.
6769
*
6870
* @param string $locale The locale
6971
* @param MessageSelector|null $selector The message selector for pluralization
70-
* @param array $options An array of options
72+
* @param string|null $cacheDir The directory to use for the cache
73+
* @param bool $debug Use cache in debug mode ?
7174
*
7275
* @throws \InvalidArgumentException If a locale contains invalid characters
7376
*
7477
* @api
7578
*/
76-
public function __construct($locale, MessageSelector $selector = null, array $options = array())
79+
public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false)
7780
{
7881
$this->setLocale($locale);
7982
$this->selector = $selector ?: new MessageSelector();
80-
81-
// check option names
82-
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
83-
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
84-
}
85-
86-
$this->options = array_merge($this->options, $options);
83+
$this->cacheDir = $cacheDir;
84+
$this->debug = $debug;
8785
}
8886

8987
/**
@@ -304,7 +302,7 @@ public function getMessages($locale = null)
304302
*/
305303
protected function loadCatalogue($locale)
306304
{
307-
if (null === $this->options['cache_dir']) {
305+
if (null === $this->cacheDir) {
308306
$this->initializeCatalogue($locale);
309307
} else {
310308
$this->initializeCacheCatalogue($locale);
@@ -316,6 +314,8 @@ protected function loadCatalogue($locale)
316314
*/
317315
protected function initializeCatalogue($locale)
318316
{
317+
$this->assertValidLocale($locale);
318+
319319
try {
320320
$this->doLoadCatalogue($locale);
321321
} catch (NotFoundResourceException $e) {
@@ -331,21 +331,21 @@ protected function initializeCatalogue($locale)
331331
*/
332332
private function initializeCacheCatalogue($locale)
333333
{
334+
334335
if (isset($this->catalogues[$locale])) {
335336
return;
336337
}
337338

338-
if (null === $this->options['cache_dir']) {
339+
if (null === $this->cacheDir) {
339340
$this->initialize();
340341

341-
return parent::loadCatalogue($locale);
342+
return $this->loadCatalogue($locale);
342343
}
343344

344-
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
345+
$this->assertValidLocale($locale);
346+
$cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug);
345347
if (!$cache->isFresh()) {
346-
$this->initialize();
347-
348-
parent::loadCatalogue($locale);
348+
$this->initializeCatalogue($locale);
349349

350350
$fallbackContent = '';
351351
$current = '';

0 commit comments

Comments
 (0)
0