DataCollectorTranslatorTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\DataCollectorTranslator;
  13. use Symfony\Component\Translation\Loader\ArrayLoader;
  14. use Symfony\Component\Translation\Translator;
  15. class DataCollectorTranslatorTest extends TestCase
  16. {
  17. public function testCollectMessages()
  18. {
  19. $collector = $this->createCollector();
  20. $collector->setFallbackLocales(['fr', 'ru']);
  21. $collector->trans('foo');
  22. $collector->trans('bar');
  23. $collector->trans('choice', ['%count%' => 0]);
  24. $collector->trans('bar_ru');
  25. $collector->trans('bar_ru', ['foo' => 'bar']);
  26. $expectedMessages = [];
  27. $expectedMessages[] = [
  28. 'id' => 'foo',
  29. 'translation' => 'foo (en)',
  30. 'locale' => 'en',
  31. 'domain' => 'messages',
  32. 'state' => DataCollectorTranslator::MESSAGE_DEFINED,
  33. 'parameters' => [],
  34. 'transChoiceNumber' => null,
  35. ];
  36. $expectedMessages[] = [
  37. 'id' => 'bar',
  38. 'translation' => 'bar (fr)',
  39. 'locale' => 'fr',
  40. 'domain' => 'messages',
  41. 'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
  42. 'parameters' => [],
  43. 'transChoiceNumber' => null,
  44. ];
  45. $expectedMessages[] = [
  46. 'id' => 'choice',
  47. 'translation' => 'choice',
  48. 'locale' => 'en',
  49. 'domain' => 'messages',
  50. 'state' => DataCollectorTranslator::MESSAGE_MISSING,
  51. 'parameters' => ['%count%' => 0],
  52. 'transChoiceNumber' => 0,
  53. ];
  54. $expectedMessages[] = [
  55. 'id' => 'bar_ru',
  56. 'translation' => 'bar (ru)',
  57. 'locale' => 'ru',
  58. 'domain' => 'messages',
  59. 'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
  60. 'parameters' => [],
  61. 'transChoiceNumber' => null,
  62. ];
  63. $expectedMessages[] = [
  64. 'id' => 'bar_ru',
  65. 'translation' => 'bar (ru)',
  66. 'locale' => 'ru',
  67. 'domain' => 'messages',
  68. 'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
  69. 'parameters' => ['foo' => 'bar'],
  70. 'transChoiceNumber' => null,
  71. ];
  72. $this->assertEquals($expectedMessages, $collector->getCollectedMessages());
  73. }
  74. /**
  75. * @group legacy
  76. */
  77. public function testCollectMessagesTransChoice()
  78. {
  79. $collector = $this->createCollector();
  80. $collector->setFallbackLocales(['fr', 'ru']);
  81. $collector->transChoice('choice', 0);
  82. $expectedMessages = [];
  83. $expectedMessages[] = [
  84. 'id' => 'choice',
  85. 'translation' => 'choice',
  86. 'locale' => 'en',
  87. 'domain' => 'messages',
  88. 'state' => DataCollectorTranslator::MESSAGE_MISSING,
  89. 'parameters' => ['%count%' => 0],
  90. 'transChoiceNumber' => 0,
  91. ];
  92. $this->assertEquals($expectedMessages, $collector->getCollectedMessages());
  93. }
  94. private function createCollector()
  95. {
  96. $translator = new Translator('en');
  97. $translator->addLoader('array', new ArrayLoader());
  98. $translator->addResource('array', ['foo' => 'foo (en)'], 'en');
  99. $translator->addResource('array', ['bar' => 'bar (fr)'], 'fr');
  100. $translator->addResource('array', ['bar_ru' => 'bar (ru)'], 'ru');
  101. return new DataCollectorTranslator($translator);
  102. }
  103. }