Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

fix fallback for pluralized strings #7480

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __mocks__/browser-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const en = require("../src/i18n/strings/en_EN");
const de = require("../src/i18n/strings/de_DE");
const lv = {
"Save": "Saglabāt",
"Uploading %(filename)s and %(count)s others|one": "Качване на %(filename)s и %(count)s друг",
};

// Mock the browser-request for the languageHandler tests to return
Expand Down
3 changes: 1 addition & 2 deletions src/languageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function _td(s: string): string { // eslint-disable-line @typescript-esli
const translateWithFallback = (text: string, options?: object): { translated?: string, isFallback?: boolean } => {
const translated = counterpart.translate(text, options);
if (/^missing translation:/.test(translated)) {
const fallbackTranslated = counterpart.translate(text, { ...options, fallbackLocale: FALLBACK_LOCALE });
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
return { translated: fallbackTranslated, isFallback: true };
}
return { translated };
Expand Down Expand Up @@ -168,7 +168,6 @@ export function _t(text: string, variables: IVariables, tags: Tags): React.React
export function _t(text: string, variables?: IVariables, tags?: Tags): TranslatedString {
// The translation returns text so there's no XSS vector here (no unsafe HTML, no code execution)
const { translated } = safeCounterpartTranslate(text, variables);

const substituted = substitute(translated, variables, tags);

return annotateStrings(substituted, text);
Expand Down
22 changes: 20 additions & 2 deletions test/i18n-test/languageHandler-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('languageHandler', function() {
type TestCase = [string, string, Record<string, unknown>, Record<string, unknown>, TranslatedString];
const testCasesEn: TestCase[] = [
['translates a basic string', basicString, {}, undefined, 'Rooms'],
[
'handles plurals when count is 0',
plurals,
{ count: 0 },
undefined,
'and 0 others...',
],
[
'handles plurals when count is 1',
plurals,
Expand Down Expand Up @@ -123,8 +130,19 @@ describe('languageHandler', function() {
setMissingEntryGenerator(counterpartDefaultMissingEntryGen);
});

const lvExistingPlural = 'Uploading %(filename)s and %(count)s others';

// lv does not have a pluralizer function
const noPluralizerCase = [
'handles plural strings when no pluralizer exists for language',
lvExistingPlural,
{ count: 1, filename: 'test.txt' },
undefined,
'Uploading test.txt and 1 other',
] as TestCase;

describe('_t', () => {
it.each(testCasesEn)(
it.each([...testCasesEn, noPluralizerCase])(
"%s and translates with fallback locale",
async (_d, translationString, variables, tags, result) => {
expect(_t(translationString, variables, tags)).toEqual(result);
Expand All @@ -133,7 +151,7 @@ describe('languageHandler', function() {
});

describe('_tDom()', () => {
it.each(testCasesEn)(
it.each([...testCasesEn, noPluralizerCase])(
"%s and translates with fallback locale, attributes fallback locale",
async (_d, translationString, variables, tags, result) => {
expect(_tDom(translationString, variables, tags)).toEqual(<span lang="en">{ result }</span>);
Expand Down