I18nTest.php 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  22. // use Jchook\AssertThrows\AssertThrows;
  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. // $this->assertThrows(\Exception::class,
  55. // function () use ($pronouns) { _m($pronouns, ['pronoun' => 'unknown']); });
  56. $pronouns = ['she' => 'her apple', 'he' => 'his apple', 'they' => 'their apple', 'someone\'s apple'];
  57. static::assertSame('someone\'s apple', _m($pronouns, ['pronoun' => 'unknown']));
  58. $complex = [
  59. 'she' => [1 => 'her apple', 'her # apples'],
  60. 'he' => [1 => 'his apple', 'his # apples'],
  61. ];
  62. static::assertSame('her apple', _m($complex, ['pronoun' => 'she', 'count' => 1]));
  63. static::assertSame('his apple', _m($complex, ['pronoun' => 'he', 'count' => 1]));
  64. static::assertSame('her 2 apples', _m($complex, ['pronoun' => 'she', 'count' => 2]));
  65. static::assertSame('his 2 apples', _m($complex, ['pronoun' => 'he', 'count' => 2]));
  66. static::assertSame('her 42 apples', _m($complex, ['pronoun' => 'she', 'count' => 42]));
  67. $complex = [
  68. 'she' => [1 => 'her apple', 'her # apples'],
  69. 'he' => [1 => 'his apple', 'his # apples'],
  70. 'their' => [1 => 'their apple', 'their # apples'],
  71. ];
  72. static::assertSame('her apple', _m($complex, ['pronoun' => 'she', 'count' => 1]));
  73. static::assertSame('his apple', _m($complex, ['pronoun' => 'he', 'count' => 1]));
  74. static::assertSame('her 2 apples', _m($complex, ['pronoun' => 'she', 'count' => 2]));
  75. static::assertSame('his 2 apples', _m($complex, ['pronoun' => 'he', 'count' => 2]));
  76. static::assertSame('her 42 apples', _m($complex, ['pronoun' => 'she', 'count' => 42]));
  77. static::assertSame('their apple', _m($complex, ['pronoun' => 'they', 'count' => 1]));
  78. static::assertSame('their 3 apples', _m($complex, ['pronoun' => 'they', 'count' => 3]));
  79. }
  80. }