菜谱项目

DataCollectorTranslatorTest.php 3.1KB

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