PoFileParserTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 Aura\Intl\Package;
  17. use Cake\Cache\Cache;
  18. use Cake\I18n\I18n;
  19. use Cake\I18n\Parser\PoFileParser;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Tests the PoFileLoader
  23. */
  24. class PoFileParserTest extends TestCase
  25. {
  26. protected $locale;
  27. /**
  28. * Set Up
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->locale = I18n::locale();
  36. }
  37. /**
  38. * Tear down method
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. parent::tearDown();
  45. I18n::clear();
  46. I18n::locale($this->locale);
  47. Cache::clear(false, '_cake_core_');
  48. }
  49. /**
  50. * Tests parsing a file with plurals and message context
  51. *
  52. * @return void
  53. */
  54. public function testParse()
  55. {
  56. $parser = new PoFileParser;
  57. $file = APP . 'Locale' . DS . 'rule_1_po' . DS . 'default.po';
  58. $messages = $parser->parse($file);
  59. $this->assertCount(8, $messages);
  60. $expected = [
  61. 'Plural Rule 1' => [
  62. '_context' => [
  63. '' => 'Plural Rule 1 (translated)'
  64. ]
  65. ],
  66. '%d = 1' => [
  67. '_context' => [
  68. 'This is the context' => 'First Context translation',
  69. 'Another Context' => '%d = 1 (translated)'
  70. ]
  71. ],
  72. 'p:%d = 0 or > 1' => [
  73. '_context' => [
  74. 'Another Context' => [
  75. 0 => '%d = 1 (translated)',
  76. 1 => '%d = 0 or > 1 (translated)'
  77. ]
  78. ]
  79. ],
  80. '%-5d = 1' => [
  81. '_context' => [
  82. '' => '%-5d = 1 (translated)'
  83. ]
  84. ],
  85. 'p:%-5d = 0 or > 1' => [
  86. '_context' => [
  87. '' => [
  88. 0 => '%-5d = 1 (translated)',
  89. 1 => '',
  90. 2 => '',
  91. 3 => '',
  92. 4 => '%-5d = 0 or > 1 (translated)'
  93. ]
  94. ]
  95. ],
  96. '%d = 2' => [
  97. '_context' => [
  98. 'This is another translated context' => 'First Context translation',
  99. ]
  100. ],
  101. '%-6d = 3' => [
  102. '_context' => [
  103. '' => '%-6d = 1 (translated)',
  104. ]
  105. ],
  106. 'p:%-6d = 0 or > 1' => [
  107. '_context' => [
  108. '' => [
  109. 0 => '%-6d = 1 (translated)',
  110. 1 => '',
  111. 2 => '',
  112. 3 => '',
  113. 4 => '%-6d = 0 or > 1 (translated)',
  114. ]
  115. ]
  116. ],
  117. ];
  118. $this->assertEquals($expected, $messages);
  119. }
  120. /**
  121. * Tests parsing a file with multiline keys and values
  122. *
  123. * @return void
  124. */
  125. public function testParseMultiLine()
  126. {
  127. $parser = new PoFileParser;
  128. $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
  129. $messages = $parser->parse($file);
  130. $this->assertCount(12, $messages);
  131. $this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]['_context']['']);
  132. }
  133. /**
  134. * Test parsing a file with quoted strings
  135. *
  136. * @return void
  137. */
  138. public function testQuotedString()
  139. {
  140. $parser = new PoFileParser;
  141. $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
  142. $messages = $parser->parse($file);
  143. $this->assertTextEquals('this is a "quoted string" (translated)', $messages['this is a "quoted string"']['_context']['']);
  144. }
  145. /**
  146. * Test parsing a file with message context on some msgid values.
  147. *
  148. * This behavior is not ideal, but more thorough solutions
  149. * would break compatibility. Perhaps this is something we can
  150. * reconsider in 4.x
  151. *
  152. * @return void
  153. */
  154. public function testParseContextOnSomeMessages()
  155. {
  156. $parser = new PoFileParser();
  157. $file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
  158. $messages = $parser->parse($file);
  159. I18n::translator('default', 'en_CA', function () use ($messages) {
  160. $package = new Package('default');
  161. $package->setMessages($messages);
  162. return $package;
  163. });
  164. $this->assertSame('En cours', $messages['Pending']['_context']['']);
  165. $this->assertSame('En cours - context', $messages['Pending']['_context']['Pay status']);
  166. $this->assertSame('En resolved', $messages['Resolved']['_context']['']);
  167. $this->assertSame('En resolved - context', $messages['Resolved']['_context']['Pay status']);
  168. // Confirm actual behavior
  169. I18n::locale('en_CA');
  170. $this->assertSame('En cours', __('Pending'));
  171. $this->assertSame('En cours - context', __x('Pay status', 'Pending'));
  172. $this->assertSame('En resolved', __('Resolved'));
  173. $this->assertSame('En resolved - context', __x('Pay status', 'Resolved'));
  174. }
  175. /**
  176. * Test parsing context based messages
  177. *
  178. * @return void
  179. */
  180. public function testParseContextMessages()
  181. {
  182. $parser = new PoFileParser();
  183. $file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
  184. $messages = $parser->parse($file);
  185. I18n::translator('default', 'en_US', function () use ($messages) {
  186. $package = new Package('default');
  187. $package->setMessages($messages);
  188. return $package;
  189. });
  190. // Check translated messages
  191. I18n::locale('en_US');
  192. $this->assertSame('Titel mit Kontext', __x('context', 'title'));
  193. $this->assertSame('Titel mit anderem Kontext', __x('another_context', 'title'));
  194. $this->assertSame('Titel ohne Kontext', __('title'));
  195. }
  196. /**
  197. * Test parsing plurals
  198. *
  199. * @return void
  200. */
  201. public function testPlurals()
  202. {
  203. $parser = new PoFileParser();
  204. $file = APP . 'Locale' . DS . 'de' . DS . 'wa.po';
  205. $messages = $parser->parse($file);
  206. I18n::translator('default', 'de_DE', function () use ($messages) {
  207. $package = new Package('default');
  208. $package->setMessages($messages);
  209. return $package;
  210. });
  211. // Check translated messages
  212. I18n::locale('de_DE');
  213. $this->assertEquals('Standorte', __d('wa', 'Locations'));
  214. I18n::locale('en_EN');
  215. $this->assertEquals('Locations', __d('wa', 'Locations'));
  216. }
  217. }