7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Translate
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 require_once dirname(__FILE__
) . '/../TestHelper.php';
28 require_once 'Zend/Translate.php';
31 * Zend_Translate_Plural
33 require_once 'Zend/Translate/Plural.php';
37 * @package Zend_Translate
38 * @subpackage UnitTests
39 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
40 * @license http://framework.zend.com/license/new-bsd New BSD License
41 * @group Zend_Translate
43 class Zend_TranslateTest
extends PHPUnit_Framework_TestCase
46 * Runs the test methods of this class.
50 public static function main()
52 $suite = new PHPUnit_Framework_TestSuite("Zend_TranslateTest");
53 $result = PHPUnit_TextUI_TestRunner
::run($suite);
56 public function setUp()
58 if (Zend_Translate
::hasCache()) {
59 Zend_Translate
::removeCache();
62 require_once 'Zend/Translate/Adapter/Array.php';
63 if (Zend_Translate_Adapter_Array
::hasCache()) {
64 Zend_Translate_Adapter_Array
::removeCache();
68 public function testCreate()
70 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('1' => '1'));
71 $this->assertTrue($lang instanceof Zend_Translate
);
74 public function testLocaleInitialization()
76 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'message1'), 'en');
77 $this->assertEquals('en', $lang->getLocale());
80 public function testDefaultLocale()
82 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'message1'));
83 $defaultLocale = new Zend_Locale();
84 $this->assertEquals($defaultLocale->toString(), $lang->getLocale());
87 public function testGetAdapter()
89 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('1' => '1'), 'en');
90 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Array
);
92 $lang = new Zend_Translate(Zend_Translate
::AN_GETTEXT
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.mo', 'en');
93 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Gettext
);
95 $lang = new Zend_Translate(Zend_Translate
::AN_TMX
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.tmx', 'en');
96 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Tmx
);
98 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.csv', 'en');
99 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Csv
);
101 $lang = new Zend_Translate(Zend_Translate
::AN_XLIFF
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.xliff', 'en');
102 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Xliff
);
104 $lang = new Zend_Translate('Qt' , dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en2.ts', 'en');
105 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Qt
);
107 $lang = new Zend_Translate('XmlTm' , dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.xmltm', 'en');
108 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_XmlTm
);
110 $lang = new Zend_Translate('Tbx' , dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.tbx', 'en');
111 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Tbx
);
114 public function testSetAdapter()
116 $lang = new Zend_Translate(Zend_Translate
::AN_GETTEXT
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.mo', 'en');
117 $lang->setAdapter(Zend_Translate
::AN_ARRAY
, array('de' => 'de'));
118 $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Array
);
121 $lang->xxxFunction();
122 $this->fail("exception expected");
123 } catch (Zend_Translate_Exception
$e) {
128 public function testAddTranslation()
130 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
132 $this->assertEquals('msg2', $lang->_('msg2'));
134 $lang->addTranslation(array('msg2' => 'Message 2'), 'en');
135 $this->assertEquals('Message 2', $lang->_('msg2'));
136 $this->assertEquals('msg3', $lang->_('msg3'));
138 $lang->addTranslation(array('msg3' => 'Message 3'), 'en', array('clear' => true));
139 $this->assertEquals('msg2', $lang->_('msg2'));
140 $this->assertEquals('Message 3', $lang->_('msg3'));
143 public function testGetLocale()
145 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
146 $this->assertEquals('en', $lang->getLocale());
149 public function testSetLocale()
151 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
152 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
153 $this->assertEquals('ru', $lang->getLocale());
155 $lang->setLocale('en');
156 $this->assertEquals('en', $lang->getLocale());
158 $lang->setLocale('ru');
159 $this->assertEquals('ru', $lang->getLocale());
161 $lang->setLocale('ru_RU');
162 $this->assertEquals('ru', $lang->getLocale());
165 public function testSetLanguage()
167 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
168 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
169 $this->assertEquals('ru', $lang->getLocale());
171 $lang->setLocale('en');
172 $this->assertEquals('en', $lang->getLocale());
175 public function testGetLanguageList()
177 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
178 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
179 $this->assertEquals(2, count($lang->getList()));
180 $this->assertTrue(in_array('en', $lang->getList()));
181 $this->assertTrue(in_array('ru', $lang->getList()));
184 public function testIsAvailable()
186 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
187 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
188 $this->assertTrue( $lang->isAvailable('en'));
189 $this->assertTrue( $lang->isAvailable('ru'));
190 $this->assertFalse($lang->isAvailable('fr'));
193 public function testTranslate()
195 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1 (en)'), 'en');
196 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
197 $this->assertEquals('Message 1 (en)', $lang->_('msg1', 'en' ));
198 $this->assertEquals('Message 1 (ru)', $lang->_('msg1' ));
199 $this->assertEquals('msg2', $lang->_('msg2', 'en' ));
200 $this->assertEquals('msg2', $lang->_('msg2' ));
201 $this->assertEquals('Message 1 (en)', $lang->translate('msg1', 'en'));
202 $this->assertEquals('Message 1 (ru)', $lang->translate('msg1' ));
203 $this->assertEquals('msg2', $lang->translate('msg2', 'en'));
204 $this->assertEquals('msg2', $lang->translate('msg2' ));
207 public function testIsTranslated()
209 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1 (en)'), 'en_US');
210 $this->assertTrue( $lang->isTranslated('msg1' ));
211 $this->assertFalse($lang->isTranslated('msg2' ));
212 $this->assertFalse($lang->isTranslated('msg1', false, 'en'));
213 $this->assertFalse($lang->isTranslated('msg1', true, 'en'));
214 $this->assertFalse($lang->isTranslated('msg1', false, 'ru'));
217 public function testWithOption()
219 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_otherdelimiter.csv', 'en', array('delimiter' => ','));
220 $this->assertEquals('Message 1 (en)', $lang->translate('Message 1'));
221 $this->assertEquals('Message 4 (en)', $lang->translate('Message 4,'));
222 $this->assertEquals('Message 5, (en)', $lang->translate('Message 5'));
225 public function testDirectorySearch()
227 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files/testcsv', 'de_AT', array('scan' => Zend_Translate
::LOCALE_DIRECTORY
));
228 $this->assertEquals(array('de_AT' => 'de_AT', 'en_GB' => 'en_GB'), $lang->getList());
229 $this->assertEquals('Nachricht 8', $lang->translate('Message 8'));
232 public function testFileSearch()
234 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files/testcsv', 'de_DE', array('scan' => Zend_Translate
::LOCALE_FILENAME
));
235 $this->assertEquals(array('de_DE' => 'de_DE', 'en_US' => 'en_US'), $lang->getList());
236 $this->assertEquals('Nachricht 8', $lang->translate('Message 8'));
239 public function testTestingCacheHandling()
241 require_once 'Zend/Cache.php';
242 $cache = Zend_Cache
::factory('Core', 'File',
243 array('lifetime' => 120, 'automatic_serialization' => true),
244 array('cache_dir' => dirname(__FILE__
) . '/_files/'));
245 Zend_Translate
::setCache($cache);
247 $cache = Zend_Translate
::getCache();
248 $this->assertTrue($cache instanceof Zend_Cache_Core
);
249 $this->assertTrue(Zend_Translate
::hasCache());
251 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1 (en)'), 'en');
252 $adapter = $lang->getAdapter();
253 $this->assertTrue($adapter instanceof Zend_Translate_Adapter_Array
);
254 $adaptercache = $adapter->getCache();
255 $this->assertTrue($adaptercache instanceof Zend_Cache_Core
);
257 Zend_Translate
::clearCache();
258 $this->assertTrue(Zend_Translate
::hasCache());
259 Zend_Translate
::removeCache();
260 $this->assertFalse(Zend_Translate
::hasCache());
263 public function testExceptionWhenNoAdapterClassWasSet()
266 $lang = new Zend_Translate('Zend_Locale', dirname(__FILE__
) . '/Translate/_files/test2', null, array('scan' => Zend_Translate
::LOCALE_FILENAME
));
267 $this->fail('Exception due to false adapter class expected');
268 } catch (Zend_Translate_Exception
$e) {
269 $this->assertContains('does not extend Zend_Translate_Adapter', $e->getMessage());
273 public function testZF3679()
275 require_once 'Zend/Locale.php';
276 $locale = new Zend_Locale('de_AT');
277 require_once 'Zend/Registry.php';
278 Zend_Registry
::set('Zend_Locale', $locale);
280 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'message1'), 'de_AT');
281 $this->assertEquals('de_AT', $lang->getLocale());
282 Zend_Registry
::_unsetInstance();
288 public function testCamelCasedOptions()
290 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_otherdelimiter.csv', 'en', array('delimiter' => ','));
291 $lang->setOptions(array('myOption' => true));
292 $this->assertTrue($lang->getOptions('myOption'));
298 public function testPathNameWithColonResolution()
300 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/../Adapter/_files', 'en', array('delimiter' => ','));
301 $this->assertEquals('en', $lang->getLocale());
304 public function testUntranslatedMessageWithTriggeredError()
306 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
307 $this->assertEquals('ignored', $lang->translate('ignored'));
309 $this->_errorOccured
= false;
310 $lang->setOptions(array('logUntranslated' => true));
311 set_error_handler(array($this, 'errorHandlerIgnore'));
312 $this->assertEquals('ignored', $lang->translate('ignored'));
313 $this->assertTrue($this->_errorOccured
);
314 restore_error_handler();
317 public function testLogUntranslatedMessage()
319 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
320 $this->assertEquals('ignored', $lang->translate('ignored'));
322 $stream = fopen('php://memory', 'w+');
323 require_once 'Zend/Log/Writer/Stream.php';
324 $writer = new Zend_Log_Writer_Stream($stream);
325 require_once 'Zend/Log.php';
326 $log = new Zend_Log($writer);
328 $lang->setOptions(array('logUntranslated' => true, 'log' => $log));
329 $this->assertEquals('ignored', $lang->translate('ignored'));
332 $this->assertContains('ignored', stream_get_contents($stream));
335 public function testSettingUnknownLocaleWithTriggeredError()
337 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
338 $this->_errorOccured
= false;
339 set_error_handler(array($this, 'errorHandlerIgnore'));
340 $lang->setLocale('ru');
341 $this->assertEquals('ru', $lang->getLocale('ru'));
342 $this->assertTrue($this->_errorOccured
);
343 restore_error_handler();
346 public function testSettingUnknownLocaleWritingToLog()
348 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
350 $stream = fopen('php://memory', 'w+');
351 require_once 'Zend/Log/Writer/Stream.php';
352 $writer = new Zend_Log_Writer_Stream($stream);
353 require_once 'Zend/Log.php';
354 $log = new Zend_Log($writer);
356 $lang->setOptions(array('log' => $log));
357 $lang->setLocale('ru');
360 $this->assertContains('has to be added', stream_get_contents($stream));
363 public function testSettingNoLogAsLog()
365 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
368 $lang->setOptions(array('log' => 'nolog'));
370 } catch (Zend_Translate_Exception
$e) {
371 $this->assertContains('Instance of Zend_Log expected', $e->getMessage());
375 public function testSettingUnknownLocaleWritingToSelfDefinedLog()
377 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
378 $this->assertEquals('ignored', $lang->translate('ignored'));
380 $stream = fopen('php://memory', 'w+');
381 require_once 'Zend/Log/Writer/Stream.php';
382 $writer = new Zend_Log_Writer_Stream($stream);
383 require_once 'Zend/Log.php';
384 $log = new Zend_Log($writer);
386 $lang->setOptions(array('logUntranslated' => true, 'log' => $log, 'logMessage' => 'Self defined log message'));
387 $this->assertEquals('ignored', $lang->translate('ignored'));
390 $this->assertContains('Self defined log message', stream_get_contents($stream));
394 * Tests if cached options are read from the cache for a new instance
396 public function testGetOptionsFromCache()
398 require_once 'Zend/Cache.php';
399 $cache = Zend_Cache
::factory('Core', 'File',
400 array('lifetime' => 120, 'automatic_serialization' => true),
401 array('cache_dir' => dirname(__FILE__
) . '/_files/'));
402 Zend_Translate
::setCache($cache);
404 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
405 $lang->setOptions(array('logMessage' => 'test'));
406 $this->assertEquals('test', $lang->getOptions('logMessage'));
409 $lang2 = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
410 $this->assertEquals('test', $lang2->getOptions('logMessage'));
414 * Tests if setting locale as options sets locale
416 public function testSetLocaleAsOption()
418 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
419 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
420 $lang->setOptions(array('locale' => 'ru'));
421 $this->assertEquals('ru', $lang->getLocale());
422 $lang->setOptions(array('locale' => 'en'));
423 $this->assertEquals('en', $lang->getLocale());
427 * Tests getting null returns all options
429 public function testGettingAllOptions()
431 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
432 $this->assertTrue(is_array($lang->getOptions()));
436 * Tests if setting locale as options sets locale
438 public function testGettingUnknownOption()
440 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
441 $this->assertEquals(null, $lang->getOptions('unknown'));
445 * Tests getting of all message ids works
447 public function testGettingAllMessageIds()
449 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1', 'msg2' => 'Message 2'), 'en');
450 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
451 $this->assertEquals(array('msg1'), $lang->getMessageIds());
452 $this->assertEquals(array('msg1', 'msg2'), $lang->getMessageIds('en'));
456 * Tests getting of all messages
458 public function testGettingAllMessages()
460 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1', 'msg2' => 'Message 2'), 'en');
461 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
462 $this->assertEquals(array('msg1' => 'Message 1 (ru)'), $lang->getMessages());
464 array('msg1' => 'Message 1', 'msg2' => 'Message 2'),
465 $lang->getMessages('en'));
468 'en' => array('msg1' => 'Message 1', 'msg2' => 'Message 2'),
469 'ru' => array('msg1' => 'Message 1 (ru)')),
470 $lang->getMessages('all'));
474 * Tests getting default plurals
476 public function testGettingPlurals()
478 $lang = new Zend_Translate(
479 Zend_Translate
::AN_ARRAY
,
481 array('plural_0 (en)',
485 'plural' => ''), 'en'
488 $this->assertEquals('plural_0 (en)', $lang->translate(array('singular', 'plural', 1)));
489 $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 2)));
491 $this->assertEquals('plural_0 (en)', $lang->plural('singular', 'plural', 1));
492 $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 2));
496 * Tests getting plurals from lowered locale
498 public function testGettingPluralsFromLoweredLocale()
500 $lang = new Zend_Translate(
501 Zend_Translate
::AN_ARRAY
,
503 array('plural_0 (en)',
507 'plural' => ''), 'en'
509 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'en_US');
510 $lang->setLocale('en_US');
512 $this->assertEquals('plural_0 (en)', $lang->translate(array('singular', 'plural', 1)));
513 $this->assertEquals('plural_0 (en)', $lang->plural('singular', 'plural', 1));
517 * Tests getting plurals from lowered locale
519 public function testGettingPluralsFromUnknownLocale()
521 $lang = new Zend_Translate(
522 Zend_Translate
::AN_ARRAY
,
524 array('plural_0 (en)',
528 'plural' => ''), 'en'
531 $this->assertEquals('singular', $lang->translate(array('singular', 'plural', 1), 'ru'));
532 $this->assertEquals('singular', $lang->plural('singular', 'plural', 1, 'ru'));
533 $this->assertEquals('plural', $lang->translate(array('singular', 'plural', 'plural2', 2, 'en'), 'ru'));
534 $this->assertEquals('plural', $lang->plural('singular', 'plural', 2, 'ru'));
537 public function testPluralsWithGettext()
539 $lang = new Zend_Translate(Zend_Translate
::AN_GETTEXT
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.mo', 'en');
541 $this->assertEquals('Message 5 (en) Plural 0', $lang->translate(array('Message 5', 'Message 5 Plural', 1)));
542 $this->assertEquals('Message 5 (en) Plural 0', $lang->plural('Message 5', 'Message 5 Plural', 1));
543 $this->assertEquals('Message 5 (en) Plural 1', $lang->translate(array('Message 5', 'Message 5 Plural', 2)));
544 $this->assertEquals('Message 5 (en) Plural 1', $lang->plural('Message 5', 'Message 5 Plural', 2));
547 public function testPluralsWithCsv()
549 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.csv', 'en');
551 $this->assertEquals('Message 6 (en) Plural 0', $lang->translate(array('Message 6', 'Message 6 Plural1', 1)));
552 $this->assertEquals('Message 6 (en) Plural 0', $lang->plural('Message 6', 'Message 6 Plural1', 1));
553 $this->assertEquals('Message 6 (en) Plural 1', $lang->translate(array('Message 6', 'Message 6 Plural1', 2)));
554 $this->assertEquals('Message 6 (en) Plural 1', $lang->plural('Message 6', 'Message 6 Plural1', 2));
560 public function testAddTranslationAfterwards()
562 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array('msg1' => 'Message 1'), 'en');
563 $this->assertEquals('Message 1', $lang->_('msg1'));
565 $lang->addTranslation(array('msg1' => 'Message 1 (en)'), 'en');
566 $this->assertEquals('Message 1 (en)', $lang->_('msg1'));
572 public function testUseNumericTranslations()
574 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, array(0 => 'Message 1', 2 => 'Message 2'), 'en');
575 $this->assertEquals('Message 1', $lang->_(0));
576 $this->assertEquals('Message 2', $lang->_(2));
578 $lang->addTranslation(array(4 => 'Message 4'), 'en');
579 $this->assertEquals('Message 4', $lang->_(4));
585 public function testDontLogUntranslatedMessageWithIsTranslated()
587 $lang = new Zend_Translate(Zend_Translate
::AN_CSV
, dirname(__FILE__
) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
588 $this->assertFalse($lang->isTranslated('ignored'));
590 $stream = fopen('php://memory', 'w+');
591 require_once 'Zend/Log/Writer/Stream.php';
592 $writer = new Zend_Log_Writer_Stream($stream);
593 require_once 'Zend/Log.php';
594 $log = new Zend_Log($writer);
596 $lang->setOptions(array('logUntranslated' => true, 'log' => $log));
597 $this->assertFalse($lang->isTranslated('ignored'));
600 $this->assertNotContains('ignored', stream_get_contents($stream));
606 public function testMultiFolderScan()
608 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, dirname(__FILE__
) . '/Translate/Adapter/_files/testarray', 'en_GB', array('scan' => Zend_Translate
::LOCALE_DIRECTORY
));
609 $this->assertEquals('Message 1 (ja)', $lang->_('Message 1', 'ja' ));
610 $this->assertEquals('Message 1 (en)', $lang->_('Message 1' ));
616 public function testMultiClear()
618 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, dirname(__FILE__
) . '/Translate/Adapter/_files/testarray', 'en_GB', array('scan' => Zend_Translate
::LOCALE_DIRECTORY
));
619 $this->assertEquals('Message 1 (ja)', $lang->_('Message 1', 'ja'));
620 $lang->addTranslation(dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.php', 'ja', array('clear'));
621 $this->assertEquals('Message 1 (en)', $lang->_('Message 1', 'ja'));
627 public function testEmptyTranslation()
629 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, null, null, array('disableNotices' => true));
630 $this->assertEquals(0, count($lang->getList()));
636 public function testObjectTranslation()
638 $lang = new Zend_Translate(Zend_Translate
::AN_ARRAY
, dirname(__FILE__
) . '/Translate/Adapter/_files/testarray', 'en_GB', array('scan' => Zend_Translate
::LOCALE_DIRECTORY
));
639 $this->assertEquals('Message 1 (ja)', $lang->_('Message 1', 'ja'));
641 $this->assertEquals($lang, $lang->translate($lang));
645 * Tests getting plurals from lowered locale
647 public function testGettingPluralsUsingOwnRule()
649 $lang = new Zend_Translate(
650 Zend_Translate
::AN_ARRAY
,
652 array('plural_0 (en)',
656 'plural' => ''), 'en'
658 $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'en_US');
659 $lang->setLocale('en_US');
661 Zend_Translate_Plural
::setPlural(array($this, 'customPlural'), 'en_US');
662 $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 1)));
663 $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 1));
664 $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 0)));
665 $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 0));
671 public function testAddingAdapterToSourcealsUsingOwnRule()
673 $translate = new Zend_Translate(
674 Zend_Translate
::AN_ARRAY
,
676 array('plural_0 (en)',
680 'plural' => ''), 'en'
683 $this->assertFalse($translate->isTranslated('Message 1'));
684 $adapter = new Zend_Translate_Adapter_Gettext(dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en.mo', 'en');
685 $translate->addTranslation($adapter);
687 $this->assertTrue($adapter->isTranslated('Message 1'));
689 $adapter2 = new Zend_Translate_Adapter_Gettext(dirname(__FILE__
) . '/Translate/Adapter/_files/testmo/de_AT/LC_TEST/translation-de_DE.mo', 'de_AT');
690 $adapter2->addTranslation(dirname(__FILE__
) . '/Translate/Adapter/_files/translation_en2.mo', 'fr');
691 $translate->addTranslation($adapter2, 'fr');
693 $languages = $translate->getList();
694 $this->assertFalse(array_key_exists('de_AT', $languages));
695 $this->assertTrue(array_key_exists('fr', $languages));
701 public function testIgnoreMultipleDirectories()
703 $translate = new Zend_Translate(
704 Zend_Translate
::AN_ARRAY
,
705 dirname(__FILE__
) . '/Translate/Adapter/_files/testArray/',
708 'scan' => Zend_Translate
::LOCALE_FILENAME
,
709 'ignore' => array('.', 'ignoreme', 'LC_TEST')
713 $langs = $translate->getList();
714 $this->assertFalse(array_key_exists('de_DE', $langs));
715 $this->assertTrue(array_key_exists('ja', $langs));
716 $this->assertTrue(array_key_exists('en_US', $langs));
718 $translate2 = new Zend_Translate(
719 Zend_Translate
::AN_ARRAY
,
720 dirname(__FILE__
) . '/Translate/Adapter/_files/testArray/',
723 'scan' => Zend_Translate
::LOCALE_FILENAME
,
724 'ignore' => array('.', 'regex_1' => '/de_DE/', 'regex' => '/ja/')
728 $langs = $translate2->getList();
729 $this->assertFalse(array_key_exists('de_DE', $langs));
730 $this->assertFalse(array_key_exists('ja', $langs));
731 $this->assertTrue(array_key_exists('en_US', $langs));
735 * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
737 * @param integer $errno
738 * @param string $errstr
739 * @param string $errfile
740 * @param integer $errline
741 * @param array $errcontext
744 public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
746 $this->_errorOccured
= true;
750 * Custom callback for testGettingPluralsUsingOwnRule
752 * @param integer $number
755 public function customPlural($number) {
760 // Call Zend_TranslateTest::main() if this source file is executed directly.
761 if (PHPUnit_MAIN_METHOD
== "Zend_TranslateTest::main") {
762 Zend_TranslateTest
::main();