Skip to content

Commit

Permalink
Add tests for plural rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
rob006 authored and SychO9 committed Oct 23, 2024
1 parent d7092fd commit 8b17a4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
17 changes: 14 additions & 3 deletions framework/core/js/tests/unit/common/utils/Translator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ test('placeholders encoding', () => {
const translator = new Translator();
translator.setLocale('en');
translator.addTranslations({
'test1': "test1 {placeholder} test1",
'test2': "test2 {placeholder} test2",
'test1': 'test1 {placeholder} test1',
'test2': 'test2 {placeholder} test2',
});

expect(extractText(translator.trans('test1', {'{placeholder}': "'"}))).toBe("test1 ' test1");
Expand All @@ -22,7 +22,7 @@ test('missing placeholders', () => {
const translator = new Translator();
translator.setLocale('en');
translator.addTranslations({
'test1': "test1 {placeholder} test1",
'test1': 'test1 {placeholder} test1',
});

expect(extractText(translator.trans('test1', {}))).toBe('test1 {placeholder} test1');
Expand All @@ -37,3 +37,14 @@ test('escaped placeholders', () => {

expect(extractText(translator.trans('test3', {placeholder: "'"}))).toBe("test1 ' {placeholder} test1");
});

test('plural rules', () => {
const translator = new Translator();
translator.setLocale('en');
translator.addTranslations({
'test4': '{pageNumber, plural, =1 {{forumName}} other {Page # - {forumName}}}',
});

expect(extractText(translator.trans('test4', {forumName: 'A & B', pageNumber: 1}))).toBe('A & B');
expect(extractText(translator.trans('test4', {forumName: 'A & B', pageNumber: 2}))).toBe('Page 2 - A & B');
});
13 changes: 13 additions & 0 deletions framework/core/tests/unit/Locale/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ public function escaped_placeholders()

$this->assertSame("test1 ' {placeholder} test1", $translator->trans('test3', ['placeholder' => "'"]));
}

/** @test */
public function plural_rules()
{
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', [
'test4' => '{pageNumber, plural, =1 {{forumName}} other {Page # - {forumName}}}',
], 'en', self::DOMAIN);

$this->assertSame('A & B', $translator->trans('test4', ['forumName' => 'A & B', 'pageNumber' => 1]));
$this->assertSame('Page 2 - A & B', $translator->trans('test4', ['forumName' => 'A & B', 'pageNumber' => 2]));
}
}

0 comments on commit 8b17a4c

Please sign in to comment.