2022-05-06 17:09:28 +08:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-01-05 18:37:28 +08:00
|
|
|
import React from "react";
|
2023-08-22 22:07:16 +08:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2022-01-05 18:37:28 +08:00
|
|
|
|
2023-08-22 22:07:16 +08:00
|
|
|
import SdkConfig from "../src/SdkConfig";
|
2022-01-05 18:37:28 +08:00
|
|
|
import {
|
|
|
|
_t,
|
|
|
|
_tDom,
|
2023-08-22 22:07:16 +08:00
|
|
|
CustomTranslationOptions,
|
|
|
|
getAllLanguagesWithLabels,
|
|
|
|
ICustomTranslations,
|
|
|
|
registerCustomTranslations,
|
2022-01-05 18:37:28 +08:00
|
|
|
setLanguage,
|
|
|
|
setMissingEntryGenerator,
|
|
|
|
substitute,
|
2023-08-22 22:07:16 +08:00
|
|
|
TranslatedString,
|
|
|
|
UserFriendlyError,
|
2023-08-22 23:32:05 +08:00
|
|
|
TranslationKey,
|
2023-08-22 22:07:16 +08:00
|
|
|
} from "../src/languageHandler";
|
|
|
|
import { stubClient } from "./test-utils";
|
|
|
|
import { setupLanguageMock } from "./setup/setupLanguage";
|
|
|
|
|
|
|
|
async function setupTranslationOverridesForTests(overrides: ICustomTranslations) {
|
|
|
|
const lookupUrl = "/translations.json";
|
|
|
|
const fn = (url: string): ICustomTranslations => {
|
|
|
|
expect(url).toEqual(lookupUrl);
|
|
|
|
return overrides;
|
|
|
|
};
|
|
|
|
|
|
|
|
SdkConfig.add({
|
|
|
|
custom_translations_url: lookupUrl,
|
|
|
|
});
|
|
|
|
CustomTranslationOptions.lookupFn = fn;
|
|
|
|
await registerCustomTranslations({
|
|
|
|
testOnlyIgnoreCustomTranslationsCache: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("languageHandler", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await setLanguage("en");
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
SdkConfig.reset();
|
|
|
|
CustomTranslationOptions.lookupFn = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support overriding translations", async () => {
|
2023-08-22 23:32:05 +08:00
|
|
|
const str = "This is a test string that does not exist in the app." as TranslationKey;
|
|
|
|
const enOverride = "This is the English version of a custom string." as TranslationKey;
|
|
|
|
const deOverride = "This is the German version of a custom string." as TranslationKey;
|
2023-08-22 22:07:16 +08:00
|
|
|
|
|
|
|
// First test that overrides aren't being used
|
|
|
|
await setLanguage("en");
|
|
|
|
expect(_t(str)).toEqual(str);
|
|
|
|
await setLanguage("de");
|
|
|
|
expect(_t(str)).toEqual(str);
|
|
|
|
|
|
|
|
await setupTranslationOverridesForTests({
|
|
|
|
[str]: {
|
|
|
|
en: enOverride,
|
|
|
|
de: deOverride,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now test that they *are* being used
|
|
|
|
await setLanguage("en");
|
|
|
|
expect(_t(str)).toEqual(enOverride);
|
|
|
|
|
|
|
|
await setLanguage("de");
|
|
|
|
expect(_t(str)).toEqual(deOverride);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("UserFriendlyError", () => {
|
2023-08-22 23:32:05 +08:00
|
|
|
const testErrorMessage = "This email address is already in use (%(email)s)" as TranslationKey;
|
2023-08-22 22:07:16 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
// Setup some strings with variable substituations that we can use in the tests.
|
|
|
|
const deOverride = "Diese E-Mail-Adresse wird bereits verwendet (%(email)s)";
|
|
|
|
await setupTranslationOverridesForTests({
|
|
|
|
[testErrorMessage]: {
|
|
|
|
en: testErrorMessage,
|
|
|
|
de: deOverride,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("includes English message and localized translated message", async () => {
|
|
|
|
await setLanguage("de");
|
|
|
|
|
|
|
|
const friendlyError = new UserFriendlyError(testErrorMessage, {
|
|
|
|
email: "test@example.com",
|
|
|
|
cause: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ensure message is in English so it's readable in the logs
|
|
|
|
expect(friendlyError.message).toStrictEqual("This email address is already in use (test@example.com)");
|
|
|
|
// Ensure the translated message is localized appropriately
|
|
|
|
expect(friendlyError.translatedMessage).toStrictEqual(
|
|
|
|
"Diese E-Mail-Adresse wird bereits verwendet (test@example.com)",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("includes underlying cause error", async () => {
|
|
|
|
await setLanguage("de");
|
|
|
|
|
|
|
|
const underlyingError = new Error("Fake underlying error");
|
|
|
|
const friendlyError = new UserFriendlyError(testErrorMessage, {
|
|
|
|
email: "test@example.com",
|
|
|
|
cause: underlyingError,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(friendlyError.cause).toStrictEqual(underlyingError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("ok to omit the substitution variables and cause object, there just won't be any cause", async () => {
|
2023-08-22 23:32:05 +08:00
|
|
|
const friendlyError = new UserFriendlyError("foo error" as TranslationKey);
|
2023-08-22 22:07:16 +08:00
|
|
|
expect(friendlyError.cause).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getAllLanguagesWithLabels", () => {
|
|
|
|
it("should handle unknown language sanely", async () => {
|
|
|
|
fetchMock.getOnce(
|
|
|
|
"/i18n/languages.json",
|
|
|
|
{
|
|
|
|
en: "en_EN.json",
|
|
|
|
de: "de_DE.json",
|
|
|
|
qq: "qq.json",
|
|
|
|
},
|
|
|
|
{ overwriteRoutes: true },
|
|
|
|
);
|
|
|
|
await expect(getAllLanguagesWithLabels()).resolves.toMatchInlineSnapshot(`
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"label": "English",
|
|
|
|
"labelInTargetLanguage": "English",
|
|
|
|
"value": "en",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": "German",
|
|
|
|
"labelInTargetLanguage": "Deutsch",
|
|
|
|
"value": "de",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": "qq",
|
|
|
|
"labelInTargetLanguage": "qq",
|
|
|
|
"value": "qq",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
setupLanguageMock(); // restore language mock
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-01-05 18:37:28 +08:00
|
|
|
|
2023-08-22 22:07:16 +08:00
|
|
|
describe("languageHandler JSX", function () {
|
2022-10-13 01:59:07 +08:00
|
|
|
// See setupLanguage.ts for how we are stubbing out translations to provide fixture data for these tests
|
2022-01-05 18:37:28 +08:00
|
|
|
const basicString = "Rooms";
|
2023-08-22 23:32:05 +08:00
|
|
|
const selfClosingTagSub = "Accept <policyLink /> to continue:" as TranslationKey;
|
|
|
|
const textInTagSub = "<a>Upgrade</a> to your own domain" as TranslationKey;
|
2022-01-05 18:37:28 +08:00
|
|
|
const plurals = "and %(count)s others...";
|
|
|
|
const variableSub = "You are now ignoring %(userId)s";
|
|
|
|
|
2023-08-22 23:32:05 +08:00
|
|
|
type TestCase = [
|
|
|
|
string,
|
|
|
|
TranslationKey,
|
|
|
|
Record<string, unknown>,
|
|
|
|
Record<string, unknown> | undefined,
|
|
|
|
TranslatedString,
|
|
|
|
];
|
2022-01-05 18:37:28 +08:00
|
|
|
const testCasesEn: TestCase[] = [
|
2022-05-16 20:28:24 +08:00
|
|
|
// description of the test case, translationString, variables, tags, expected result
|
2022-01-05 18:37:28 +08:00
|
|
|
["translates a basic string", basicString, {}, undefined, "Rooms"],
|
2022-01-07 23:20:24 +08:00
|
|
|
["handles plurals when count is 0", plurals, { count: 0 }, undefined, "and 0 others..."],
|
2022-01-05 18:37:28 +08:00
|
|
|
["handles plurals when count is 1", plurals, { count: 1 }, undefined, "and one other..."],
|
|
|
|
["handles plurals when count is not 1", plurals, { count: 2 }, undefined, "and 2 others..."],
|
|
|
|
["handles simple variable substitution", variableSub, { userId: "foo" }, undefined, "You are now ignoring foo"],
|
2022-01-07 23:20:24 +08:00
|
|
|
[
|
2022-01-05 18:37:28 +08:00
|
|
|
"handles simple tag substitution",
|
|
|
|
selfClosingTagSub,
|
|
|
|
{},
|
|
|
|
{ policyLink: () => "foo" },
|
|
|
|
"Accept foo to continue:",
|
|
|
|
],
|
2023-02-13 19:39:16 +08:00
|
|
|
["handles text in tags", textInTagSub, {}, { a: (sub: string) => `x${sub}x` }, "xUpgradex to your own domain"],
|
2022-01-05 18:37:28 +08:00
|
|
|
[
|
|
|
|
"handles variable substitution with React function component",
|
|
|
|
variableSub,
|
|
|
|
{ userId: () => <i>foo</i> },
|
|
|
|
undefined,
|
|
|
|
// eslint-disable-next-line react/jsx-key
|
|
|
|
<span>
|
|
|
|
You are now ignoring <i>foo</i>
|
|
|
|
</span>,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"handles variable substitution with react node",
|
|
|
|
variableSub,
|
|
|
|
{ userId: <i>foo</i> },
|
|
|
|
undefined,
|
|
|
|
// eslint-disable-next-line react/jsx-key
|
|
|
|
<span>
|
|
|
|
You are now ignoring <i>foo</i>
|
|
|
|
</span>,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"handles tag substitution with React function component",
|
|
|
|
selfClosingTagSub,
|
|
|
|
{},
|
|
|
|
{ policyLink: () => <i>foo</i> },
|
|
|
|
// eslint-disable-next-line react/jsx-key
|
|
|
|
<span>
|
|
|
|
Accept <i>foo</i> to continue:
|
|
|
|
</span>,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2023-02-15 21:36:22 +08:00
|
|
|
let oldNodeEnv: string | undefined;
|
2022-10-13 21:32:45 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
oldNodeEnv = process.env.NODE_ENV;
|
|
|
|
process.env.NODE_ENV = "test";
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
process.env.NODE_ENV = oldNodeEnv;
|
|
|
|
});
|
|
|
|
|
2022-01-05 18:37:28 +08:00
|
|
|
describe("when translations exist in language", () => {
|
2023-03-01 23:23:35 +08:00
|
|
|
beforeEach(function () {
|
2022-01-05 18:37:28 +08:00
|
|
|
stubClient();
|
|
|
|
|
2023-03-01 23:23:35 +08:00
|
|
|
setLanguage("en");
|
2022-01-05 18:37:28 +08:00
|
|
|
setMissingEntryGenerator((key) => key.split("|", 2)[1]);
|
|
|
|
});
|
|
|
|
|
2023-03-01 23:23:35 +08:00
|
|
|
it("translates a string to german", async () => {
|
|
|
|
await setLanguage("de");
|
|
|
|
const translated = _t(basicString);
|
|
|
|
expect(translated).toBe("Räume");
|
2022-01-05 18:37:28 +08:00
|
|
|
});
|
|
|
|
|
2022-10-13 21:32:45 +08:00
|
|
|
it.each(testCasesEn)("%s", (_d, translationString, variables, tags, result) => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_t(translationString, variables, tags!)).toEqual(result);
|
2022-01-05 18:37:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("replacements in the wrong order", function () {
|
2023-08-22 23:32:05 +08:00
|
|
|
const text = "%(var1)s %(var2)s" as TranslationKey;
|
2022-01-05 18:37:28 +08:00
|
|
|
expect(_t(text, { var2: "val2", var1: "val1" })).toBe("val1 val2");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("multiple replacements of the same variable", function () {
|
|
|
|
const text = "%(var1)s %(var1)s";
|
|
|
|
expect(substitute(text, { var1: "val1" })).toBe("val1 val1");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("multiple replacements of the same tag", function () {
|
|
|
|
const text = "<a>Click here</a> to join the discussion! <a>or here</a>";
|
|
|
|
expect(substitute(text, {}, { a: (sub) => `x${sub}x` })).toBe(
|
|
|
|
"xClick herex to join the discussion! xor herex",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-10 21:54:57 +08:00
|
|
|
describe("for a non-en language", () => {
|
2022-10-13 21:32:45 +08:00
|
|
|
beforeEach(() => {
|
2022-01-05 18:37:28 +08:00
|
|
|
stubClient();
|
2022-10-13 21:32:45 +08:00
|
|
|
setLanguage("lv");
|
2022-01-05 18:37:28 +08:00
|
|
|
// counterpart doesnt expose any way to restore default config
|
|
|
|
// missingEntryGenerator is mocked in the root setup file
|
|
|
|
// reset to default here
|
2023-02-13 19:39:16 +08:00
|
|
|
const counterpartDefaultMissingEntryGen = function (key: string) {
|
2022-01-05 18:37:28 +08:00
|
|
|
return "missing translation: " + key;
|
|
|
|
};
|
|
|
|
setMissingEntryGenerator(counterpartDefaultMissingEntryGen);
|
|
|
|
});
|
|
|
|
|
2022-01-10 21:54:57 +08:00
|
|
|
// mocked lv has only `"Uploading %(filename)s and %(count)s others|one"`
|
2022-01-07 23:20:24 +08:00
|
|
|
const lvExistingPlural = "Uploading %(filename)s and %(count)s others";
|
2022-01-10 21:54:57 +08:00
|
|
|
const lvNonExistingPlural = "%(spaceName)s and %(count)s others";
|
2022-01-07 23:20:24 +08:00
|
|
|
|
2022-01-10 21:54:57 +08:00
|
|
|
describe("pluralization", () => {
|
|
|
|
const pluralCases = [
|
|
|
|
[
|
|
|
|
"falls back when plural string exists but not for for count",
|
|
|
|
lvExistingPlural,
|
|
|
|
{ count: 2, filename: "test.txt" },
|
|
|
|
undefined,
|
|
|
|
"Uploading test.txt and 2 others",
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"falls back when plural string does not exists at all",
|
|
|
|
lvNonExistingPlural,
|
|
|
|
{ count: 2, spaceName: "test" },
|
|
|
|
undefined,
|
|
|
|
"test and 2 others",
|
|
|
|
],
|
|
|
|
] as TestCase[];
|
|
|
|
|
|
|
|
describe("_t", () => {
|
|
|
|
it("translated correctly when plural string exists for count", () => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_t(lvExistingPlural, { count: 1, filename: "test.txt" })).toEqual(
|
2022-01-10 21:54:57 +08:00
|
|
|
"Качване на test.txt и 1 друг",
|
2022-12-12 19:24:14 +08:00
|
|
|
);
|
|
|
|
});
|
2022-10-13 21:32:45 +08:00
|
|
|
it.each(pluralCases)("%s", (_d, translationString, variables, tags, result) => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_t(translationString, variables, tags!)).toEqual(result);
|
2022-01-10 21:54:57 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("_tDom()", () => {
|
|
|
|
it("translated correctly when plural string exists for count", () => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_tDom(lvExistingPlural, { count: 1, filename: "test.txt" })).toEqual(
|
2022-01-10 21:54:57 +08:00
|
|
|
"Качване на test.txt и 1 друг",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it.each(pluralCases)(
|
|
|
|
"%s and translates with fallback locale, attributes fallback locale",
|
2022-10-13 21:32:45 +08:00
|
|
|
(_d, translationString, variables, tags, result) => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_tDom(translationString, variables, tags!)).toEqual(<span lang="en">{result}</span>);
|
2022-01-10 21:54:57 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2022-01-05 18:37:28 +08:00
|
|
|
});
|
|
|
|
|
2022-01-10 21:54:57 +08:00
|
|
|
describe("when a translation string does not exist in active language", () => {
|
|
|
|
describe("_t", () => {
|
|
|
|
it.each(testCasesEn)(
|
|
|
|
"%s and translates with fallback locale",
|
2022-10-13 21:32:45 +08:00
|
|
|
(_d, translationString, variables, tags, result) => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_t(translationString, variables, tags!)).toEqual(result);
|
2022-01-10 21:54:57 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("_tDom()", () => {
|
|
|
|
it.each(testCasesEn)(
|
|
|
|
"%s and translates with fallback locale, attributes fallback locale",
|
2022-10-13 21:32:45 +08:00
|
|
|
(_d, translationString, variables, tags, result) => {
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_tDom(translationString, variables, tags!)).toEqual(<span lang="en">{result}</span>);
|
2022-01-10 21:54:57 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2022-01-05 18:37:28 +08:00
|
|
|
});
|
|
|
|
});
|
2022-05-16 20:28:24 +08:00
|
|
|
|
|
|
|
describe("when languages dont load", () => {
|
2022-10-13 21:32:45 +08:00
|
|
|
it("_t", () => {
|
2023-08-22 23:32:05 +08:00
|
|
|
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary" as TranslationKey;
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_t(STRING_NOT_IN_THE_DICTIONARY, {})).toEqual(STRING_NOT_IN_THE_DICTIONARY);
|
2022-05-16 20:28:24 +08:00
|
|
|
});
|
|
|
|
|
2022-10-13 21:32:45 +08:00
|
|
|
it("_tDom", () => {
|
2023-08-22 23:32:05 +08:00
|
|
|
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary" as TranslationKey;
|
2023-02-15 21:36:22 +08:00
|
|
|
expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {})).toEqual(
|
2022-05-16 20:28:24 +08:00
|
|
|
<span lang="en">{STRING_NOT_IN_THE_DICTIONARY}</span>,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-01-05 18:37:28 +08:00
|
|
|
});
|