Fix instances of double translation and guard translation calls using typescript (#11443)

Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
Michael Telatynski 2023-08-22 16:32:05 +01:00 committed by GitHub
parent d13b6e1b41
commit ac70f7ac9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
157 changed files with 554 additions and 780 deletions

View file

@ -30,6 +30,7 @@ import {
substitute,
TranslatedString,
UserFriendlyError,
TranslationKey,
} from "../src/languageHandler";
import { stubClient } from "./test-utils";
import { setupLanguageMock } from "./setup/setupLanguage";
@ -61,9 +62,9 @@ describe("languageHandler", () => {
});
it("should support overriding translations", async () => {
const str = "This is a test string that does not exist in the app.";
const enOverride = "This is the English version of a custom string.";
const deOverride = "This is the German version of a custom string.";
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;
// First test that overrides aren't being used
await setLanguage("en");
@ -87,7 +88,7 @@ describe("languageHandler", () => {
});
describe("UserFriendlyError", () => {
const testErrorMessage = "This email address is already in use (%(email)s)";
const testErrorMessage = "This email address is already in use (%(email)s)" as TranslationKey;
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)";
@ -128,7 +129,7 @@ describe("languageHandler", () => {
});
it("ok to omit the substitution variables and cause object, there just won't be any cause", async () => {
const friendlyError = new UserFriendlyError("foo error");
const friendlyError = new UserFriendlyError("foo error" as TranslationKey);
expect(friendlyError.cause).toBeUndefined();
});
});
@ -171,12 +172,18 @@ describe("languageHandler", () => {
describe("languageHandler JSX", function () {
// See setupLanguage.ts for how we are stubbing out translations to provide fixture data for these tests
const basicString = "Rooms";
const selfClosingTagSub = "Accept <policyLink /> to continue:";
const textInTagSub = "<a>Upgrade</a> to your own domain";
const selfClosingTagSub = "Accept <policyLink /> to continue:" as TranslationKey;
const textInTagSub = "<a>Upgrade</a> to your own domain" as TranslationKey;
const plurals = "and %(count)s others...";
const variableSub = "You are now ignoring %(userId)s";
type TestCase = [string, string, Record<string, unknown>, Record<string, unknown> | undefined, TranslatedString];
type TestCase = [
string,
TranslationKey,
Record<string, unknown>,
Record<string, unknown> | undefined,
TranslatedString,
];
const testCasesEn: TestCase[] = [
// description of the test case, translationString, variables, tags, expected result
["translates a basic string", basicString, {}, undefined, "Rooms"],
@ -253,7 +260,7 @@ describe("languageHandler JSX", function () {
});
it("replacements in the wrong order", function () {
const text = "%(var1)s %(var2)s";
const text = "%(var1)s %(var2)s" as TranslationKey;
expect(_t(text, { var2: "val2", var1: "val1" })).toBe("val1 val2");
});
@ -354,12 +361,12 @@ describe("languageHandler JSX", function () {
describe("when languages dont load", () => {
it("_t", () => {
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary" as TranslationKey;
expect(_t(STRING_NOT_IN_THE_DICTIONARY, {})).toEqual(STRING_NOT_IN_THE_DICTIONARY);
});
it("_tDom", () => {
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary" as TranslationKey;
expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {})).toEqual(
<span lang="en">{STRING_NOT_IN_THE_DICTIONARY}</span>,
);