DataCollectorTranslatorTest.php 3.2 KB

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