XliffFileDumperTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\Dumper\XliffFileDumper;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. class XliffFileDumperTest extends TestCase
  15. {
  16. public function testFormatCatalogue()
  17. {
  18. $catalogue = new MessageCatalogue('en_US');
  19. $catalogue->add([
  20. 'foo' => 'bar',
  21. 'key' => '',
  22. 'key.with.cdata' => '<source> & <target>',
  23. ]);
  24. $catalogue->setMetadata('foo', ['notes' => [['priority' => 1, 'from' => 'bar', 'content' => 'baz']]]);
  25. $catalogue->setMetadata('key', ['notes' => [['content' => 'baz'], ['content' => 'qux']]]);
  26. $dumper = new XliffFileDumper();
  27. $this->assertStringEqualsFile(
  28. __DIR__.'/../fixtures/resources-clean.xlf',
  29. $dumper->formatCatalogue($catalogue, 'messages', ['default_locale' => 'fr_FR'])
  30. );
  31. }
  32. public function testFormatCatalogueXliff2()
  33. {
  34. $catalogue = new MessageCatalogue('en_US');
  35. $catalogue->add([
  36. 'foo' => 'bar',
  37. 'key' => '',
  38. 'key.with.cdata' => '<source> & <target>',
  39. ]);
  40. $catalogue->setMetadata('key', ['target-attributes' => ['order' => 1]]);
  41. $dumper = new XliffFileDumper();
  42. $this->assertStringEqualsFile(
  43. __DIR__.'/../fixtures/resources-2.0-clean.xlf',
  44. $dumper->formatCatalogue($catalogue, 'messages', ['default_locale' => 'fr_FR', 'xliff_version' => '2.0'])
  45. );
  46. }
  47. public function testFormatIcuCatalogueXliff2()
  48. {
  49. $catalogue = new MessageCatalogue('en_US');
  50. $catalogue->add([
  51. 'foo' => 'bar',
  52. ], 'messages'.MessageCatalogue::INTL_DOMAIN_SUFFIX);
  53. $dumper = new XliffFileDumper();
  54. $this->assertStringEqualsFile(
  55. __DIR__.'/../fixtures/resources-2.0+intl-icu.xlf',
  56. $dumper->formatCatalogue($catalogue, 'messages'.MessageCatalogue::INTL_DOMAIN_SUFFIX, ['default_locale' => 'fr_FR', 'xliff_version' => '2.0'])
  57. );
  58. }
  59. public function testFormatCatalogueWithCustomToolInfo()
  60. {
  61. $options = [
  62. 'default_locale' => 'en_US',
  63. 'tool_info' => ['tool-id' => 'foo', 'tool-name' => 'foo', 'tool-version' => '0.0', 'tool-company' => 'Foo'],
  64. ];
  65. $catalogue = new MessageCatalogue('en_US');
  66. $catalogue->add(['foo' => 'bar']);
  67. $dumper = new XliffFileDumper();
  68. $this->assertStringEqualsFile(
  69. __DIR__.'/../fixtures/resources-tool-info.xlf',
  70. $dumper->formatCatalogue($catalogue, 'messages', $options)
  71. );
  72. }
  73. public function testFormatCatalogueWithTargetAttributesMetadata()
  74. {
  75. $catalogue = new MessageCatalogue('en_US');
  76. $catalogue->add([
  77. 'foo' => 'bar',
  78. ]);
  79. $catalogue->setMetadata('foo', ['target-attributes' => ['state' => 'needs-translation']]);
  80. $dumper = new XliffFileDumper();
  81. $this->assertStringEqualsFile(
  82. __DIR__.'/../fixtures/resources-target-attributes.xlf',
  83. $dumper->formatCatalogue($catalogue, 'messages', ['default_locale' => 'fr_FR'])
  84. );
  85. }
  86. public function testFormatCatalogueWithNotesMetadata()
  87. {
  88. $catalogue = new MessageCatalogue('en_US');
  89. $catalogue->add([
  90. 'foo' => 'bar',
  91. 'baz' => 'biz',
  92. ]);
  93. $catalogue->setMetadata('foo', ['notes' => [
  94. ['category' => 'state', 'content' => 'new'],
  95. ['category' => 'approved', 'content' => 'true'],
  96. ['category' => 'section', 'content' => 'user login', 'priority' => '1'],
  97. ]]);
  98. $catalogue->setMetadata('baz', ['notes' => [
  99. ['id' => 'x', 'content' => 'x_content'],
  100. ['appliesTo' => 'target', 'category' => 'quality', 'content' => 'Fuzzy'],
  101. ]]);
  102. $dumper = new XliffFileDumper();
  103. $this->assertStringEqualsFile(
  104. __DIR__.'/../fixtures/resources-notes-meta.xlf',
  105. $dumper->formatCatalogue($catalogue, 'messages', ['default_locale' => 'fr_FR', 'xliff_version' => '2.0'])
  106. );
  107. }
  108. }