MoFileParserTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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. class MoFileParserTest extends TestCase
  22. {
  23. /**
  24. * Tests parsing a file with plurals and message context
  25. *
  26. * @return void
  27. */
  28. public function testParse()
  29. {
  30. $parser = new MoFileParser;
  31. $file = APP . 'Locale' . DS . 'rule_1_mo' . DS . 'core.mo';
  32. $messages = $parser->parse($file);
  33. $this->assertCount(3, $messages);
  34. $expected = [
  35. '%d = 1 (from core)' => [
  36. '_context' => [
  37. '' => '%d = 1 (from core translated)'
  38. ]
  39. ],
  40. '%d = 0 or > 1 (from core)' => [
  41. '_context' => [
  42. '' => [
  43. '%d = 1 (from core translated)',
  44. '%d = 0 or > 1 (from core translated)'
  45. ]
  46. ]
  47. ],
  48. 'Plural Rule 1 (from core)' => [
  49. '_context' => [
  50. '' => 'Plural Rule 1 (from core translated)'
  51. ]
  52. ]
  53. ];
  54. $this->assertEquals($expected, $messages);
  55. }
  56. /**
  57. * Tests parsing a file with single form plurals
  58. *
  59. * @return void
  60. */
  61. public function testParse0()
  62. {
  63. $parser = new MoFileParser;
  64. $file = APP . 'Locale' . DS . 'rule_0_mo' . DS . 'core.mo';
  65. $messages = $parser->parse($file);
  66. $this->assertCount(3, $messages);
  67. $expected = [
  68. 'Plural Rule 1 (from core)' => [
  69. '_context' => [
  70. '' => 'Plural Rule 0 (from core translated)'
  71. ]
  72. ],
  73. '%d = 1 (from core)' => [
  74. '_context' => [
  75. '' => '%d ends with any # (from core translated)'
  76. ]
  77. ],
  78. '%d = 0 or > 1 (from core)' => [
  79. '_context' => [
  80. '' => [
  81. '%d ends with any # (from core translated)',
  82. ]
  83. ]
  84. ],
  85. ];
  86. $this->assertEquals($expected, $messages);
  87. }
  88. /**
  89. * Tests parsing a file with larger plural forms
  90. *
  91. * @return void
  92. */
  93. public function testParse2()
  94. {
  95. $parser = new MoFileParser;
  96. $file = APP . 'Locale' . DS . 'rule_9_mo' . DS . 'core.mo';
  97. $messages = $parser->parse($file);
  98. $this->assertCount(3, $messages);
  99. $expected = [
  100. '%d = 1 (from core)' => [
  101. '_context' => [
  102. '' => '%d is 1 (from core translated)'
  103. ]
  104. ],
  105. '%d = 0 or > 1 (from core)' => [
  106. '_context' => [
  107. '' => [
  108. '%d is 1 (from core translated)',
  109. '%d ends in 2-4, not 12-14 (from core translated)',
  110. '%d everything else (from core translated)'
  111. ]
  112. ]
  113. ],
  114. 'Plural Rule 1 (from core)' => [
  115. '_context' => [
  116. '' => 'Plural Rule 9 (from core translated)'
  117. ]
  118. ]
  119. ];
  120. $this->assertEquals($expected, $messages);
  121. }
  122. /**
  123. * Tests parsing a file with plurals and message context
  124. *
  125. * @return void
  126. */
  127. public function testParseFull()
  128. {
  129. $parser = new MoFileParser;
  130. $file = APP . 'Locale' . DS . 'rule_0_mo' . DS . 'default.mo';
  131. $messages = $parser->parse($file);
  132. $this->assertCount(5, $messages);
  133. $expected = [
  134. 'Plural Rule 1' => [
  135. '_context' => [
  136. '' => 'Plural Rule 1 (translated)'
  137. ]
  138. ],
  139. '%d = 1' => [
  140. '_context' => [
  141. 'This is the context' => 'First Context trasnlation',
  142. 'Another Context' => '%d = 1 (translated)'
  143. ]
  144. ],
  145. '%d = 0 or > 1' => [
  146. '_context' => [
  147. 'Another Context' => [
  148. 0 => '%d = 1 (translated)',
  149. 1 => '%d = 0 or > 1 (translated)'
  150. ]
  151. ]
  152. ],
  153. '%-5d = 1' => [
  154. '_context' => [
  155. '' => '%-5d = 1 (translated)'
  156. ]
  157. ],
  158. '%-5d = 0 or > 1' => [
  159. '_context' => [
  160. '' => [
  161. '%-5d = 1 (translated)',
  162. '%-5d = 0 or > 1 (translated)'
  163. ]
  164. ]
  165. ]
  166. ];
  167. $this->assertEquals($expected, $messages);
  168. }
  169. }