IcuFormatterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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;
  16. use Cake\I18n\Formatter\IcuFormatter;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * IcuFormatter tests
  20. *
  21. */
  22. class IcuFormatterTest extends TestCase
  23. {
  24. /**
  25. * Tests that variables are interpolated correctly
  26. *
  27. * @return void
  28. */
  29. public function testFormatSimple()
  30. {
  31. $formatter = new IcuFormatter();
  32. $this->assertEquals('Hello José', $formatter->format('en_US', 'Hello {0}', ['José']));
  33. $result = $formatter->format(
  34. '1 Orange',
  35. '{0, number} {1}',
  36. [1.0, 'Orange']
  37. );
  38. $this->assertEquals('1 Orange', $result);
  39. }
  40. /**
  41. * Tests that plural forms can be selected using the PO file format plural forms
  42. *
  43. * @return void
  44. */
  45. public function testFormatPlural()
  46. {
  47. $formatter = new IcuFormatter();
  48. $messages = [
  49. '{0} is 0',
  50. '{0} is 1',
  51. '{0} is 2',
  52. '{0} is 3',
  53. '{0} > 11'
  54. ];
  55. $this->assertEquals('1 is 1', $formatter->format('ar', $messages, ['_count' => 1, 1]));
  56. $this->assertEquals('2 is 2', $formatter->format('ar', $messages, ['_count' => 2, 2]));
  57. $this->assertEquals('20 > 11', $formatter->format('ar', $messages, ['_count' => 20, 20]));
  58. }
  59. /**
  60. * Tests that plurals can instead be selected using ICU's native selector
  61. *
  62. * @return void
  63. */
  64. public function testNativePluralSelection()
  65. {
  66. $formatter = new IcuFormatter();
  67. $locale = 'en_US';
  68. $string = '{0,plural,' .
  69. '=0{No fruits.}' .
  70. '=1{We have one fruit}' .
  71. 'other{We have {1} fruits}' .
  72. '}';
  73. $params = [0, 0];
  74. $expect = 'No fruits.';
  75. $actual = $formatter->format($locale, $string, $params);
  76. $this->assertSame($expect, $actual);
  77. $params = [1, 0];
  78. $expect = 'We have one fruit';
  79. $actual = $formatter->format($locale, $string, $params);
  80. $this->assertSame($expect, $actual);
  81. $params = [10, 10];
  82. $expect = 'We have 10 fruits';
  83. $actual = $formatter->format($locale, $string, $params);
  84. $this->assertSame($expect, $actual);
  85. }
  86. /**
  87. * Tests that passing a message in the wrong format will throw an exception
  88. *
  89. * @expectedException Exception
  90. * @expectedExceptionMessage msgfmt_create: message formatter
  91. * @return void
  92. */
  93. public function testBadMessageFormat()
  94. {
  95. $formatter = new IcuFormatter();
  96. $formatter->format('en_US', '{crazy format', ['some', 'vars']);
  97. }
  98. /**
  99. * Tests that strings stored inside context namespaces can also be formatted
  100. *
  101. * @return void
  102. */
  103. public function testFormatWithContext()
  104. {
  105. $messages = [
  106. 'simple' => [
  107. '_context' => [
  108. 'context a' => 'Text "a" {0}',
  109. 'context b' => 'Text "b" {0}'
  110. ]
  111. ],
  112. 'complex' => [
  113. '_context' => [
  114. 'context b' => [
  115. 0 => 'Only one',
  116. 1 => 'there are {0}'
  117. ]
  118. ]
  119. ]
  120. ];
  121. $formatter = new IcuFormatter();
  122. $this->assertEquals(
  123. 'Text "a" is good',
  124. $formatter->format('en', $messages['simple'], ['_context' => 'context a', 'is good'])
  125. );
  126. $this->assertEquals(
  127. 'Text "b" is good',
  128. $formatter->format('en', $messages['simple'], ['_context' => 'context b', 'is good'])
  129. );
  130. $this->assertEquals(
  131. 'Text "a" is good',
  132. $formatter->format('en', $messages['simple'], ['is good'])
  133. );
  134. $this->assertEquals(
  135. 'Only one',
  136. $formatter->format('en', $messages['complex'], ['_context' => 'context b', '_count' => 1])
  137. );
  138. $this->assertEquals(
  139. 'there are 2',
  140. $formatter->format('en', $messages['complex'], ['_context' => 'context b', '_count' => 2, 2])
  141. );
  142. }
  143. /**
  144. * Tests that it is possible to provide a singular fallback when passing a string message.
  145. * This is useful for getting quick feedback on the code during development instead of
  146. * having to provide all plural forms even for the default language
  147. *
  148. * @return void
  149. */
  150. public function testSingularFallback()
  151. {
  152. $formatter = new IcuFormatter();
  153. $singular = 'one thing';
  154. $plural = 'many things';
  155. $this->assertEquals($singular, $formatter->format('en_US', $plural, ['_count' => 1, '_singular' => $singular]));
  156. $this->assertEquals($plural, $formatter->format('en_US', $plural, ['_count' => 2, '_singular' => $singular]));
  157. }
  158. }