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