MessageTranslatorTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Elgg\I18n;
  3. use PHPUnit_Framework_TestCase as TestCase;
  4. class MessageTranslatorTest extends TestCase {
  5. public function setUp() {
  6. $this->english = Locale::parse('en');
  7. $this->spanish = Locale::parse('es');
  8. }
  9. public function testKeyIsReturnedIfNoTranslationCanBeFound() {
  10. $messages = new ArrayMessageBundle([]);
  11. $translator = new MessageTranslator(Locale::parse('en'), $messages);
  12. $this->assertEquals('foobar', $translator->translate('foobar'));
  13. }
  14. public function testTranslateReturnsTranslationForSpecifiedLocaleIfAvailable() {
  15. $messages = new ArrayMessageBundle([
  16. 'en' => ['one' => 'one'],
  17. 'es' => ['one' => 'uno'],
  18. ]);
  19. $translator = new MessageTranslator(Locale::parse('en'), $messages);
  20. $this->assertEquals('uno', $translator->translate('one', [], Locale::parse('es')));
  21. }
  22. public function testTranslateReturnsTranslationForDefaultLocaleIfNoLocaleWasSpecified() {
  23. $messages = new ArrayMessageBundle([
  24. 'en' => ['one' => 'one'],
  25. 'es' => ['one' => 'uno'],
  26. ]);
  27. $translator = new MessageTranslator(Locale::parse('en'), $messages);
  28. $this->assertEquals('one', $translator->translate('one', []));
  29. }
  30. public function testFallsBackToLanguageIfTranslationForSpecifiedLanguageIsNotAvailable() {
  31. $messages = new ArrayMessageBundle([
  32. 'en' => ['one' => 'one'],
  33. ]);
  34. $translator = new MessageTranslator(Locale::parse('en'), $messages);
  35. $this->assertEquals('one', $translator->translate('one', [], Locale::parse('es')));
  36. }
  37. }