PoFileParserTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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::getLocale();
  36. }
  37. /**
  38. * Tear down method
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. parent::tearDown();
  45. I18n::clear();
  46. I18n::setLocale($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(5, $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 trasnlation',
  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. ];
  97. $this->assertEquals($expected, $messages);
  98. }
  99. /**
  100. * Tests parsing a file with multiline keys and values
  101. *
  102. * @return void
  103. */
  104. public function testParseMultiLine()
  105. {
  106. $parser = new PoFileParser;
  107. $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
  108. $messages = $parser->parse($file);
  109. $this->assertCount(12, $messages);
  110. $this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]['_context']['']);
  111. }
  112. /**
  113. * Test parsing a file with quoted strings
  114. *
  115. * @return void
  116. */
  117. public function testQuotedString()
  118. {
  119. $parser = new PoFileParser;
  120. $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
  121. $messages = $parser->parse($file);
  122. $this->assertTextEquals('this is a "quoted string" (translated)', $messages['this is a "quoted string"']['_context']['']);
  123. }
  124. /**
  125. * Test parsing a file with message context on some msgid values.
  126. *
  127. * This behavior is not ideal, but more thorough solutions
  128. * would break compatibility. Perhaps this is something we can
  129. * reconsider in 4.x
  130. *
  131. * @return void
  132. */
  133. public function testParseContextOnSomeMessages()
  134. {
  135. $parser = new PoFileParser();
  136. $file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
  137. $messages = $parser->parse($file);
  138. I18n::setTranslator('default', function () use ($messages) {
  139. $package = new Package('default');
  140. $package->setMessages($messages);
  141. return $package;
  142. }, 'en_CA');
  143. $this->assertSame('En cours', $messages['Pending']['_context']['']);
  144. $this->assertSame('En cours - context', $messages['Pending']['_context']['Pay status']);
  145. $this->assertSame('En resolved', $messages['Resolved']['_context']['']);
  146. $this->assertSame('En resolved - context', $messages['Resolved']['_context']['Pay status']);
  147. // Confirm actual behavior
  148. I18n::setLocale('en_CA');
  149. $this->assertSame('En cours', __('Pending'));
  150. $this->assertSame('En cours - context', __x('Pay status', 'Pending'));
  151. $this->assertSame('En resolved', __('Resolved'));
  152. $this->assertSame('En resolved - context', __x('Pay status', 'Resolved'));
  153. }
  154. /**
  155. * Test parsing context based messages
  156. *
  157. * @return void
  158. */
  159. public function testParseContextMessages()
  160. {
  161. $parser = new PoFileParser();
  162. $file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
  163. $messages = $parser->parse($file);
  164. I18n::setTranslator('default', function () use ($messages) {
  165. $package = new Package('default');
  166. $package->setMessages($messages);
  167. return $package;
  168. }, 'en_US');
  169. // Check translated messages
  170. I18n::setLocale('en_US');
  171. $this->assertSame('Titel mit Kontext', __x('context', 'title'));
  172. $this->assertSame('Titel mit anderem Kontext', __x('another_context', 'title'));
  173. $this->assertSame('Titel ohne Kontext', __('title'));
  174. }
  175. /**
  176. * Test parsing plurals
  177. *
  178. * @return void
  179. */
  180. public function testPlurals()
  181. {
  182. $parser = new PoFileParser();
  183. $file = APP . 'Locale' . DS . 'de' . DS . 'wa.po';
  184. $messages = $parser->parse($file);
  185. I18n::translator('default', 'de_DE', function () use ($messages) {
  186. $package = new Package('default');
  187. $package->setMessages($messages);
  188. return $package;
  189. });
  190. // Check translated messages
  191. I18n::locale('de_DE');
  192. $this->assertEquals('Standorte', __d('wa', 'Locations'));
  193. I18n::locale('en_EN');
  194. $this->assertEquals('Locations', __d('wa', 'Locations'));
  195. }
  196. }