@@ -54,7 +54,7 @@ public function testTransWithoutCaching()
54
54
{
55
55
$ translator = $ this ->getTranslator ($ this ->getLoader ());
56
56
$ 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 ' ));
58
58
59
59
$ this ->assertEquals ('foo (FR) ' , $ translator ->trans ('foo ' ));
60
60
$ this ->assertEquals ('bar (EN) ' , $ translator ->trans ('bar ' ));
@@ -63,14 +63,16 @@ public function testTransWithoutCaching()
63
63
$ this ->assertEquals ('no translation ' , $ translator ->trans ('no translation ' ));
64
64
$ this ->assertEquals ('foobarfoo (PT-PT) ' , $ translator ->trans ('foobarfoo ' ));
65
65
$ 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 ' ));
66
68
}
67
69
68
70
public function testTransWithCaching ()
69
71
{
70
72
// prime the cache
71
- $ translator = $ this ->getTranslator ($ this ->getLoader (), array ( ' cache_dir ' => $ this ->tmpDir ) );
73
+ $ translator = $ this ->getTranslator ($ this ->getLoader (), $ this ->tmpDir );
72
74
$ 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 ' ));
74
76
75
77
$ this ->assertEquals ('foo (FR) ' , $ translator ->trans ('foo ' ));
76
78
$ this ->assertEquals ('bar (EN) ' , $ translator ->trans ('bar ' ));
@@ -79,12 +81,14 @@ public function testTransWithCaching()
79
81
$ this ->assertEquals ('no translation ' , $ translator ->trans ('no translation ' ));
80
82
$ this ->assertEquals ('foobarfoo (PT-PT) ' , $ translator ->trans ('foobarfoo ' ));
81
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 ' ));
82
86
83
87
// do it another time as the cache is primed now
84
88
$ 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 );
86
90
$ 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 ' ));
88
92
89
93
$ this ->assertEquals ('foo (FR) ' , $ translator ->trans ('foo ' ));
90
94
$ this ->assertEquals ('bar (EN) ' , $ translator ->trans ('bar ' ));
@@ -93,6 +97,36 @@ public function testTransWithCaching()
93
97
$ this ->assertEquals ('no translation ' , $ translator ->trans ('no translation ' ));
94
98
$ this ->assertEquals ('foobarfoo (PT-PT) ' , $ translator ->trans ('foobarfoo ' ));
95
99
$ 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
+ }
96
130
}
97
131
98
132
protected function getCatalogue ($ locale , $ messages )
@@ -145,25 +179,53 @@ protected function getLoader()
145
179
'other choice ' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR) ' ,
146
180
))))
147
181
;
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
+ ;
148
196
149
197
return $ loader ;
150
198
}
151
199
152
- public function getTranslator ($ loader , $ options = array () )
200
+ public function getTranslator ($ loader , $ cacheDir = null , $ translatorClass = ' \Symfony\Component\Translation\Translator ' )
153
201
{
154
- $ translator = new Translator (
155
- $ loader ,
156
- new MessageSelector (),
157
- $ options
158
- );
202
+ $ translator = new $ translatorClass ('fr ' , new MessageSelector (), $ cacheDir );
159
203
160
204
$ translator ->addLoader ('loader ' , $ loader );
161
205
$ translator ->addResource ('loader ' , 'foo ' , 'fr ' );
162
206
$ translator ->addResource ('loader ' , 'foo ' , 'en ' );
163
207
$ translator ->addResource ('loader ' , 'foo ' , 'es ' );
164
208
$ translator ->addResource ('loader ' , 'foo ' , 'pt-PT ' ); // European Portuguese
165
209
$ 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
166
212
167
213
return $ translator ;
168
214
}
169
215
}
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
+ }
0 commit comments