PoFileParserTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\I18n\Parser;
  16. use Aura\Intl\Package;
  17. use Cake\I18n\I18n;
  18. use Cake\I18n\Parser\PoFileParser;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests the PoFileLoader
  22. *
  23. */
  24. class PoFileParserTest extends TestCase
  25. {
  26. /**
  27. * Tests parsing a file with plurals and message context
  28. *
  29. * @return void
  30. */
  31. public function testParse()
  32. {
  33. $parser = new PoFileParser;
  34. $file = APP . 'Locale' . DS . 'rule_1_po' . DS . 'default.po';
  35. $messages = $parser->parse($file);
  36. $this->assertCount(5, $messages);
  37. $expected = [
  38. 'Plural Rule 1' => 'Plural Rule 1 (translated)',
  39. '%d = 1' => [
  40. '_context' => [
  41. 'This is the context' => 'First Context trasnlation',
  42. 'Another Context' => '%d = 1 (translated)'
  43. ]
  44. ],
  45. '%d = 0 or > 1' => [
  46. '_context' => [
  47. 'Another Context' => [
  48. 0 => '%d = 1 (translated)',
  49. 1 => '%d = 0 or > 1 (translated)'
  50. ]
  51. ]
  52. ],
  53. '%-5d = 1' => '%-5d = 1 (translated)',
  54. '%-5d = 0 or > 1' => [
  55. 0 => '%-5d = 1 (translated)',
  56. 1 => '',
  57. 2 => '',
  58. 3 => '',
  59. 4 => '%-5d = 0 or > 1 (translated)'
  60. ]
  61. ];
  62. $this->assertEquals($expected, $messages);
  63. }
  64. /**
  65. * Tests parsing a file with multiline keys and values
  66. *
  67. * @return void
  68. */
  69. public function testParseMultiLine()
  70. {
  71. $parser = new PoFileParser;
  72. $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
  73. $messages = $parser->parse($file);
  74. $this->assertCount(12, $messages);
  75. $this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]);
  76. }
  77. /**
  78. * Test parsing a file with quoted strings
  79. *
  80. * @return void
  81. */
  82. public function testQuotedString()
  83. {
  84. $parser = new PoFileParser;
  85. $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
  86. $messages = $parser->parse($file);
  87. $this->assertTextEquals('this is a "quoted string" (translated)', $messages['this is a "quoted string"']);
  88. }
  89. /**
  90. * Test parsing a file with message context on some msgid values.
  91. *
  92. * This behavior is not ideal, but more thorough solutions
  93. * would break compatibility. Perhaps this is something we can
  94. * reconsider in 4.x
  95. *
  96. * @return void
  97. */
  98. public function testParseContextOnSomeMessages()
  99. {
  100. $parser = new PoFileParser();
  101. $file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
  102. $messages = $parser->parse($file);
  103. I18n::translator('default', 'en_US', function () use ($messages) {
  104. $package = new Package('default');
  105. $package->setMessages($messages);
  106. return $package;
  107. });
  108. $this->assertTextEquals('En cours', $messages['Pending']);
  109. $this->assertTextEquals('En resolved', $messages['Resolved']);
  110. }
  111. }