Update custom translations to support nested fields in structured JSON (#11685)
* Update matrix-web-i18n Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Fix custom translations for structured JSON nested fields Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Fix import Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Fix export Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Update @matrix-org/react-sdk-module-api Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Delint Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Update matrix-web-i18n Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Update matrix-web-i18n Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
1897962086
commit
632d8f4bc7
5 changed files with 74 additions and 108 deletions
|
@ -16,6 +16,8 @@ limitations under the License.
|
|||
|
||||
import React from "react";
|
||||
import fetchMock from "fetch-mock-jest";
|
||||
import { Translation } from "matrix-web-i18n";
|
||||
import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api";
|
||||
|
||||
import SdkConfig from "../src/SdkConfig";
|
||||
import {
|
||||
|
@ -23,7 +25,6 @@ import {
|
|||
_tDom,
|
||||
CustomTranslationOptions,
|
||||
getAllLanguagesWithLabels,
|
||||
ICustomTranslations,
|
||||
registerCustomTranslations,
|
||||
setLanguage,
|
||||
setMissingEntryGenerator,
|
||||
|
@ -35,9 +36,9 @@ import {
|
|||
import { stubClient } from "./test-utils";
|
||||
import { setupLanguageMock } from "./setup/setupLanguage";
|
||||
|
||||
async function setupTranslationOverridesForTests(overrides: ICustomTranslations) {
|
||||
async function setupTranslationOverridesForTests(overrides: TranslationStringsObject) {
|
||||
const lookupUrl = "/translations.json";
|
||||
const fn = (url: string): ICustomTranslations => {
|
||||
const fn = (url: string): TranslationStringsObject => {
|
||||
expect(url).toEqual(lookupUrl);
|
||||
return overrides;
|
||||
};
|
||||
|
@ -62,15 +63,15 @@ describe("languageHandler", () => {
|
|||
});
|
||||
|
||||
it("should support overriding translations", async () => {
|
||||
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;
|
||||
const str: TranslationKey = "power_level|default";
|
||||
const enOverride: Translation = "Visitor";
|
||||
const deOverride: Translation = "Besucher";
|
||||
|
||||
// First test that overrides aren't being used
|
||||
await setLanguage("en");
|
||||
expect(_t(str)).toEqual(str);
|
||||
expect(_t(str)).toMatchInlineSnapshot(`"Default"`);
|
||||
await setLanguage("de");
|
||||
expect(_t(str)).toEqual(str);
|
||||
expect(_t(str)).toMatchInlineSnapshot(`"Standard"`);
|
||||
|
||||
await setupTranslationOverridesForTests({
|
||||
[str]: {
|
||||
|
@ -87,6 +88,42 @@ describe("languageHandler", () => {
|
|||
expect(_t(str)).toEqual(deOverride);
|
||||
});
|
||||
|
||||
it("should support overriding plural translations", async () => {
|
||||
const str: TranslationKey = "voip|n_people_joined";
|
||||
const enOverride: Translation = {
|
||||
other: "%(count)s people in the call",
|
||||
one: "%(count)s person in the call",
|
||||
};
|
||||
const deOverride: Translation = {
|
||||
other: "%(count)s Personen im Anruf",
|
||||
one: "%(count)s Person im Anruf",
|
||||
};
|
||||
|
||||
// First test that overrides aren't being used
|
||||
await setLanguage("en");
|
||||
expect(_t(str, { count: 1 })).toMatchInlineSnapshot(`"1 person joined"`);
|
||||
expect(_t(str, { count: 5 })).toMatchInlineSnapshot(`"5 people joined"`);
|
||||
await setLanguage("de");
|
||||
expect(_t(str, { count: 1 })).toMatchInlineSnapshot(`"1 Person beigetreten"`);
|
||||
expect(_t(str, { count: 5 })).toMatchInlineSnapshot(`"5 Personen beigetreten"`);
|
||||
|
||||
await setupTranslationOverridesForTests({
|
||||
[str]: {
|
||||
en: enOverride,
|
||||
de: deOverride,
|
||||
},
|
||||
});
|
||||
|
||||
// Now test that they *are* being used
|
||||
await setLanguage("en");
|
||||
expect(_t(str, { count: 1 })).toMatchInlineSnapshot(`"1 person in the call"`);
|
||||
expect(_t(str, { count: 5 })).toMatchInlineSnapshot(`"5 people in the call"`);
|
||||
|
||||
await setLanguage("de");
|
||||
expect(_t(str, { count: 1 })).toMatchInlineSnapshot(`"1 Person im Anruf"`);
|
||||
expect(_t(str, { count: 5 })).toMatchInlineSnapshot(`"5 Personen im Anruf"`);
|
||||
});
|
||||
|
||||
describe("UserFriendlyError", () => {
|
||||
const testErrorMessage = "This email address is already in use (%(email)s)" as TranslationKey;
|
||||
beforeEach(async () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue