PoFileLoaderTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Tests\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. use Symfony\Component\Translation\Loader\PoFileLoader;
  14. class PoFileLoaderTest extends TestCase
  15. {
  16. public function testLoad()
  17. {
  18. $loader = new PoFileLoader();
  19. $resource = __DIR__.'/../fixtures/resources.po';
  20. $catalogue = $loader->load($resource, 'en', 'domain1');
  21. $this->assertEquals(['foo' => 'bar'], $catalogue->all('domain1'));
  22. $this->assertEquals('en', $catalogue->getLocale());
  23. $this->assertEquals([new FileResource($resource)], $catalogue->getResources());
  24. }
  25. public function testLoadPlurals()
  26. {
  27. $loader = new PoFileLoader();
  28. $resource = __DIR__.'/../fixtures/plurals.po';
  29. $catalogue = $loader->load($resource, 'en', 'domain1');
  30. $this->assertEquals(['foo' => 'bar', 'foos' => 'bar|bars'], $catalogue->all('domain1'));
  31. $this->assertEquals('en', $catalogue->getLocale());
  32. $this->assertEquals([new FileResource($resource)], $catalogue->getResources());
  33. }
  34. public function testLoadDoesNothingIfEmpty()
  35. {
  36. $loader = new PoFileLoader();
  37. $resource = __DIR__.'/../fixtures/empty.po';
  38. $catalogue = $loader->load($resource, 'en', 'domain1');
  39. $this->assertEquals([], $catalogue->all('domain1'));
  40. $this->assertEquals('en', $catalogue->getLocale());
  41. $this->assertEquals([new FileResource($resource)], $catalogue->getResources());
  42. }
  43. /**
  44. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  45. */
  46. public function testLoadNonExistingResource()
  47. {
  48. $loader = new PoFileLoader();
  49. $resource = __DIR__.'/../fixtures/non-existing.po';
  50. $loader->load($resource, 'en', 'domain1');
  51. }
  52. public function testLoadEmptyTranslation()
  53. {
  54. $loader = new PoFileLoader();
  55. $resource = __DIR__.'/../fixtures/empty-translation.po';
  56. $catalogue = $loader->load($resource, 'en', 'domain1');
  57. $this->assertEquals(['foo' => ''], $catalogue->all('domain1'));
  58. $this->assertEquals('en', $catalogue->getLocale());
  59. $this->assertEquals([new FileResource($resource)], $catalogue->getResources());
  60. }
  61. public function testEscapedId()
  62. {
  63. $loader = new PoFileLoader();
  64. $resource = __DIR__.'/../fixtures/escaped-id.po';
  65. $catalogue = $loader->load($resource, 'en', 'domain1');
  66. $messages = $catalogue->all('domain1');
  67. $this->assertArrayHasKey('escaped "foo"', $messages);
  68. $this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
  69. }
  70. public function testEscapedIdPlurals()
  71. {
  72. $loader = new PoFileLoader();
  73. $resource = __DIR__.'/../fixtures/escaped-id-plurals.po';
  74. $catalogue = $loader->load($resource, 'en', 'domain1');
  75. $messages = $catalogue->all('domain1');
  76. $this->assertArrayHasKey('escaped "foo"', $messages);
  77. $this->assertArrayHasKey('escaped "foos"', $messages);
  78. $this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
  79. $this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']);
  80. }
  81. public function testSkipFuzzyTranslations()
  82. {
  83. $loader = new PoFileLoader();
  84. $resource = __DIR__.'/../fixtures/fuzzy-translations.po';
  85. $catalogue = $loader->load($resource, 'en', 'domain1');
  86. $messages = $catalogue->all('domain1');
  87. $this->assertArrayHasKey('foo1', $messages);
  88. $this->assertArrayNotHasKey('foo2', $messages);
  89. $this->assertArrayHasKey('foo3', $messages);
  90. }
  91. }