MoFileParserTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Cake\I18n\Parser\MoFileParser;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests the MoFileLoader
  20. *
  21. */
  22. class MoFileParserTest extends TestCase
  23. {
  24. /**
  25. * Tests parsing a file with plurals and message context
  26. *
  27. * @return void
  28. */
  29. public function testParse()
  30. {
  31. $parser = new MoFileParser;
  32. $file = APP . 'Locale' . DS . 'rule_1_mo' . DS . 'core.mo';
  33. $messages = $parser->parse($file);
  34. $this->assertCount(3, $messages);
  35. $expected = [
  36. '%d = 1 (from core)' => '%d = 1 (from core translated)',
  37. '%d = 0 or > 1 (from core)' => [
  38. '%d = 1 (from core translated)',
  39. '%d = 0 or > 1 (from core translated)'
  40. ],
  41. 'Plural Rule 1 (from core)' => 'Plural Rule 1 (from core translated)'
  42. ];
  43. $this->assertEquals($expected, $messages);
  44. }
  45. /**
  46. * Tests parsing a file with larger plural forms
  47. *
  48. * @return void
  49. */
  50. public function testParse2()
  51. {
  52. $parser = new MoFileParser;
  53. $file = APP . 'Locale' . DS . 'rule_9_mo' . DS . 'core.mo';
  54. $messages = $parser->parse($file);
  55. $this->assertCount(3, $messages);
  56. $expected = [
  57. '%d = 1 (from core)' => '%d is 1 (from core translated)',
  58. '%d = 0 or > 1 (from core)' => [
  59. '%d is 1 (from core translated)',
  60. '%d ends in 2-4, not 12-14 (from core translated)',
  61. '%d everything else (from core translated)'
  62. ],
  63. 'Plural Rule 1 (from core)' => 'Plural Rule 9 (from core translated)'
  64. ];
  65. $this->assertEquals($expected, $messages);
  66. }
  67. /**
  68. * Tests parsing a file with plurals and message context
  69. *
  70. * @return void
  71. */
  72. public function testParseFull()
  73. {
  74. $parser = new MoFileParser;
  75. $file = APP . 'Locale' . DS . 'rule_0_mo' . DS . 'default.mo';
  76. $messages = $parser->parse($file);
  77. $this->assertCount(5, $messages);
  78. $expected = [
  79. 'Plural Rule 1' => 'Plural Rule 1 (translated)',
  80. '%d = 1' => [
  81. '_context' => [
  82. 'This is the context' => 'First Context trasnlation',
  83. 'Another Context' => '%d = 1 (translated)'
  84. ]
  85. ],
  86. '%d = 0 or > 1' => [
  87. '_context' => [
  88. 'Another Context' => [
  89. 0 => '%d = 1 (translated)',
  90. 1 => '%d = 0 or > 1 (translated)'
  91. ]
  92. ]
  93. ],
  94. '%-5d = 1' => '%-5d = 1 (translated)',
  95. '%-5d = 0 or > 1' => [
  96. '%-5d = 1 (translated)',
  97. '%-5d = 0 or > 1 (translated)'
  98. ]
  99. ];
  100. $this->assertEquals($expected, $messages);
  101. }
  102. }