I18nTest.php 5.5 KB

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