@@ -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+ }
0 commit comments