I18nTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Core\I18n;
  20. use function App\Core\I18n\_m;
  21. use App\Core\I18n\I18n;
  22. use InvalidArgumentException;
  23. use Jchook\AssertThrows\AssertThrows;
  24. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  25. class I18nTest extends KernelTestCase
  26. {
  27. use AssertThrows;
  28. public function testM()
  29. {
  30. static::bootKernel();
  31. $translator = static::$container->get('translator');
  32. I18n::setTranslator($translator);
  33. static::assertSame('test string', _m('test string'));
  34. static::assertSame('test string', _m('test {thing}', ['thing' => 'string']));
  35. }
  36. public function testICU()
  37. {
  38. static::bootKernel();
  39. $translator = static::$container->get('translator');
  40. I18n::setTranslator($translator);
  41. $apples = [1 => '1 apple', '# apples'];
  42. static::assertSame('-42 apples', _m($apples, ['count' => -42]));
  43. static::assertSame('0 apples', _m($apples, ['count' => 0]));
  44. static::assertSame('1 apple', _m($apples, ['count' => 1]));
  45. static::assertSame('2 apples', _m($apples, ['count' => 2]));
  46. static::assertSame('42 apples', _m($apples, ['count' => 42]));
  47. $apples = [0 => 'no apples', 1 => '1 apple', '# apples'];
  48. static::assertSame('no apples', _m($apples, ['count' => 0]));
  49. static::assertSame('1 apple', _m($apples, ['count' => 1]));
  50. static::assertSame('2 apples', _m($apples, ['count' => 2]));
  51. static::assertSame('42 apples', _m($apples, ['count' => 42]));
  52. $pronouns = ['she' => 'her apple', 'he' => 'his apple', 'they' => 'their apple'];
  53. static::assertSame('her apple', _m($pronouns, ['pronoun' => 'she']));
  54. static::assertSame('his apple', _m($pronouns, ['pronoun' => 'he']));
  55. static::assertSame('their apple', _m($pronouns, ['pronoun' => 'they']));
  56. static::assertSame('their apple', _m($pronouns, ['pronoun' => 'unkown'])); // a bit odd, not sure if we want this
  57. $pronouns = ['she' => 'her apple', 'he' => 'his apple', 'they' => 'their apple', 'someone\'s apple'];
  58. static::assertSame('someone\'s apple', _m($pronouns, ['pronoun' => 'unknown']));
  59. $complex = [
  60. 'she' => [1 => 'her apple', 'her # apples'],
  61. 'he' => [1 => 'his apple', 'his # apples'],
  62. ];
  63. static::assertSame('her apple', _m($complex, ['pronoun' => 'she', 'count' => 1]));
  64. static::assertSame('his apple', _m($complex, ['pronoun' => 'he', 'count' => 1]));
  65. static::assertSame('her 2 apples', _m($complex, ['pronoun' => 'she', 'count' => 2]));
  66. static::assertSame('his 2 apples', _m($complex, ['pronoun' => 'he', 'count' => 2]));
  67. static::assertSame('her 42 apples', _m($complex, ['pronoun' => 'she', 'count' => 42]));
  68. $complex = [
  69. 'she' => [1 => 'her apple', 'her # apples'],
  70. 'he' => [1 => 'his apple', 'his # apples'],
  71. 'their' => [1 => 'their apple', 'their # apples'],
  72. ];
  73. static::assertSame('her apple', _m($complex, ['pronoun' => 'she', 'count' => 1]));
  74. static::assertSame('his apple', _m($complex, ['pronoun' => 'he', 'count' => 1]));
  75. static::assertSame('her 2 apples', _m($complex, ['pronoun' => 'she', 'count' => 2]));
  76. static::assertSame('his 2 apples', _m($complex, ['pronoun' => 'he', 'count' => 2]));
  77. static::assertSame('her 42 apples', _m($complex, ['pronoun' => 'she', 'count' => 42]));
  78. static::assertSame('their apple', _m($complex, ['pronoun' => 'they', 'count' => 1]));
  79. static::assertSame('their 3 apples', _m($complex, ['pronoun' => 'they', 'count' => 3]));
  80. static::assertThrows(InvalidArgumentException::class, fn () => _m($apples, ['count' => []]));
  81. static::assertThrows(InvalidArgumentException::class, fn () => _m([1], ['foo' => 'bar']));
  82. }
  83. public function testIsRTL()
  84. {
  85. static::assertFalse(I18n::isRTL('af'));
  86. static::assertTrue(I18n::isRTL('ar'));
  87. static::assertThrows(InvalidArgumentException::class, fn () => I18n::isRTL(''));
  88. static::assertThrows(InvalidArgumentException::class, fn () => I18n::isRTL('not a language'));
  89. }
  90. public function testGetNiceList()
  91. {
  92. static::assertIsArray(I18n::getNiceLanguageList());
  93. }
  94. public function testClientPreferredLanguage()
  95. {
  96. static::assertSame('fr', I18n::clientPreferredLanguage('Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5'));
  97. static::assertSame('de', I18n::clientPreferredLanguage('Accept-Language: de'));
  98. static::assertSame('de', I18n::clientPreferredLanguage('Accept-Language: de-CH'));
  99. static::assertSame('en', I18n::clientPreferredLanguage('Accept-Language: en-US,en;q=0.5'));
  100. static::assertFalse(I18n::clientPreferredLanguage('Accept-Language: some-language'));
  101. }
  102. }