12
12
namespace Symfony \Bundle \FrameworkBundle \Tests \Translation ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Psr \Container \ContainerInterface ;
15
16
use Symfony \Bundle \FrameworkBundle \Translation \Translator ;
16
17
use Symfony \Component \Translation \MessageCatalogue ;
17
18
use Symfony \Component \Filesystem \Filesystem ;
@@ -42,6 +43,157 @@ protected function deleteTmpDir()
42
43
$ fs ->remove ($ dir );
43
44
}
44
45
46
+ /**
47
+ * @group legacy
48
+ * @expectedDeprecation Method Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() takes the default locale as 3rd argument since version 3.3. Not passing it is deprecated and will trigger an error in 4.0.
49
+ */
50
+ public function testTransWithoutCachingOmittingLocale ()
51
+ {
52
+ $ translator = $ this ->getTranslator ($ this ->getLoader (), array (), 'loader ' , '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , null );
53
+ $ translator ->setLocale ('fr ' );
54
+ $ translator ->setFallbackLocales (array ('en ' , 'es ' , 'pt-PT ' , 'pt_BR ' , 'fr.UTF-8 ' , 'sr@latin ' ));
55
+
56
+ $ this ->assertEquals ('foo (FR) ' , $ translator ->trans ('foo ' ));
57
+ $ this ->assertEquals ('bar (EN) ' , $ translator ->trans ('bar ' ));
58
+ $ this ->assertEquals ('foobar (ES) ' , $ translator ->trans ('foobar ' ));
59
+ $ this ->assertEquals ('choice 0 (EN) ' , $ translator ->transChoice ('choice ' , 0 ));
60
+ $ this ->assertEquals ('no translation ' , $ translator ->trans ('no translation ' ));
61
+ $ this ->assertEquals ('foobarfoo (PT-PT) ' , $ translator ->trans ('foobarfoo ' ));
62
+ $ this ->assertEquals ('other choice 1 (PT-BR) ' , $ translator ->transChoice ('other choice ' , 1 ));
63
+ $ this ->assertEquals ('foobarbaz (fr.UTF-8) ' , $ translator ->trans ('foobarbaz ' ));
64
+ $ this ->assertEquals ('foobarbax (sr@latin) ' , $ translator ->trans ('foobarbax ' ));
65
+ }
66
+
67
+ /**
68
+ * @group legacy
69
+ * @expectedDeprecation Method Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() takes the default locale as 3rd argument since version 3.3. Not passing it is deprecated and will trigger an error in 4.0.
70
+ */
71
+ public function testTransWithCachingOmittingLocale ()
72
+ {
73
+ // prime the cache
74
+ $ translator = $ this ->getTranslator ($ this ->getLoader (), array ('cache_dir ' => $ this ->tmpDir ), 'loader ' , '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , null );
75
+ $ translator ->setLocale ('fr ' );
76
+ $ translator ->setFallbackLocales (array ('en ' , 'es ' , 'pt-PT ' , 'pt_BR ' , 'fr.UTF-8 ' , 'sr@latin ' ));
77
+
78
+ $ this ->assertEquals ('foo (FR) ' , $ translator ->trans ('foo ' ));
79
+ $ this ->assertEquals ('bar (EN) ' , $ translator ->trans ('bar ' ));
80
+ $ this ->assertEquals ('foobar (ES) ' , $ translator ->trans ('foobar ' ));
81
+ $ this ->assertEquals ('choice 0 (EN) ' , $ translator ->transChoice ('choice ' , 0 ));
82
+ $ this ->assertEquals ('no translation ' , $ translator ->trans ('no translation ' ));
83
+ $ this ->assertEquals ('foobarfoo (PT-PT) ' , $ translator ->trans ('foobarfoo ' ));
84
+ $ this ->assertEquals ('other choice 1 (PT-BR) ' , $ translator ->transChoice ('other choice ' , 1 ));
85
+ $ this ->assertEquals ('foobarbaz (fr.UTF-8) ' , $ translator ->trans ('foobarbaz ' ));
86
+ $ this ->assertEquals ('foobarbax (sr@latin) ' , $ translator ->trans ('foobarbax ' ));
87
+
88
+ // do it another time as the cache is primed now
89
+ $ loader = $ this ->getMockBuilder ('Symfony\Component\Translation\Loader\LoaderInterface ' )->getMock ();
90
+ $ loader ->expects ($ this ->never ())->method ('load ' );
91
+
92
+ $ translator = $ this ->getTranslator ($ loader , array ('cache_dir ' => $ this ->tmpDir ), 'loader ' , '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , null );
93
+ $ translator ->setLocale ('fr ' );
94
+ $ translator ->setFallbackLocales (array ('en ' , 'es ' , 'pt-PT ' , 'pt_BR ' , 'fr.UTF-8 ' , 'sr@latin ' ));
95
+
96
+ $ this ->assertEquals ('foo (FR) ' , $ translator ->trans ('foo ' ));
97
+ $ this ->assertEquals ('bar (EN) ' , $ translator ->trans ('bar ' ));
98
+ $ this ->assertEquals ('foobar (ES) ' , $ translator ->trans ('foobar ' ));
99
+ $ this ->assertEquals ('choice 0 (EN) ' , $ translator ->transChoice ('choice ' , 0 ));
100
+ $ this ->assertEquals ('no translation ' , $ translator ->trans ('no translation ' ));
101
+ $ this ->assertEquals ('foobarfoo (PT-PT) ' , $ translator ->trans ('foobarfoo ' ));
102
+ $ this ->assertEquals ('other choice 1 (PT-BR) ' , $ translator ->transChoice ('other choice ' , 1 ));
103
+ $ this ->assertEquals ('foobarbaz (fr.UTF-8) ' , $ translator ->trans ('foobarbaz ' ));
104
+ $ this ->assertEquals ('foobarbax (sr@latin) ' , $ translator ->trans ('foobarbax ' ));
105
+ }
106
+
107
+ /**
108
+ * @group legacy
109
+ * @expectedDeprecation Method Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() takes the default locale as 3rd argument since version 3.3. Not passing it is deprecated and will trigger an error in 4.0.
110
+ * @expectedException \InvalidArgumentException
111
+ */
112
+ public function testTransWithCachingWithInvalidLocaleOmittingLocale ()
113
+ {
114
+ $ loader = $ this ->getMockBuilder ('Symfony\Component\Translation\Loader\LoaderInterface ' )->getMock ();
115
+ $ translator = $ this ->getTranslator ($ loader , array ('cache_dir ' => $ this ->tmpDir ), 'loader ' , '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale ' , null );
116
+
117
+ $ translator ->trans ('foo ' );
118
+ }
119
+
120
+ /**
121
+ * @group legacy
122
+ * @expectedDeprecation Method Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() takes the default locale as 3rd argument since version 3.3. Not passing it is deprecated and will trigger an error in 4.0.
123
+ */
124
+ public function testLoadResourcesWithoutCachingOmittingLocale ()
125
+ {
126
+ $ loader = new \Symfony \Component \Translation \Loader \YamlFileLoader ();
127
+ $ resourceFiles = array (
128
+ 'fr ' => array (
129
+ __DIR__ .'/../Fixtures/Resources/translations/messages.fr.yml ' ,
130
+ ),
131
+ );
132
+
133
+ $ translator = $ this ->getTranslator ($ loader , array ('resource_files ' => $ resourceFiles ), 'yml ' , '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , null );
134
+ $ translator ->setLocale ('fr ' );
135
+
136
+ $ this ->assertEquals ('répertoire ' , $ translator ->trans ('folder ' ));
137
+ }
138
+
139
+ /**
140
+ * @group legacy
141
+ * @expectedDeprecation Method Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() takes the default locale as 3rd argument since version 3.3. Not passing it is deprecated and will trigger an error in 4.0.
142
+ */
143
+ public function testGetDefaultLocaleOmittingLocale ()
144
+ {
145
+ $ container = $ this ->getMockBuilder ('Symfony\Component\DependencyInjection\ContainerInterface ' )->getMock ();
146
+ $ container
147
+ ->expects ($ this ->once ())
148
+ ->method ('getParameter ' )
149
+ ->with ('kernel.default_locale ' )
150
+ ->will ($ this ->returnValue ('en ' ))
151
+ ;
152
+ $ translator = new Translator ($ container , new MessageSelector ());
153
+
154
+ $ this ->assertSame ('en ' , $ translator ->getLocale ());
155
+ }
156
+
157
+ /**
158
+ * @group legacy
159
+ * @expectedException \InvalidArgumentException
160
+ * @expectedExceptionMessage Missing third $defaultLocale argument.
161
+ */
162
+ public function testGetDefaultLocaleOmittingLocaleWithPsrContainer ()
163
+ {
164
+ $ container = $ this ->getMockBuilder (ContainerInterface::class)->getMock ();
165
+ $ translator = new Translator ($ container , new MessageSelector ());
166
+ }
167
+
168
+ /**
169
+ * @group legacy
170
+ * @expectedDeprecation Method Symfony\Bundle\FrameworkBundle\Translation\Translator::__construct() takes the default locale as 3rd argument since version 3.3. Not passing it is deprecated and will trigger an error in 4.0.
171
+ */
172
+ public function testWarmupOmittingLocale ()
173
+ {
174
+ $ loader = new \Symfony \Component \Translation \Loader \YamlFileLoader ();
175
+ $ resourceFiles = array (
176
+ 'fr ' => array (
177
+ __DIR__ .'/../Fixtures/Resources/translations/messages.fr.yml ' ,
178
+ ),
179
+ );
180
+
181
+ // prime the cache
182
+ $ translator = $ this ->getTranslator ($ loader , array ('cache_dir ' => $ this ->tmpDir , 'resource_files ' => $ resourceFiles ), 'yml ' , '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , null );
183
+ $ translator ->setFallbackLocales (array ('fr ' ));
184
+ $ translator ->warmup ($ this ->tmpDir );
185
+
186
+ $ loader = $ this ->getMockBuilder ('Symfony\Component\Translation\Loader\LoaderInterface ' )->getMock ();
187
+ $ loader
188
+ ->expects ($ this ->never ())
189
+ ->method ('load ' );
190
+
191
+ $ translator = $ this ->getTranslator ($ loader , array ('cache_dir ' => $ this ->tmpDir , 'resource_files ' => $ resourceFiles ), 'yml ' , '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , null );
192
+ $ translator ->setLocale ('fr ' );
193
+ $ translator ->setFallbackLocales (array ('fr ' ));
194
+ $ this ->assertEquals ('répertoire ' , $ translator ->trans ('folder ' ));
195
+ }
196
+
45
197
public function testTransWithoutCaching ()
46
198
{
47
199
$ translator = $ this ->getTranslator ($ this ->getLoader ());
@@ -97,6 +249,7 @@ public function testTransWithCaching()
97
249
98
250
/**
99
251
* @expectedException \InvalidArgumentException
252
+ * @expectedExceptionMessage Invalid "invalid locale" locale.
100
253
*/
101
254
public function testTransWithCachingWithInvalidLocale ()
102
255
{
@@ -123,15 +276,8 @@ public function testLoadResourcesWithoutCaching()
123
276
124
277
public function testGetDefaultLocale ()
125
278
{
126
- $ container = $ this ->getMockBuilder ('Symfony\Component\DependencyInjection\ContainerInterface ' )->getMock ();
127
- $ container
128
- ->expects ($ this ->once ())
129
- ->method ('getParameter ' )
130
- ->with ('kernel.default_locale ' )
131
- ->will ($ this ->returnValue ('en ' ))
132
- ;
133
-
134
- $ translator = new Translator ($ container , new MessageSelector ());
279
+ $ container = $ this ->getMockBuilder (ContainerInterface::class)->getMock ();
280
+ $ translator = new Translator ($ container , new MessageSelector (), 'en ' );
135
281
136
282
$ this ->assertSame ('en ' , $ translator ->getLocale ());
137
283
}
@@ -144,7 +290,7 @@ public function testInvalidOptions()
144
290
{
145
291
$ container = $ this ->getMockBuilder ('Symfony\Component\DependencyInjection\ContainerInterface ' )->getMock ();
146
292
147
- (new Translator ($ container , new MessageSelector (), array (), array ('foo ' => 'bar ' )));
293
+ (new Translator ($ container , new MessageSelector (), ' en ' , array (), array ('foo ' => 'bar ' )));
148
294
}
149
295
150
296
protected function getCatalogue ($ locale , $ messages , $ resources = array ())
@@ -230,9 +376,9 @@ protected function getContainer($loader)
230
376
return $ container ;
231
377
}
232
378
233
- public function getTranslator ($ loader , $ options = array (), $ loaderFomat = 'loader ' , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' )
379
+ public function getTranslator ($ loader , $ options = array (), $ loaderFomat = 'loader ' , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ defaultLocale = ' en ' )
234
380
{
235
- $ translator = $ this ->createTranslator ($ loader , $ options , $ translatorClass , $ loaderFomat );
381
+ $ translator = $ this ->createTranslator ($ loader , $ options , $ translatorClass , $ loaderFomat, $ defaultLocale );
236
382
237
383
if ('loader ' === $ loaderFomat ) {
238
384
$ translator ->addResource ('loader ' , 'foo ' , 'fr ' );
@@ -272,11 +418,21 @@ public function testWarmup()
272
418
$ this ->assertEquals ('répertoire ' , $ translator ->trans ('folder ' ));
273
419
}
274
420
275
- private function createTranslator ($ loader , $ options , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ loaderFomat = 'loader ' )
421
+ private function createTranslator ($ loader , $ options , $ translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator ' , $ loaderFomat = 'loader ' , $ defaultLocale = ' en ' )
276
422
{
423
+ if (null === $ defaultLocale ) {
424
+ return new $ translatorClass (
425
+ $ this ->getContainer ($ loader ),
426
+ new MessageSelector (),
427
+ array ($ loaderFomat => array ($ loaderFomat )),
428
+ $ options
429
+ );
430
+ }
431
+
277
432
return new $ translatorClass (
278
433
$ this ->getContainer ($ loader ),
279
434
new MessageSelector (),
435
+ $ defaultLocale ,
280
436
array ($ loaderFomat => array ($ loaderFomat )),
281
437
$ options
282
438
);
0 commit comments