IcuFormatterTest.php 4.5 KB

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