2022-03-02 02:53:09 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import SdkConfig from "../src/SdkConfig";
|
|
|
|
import {
|
|
|
|
_t,
|
|
|
|
CustomTranslationOptions,
|
|
|
|
ICustomTranslations,
|
|
|
|
registerCustomTranslations,
|
|
|
|
setLanguage,
|
2023-03-31 15:30:43 +08:00
|
|
|
UserFriendlyError,
|
2022-03-02 02:53:09 +08:00
|
|
|
} from "../src/languageHandler";
|
|
|
|
|
2023-03-31 15:30:43 +08:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("languageHandler", () => {
|
2022-03-02 02:53:09 +08:00
|
|
|
afterEach(() => {
|
2023-04-26 17:36:00 +08:00
|
|
|
SdkConfig.reset();
|
2022-03-02 02:53:09 +08:00
|
|
|
CustomTranslationOptions.lookupFn = undefined;
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("should support overriding translations", async () => {
|
2022-03-02 02:53:09 +08:00
|
|
|
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.";
|
|
|
|
|
|
|
|
// First test that overrides aren't being used
|
|
|
|
await setLanguage("en");
|
|
|
|
expect(_t(str)).toEqual(str);
|
|
|
|
await setLanguage("de");
|
|
|
|
expect(_t(str)).toEqual(str);
|
|
|
|
|
2023-03-31 15:30:43 +08:00
|
|
|
await setupTranslationOverridesForTests({
|
|
|
|
[str]: {
|
|
|
|
en: enOverride,
|
|
|
|
de: deOverride,
|
|
|
|
},
|
2022-03-02 02:53:09 +08:00
|
|
|
});
|
|
|
|
|
2023-03-31 15:30:43 +08:00
|
|
|
// Now test that they *are* being used
|
2022-03-02 02:53:09 +08:00
|
|
|
await setLanguage("en");
|
|
|
|
expect(_t(str)).toEqual(enOverride);
|
|
|
|
|
|
|
|
await setLanguage("de");
|
|
|
|
expect(_t(str)).toEqual(deOverride);
|
|
|
|
});
|
2023-03-31 15:30:43 +08:00
|
|
|
|
|
|
|
describe("UserFriendlyError", () => {
|
|
|
|
const testErrorMessage = "This email address is already in use (%(email)s)";
|
|
|
|
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 () => {
|
|
|
|
const friendlyError = new UserFriendlyError("foo error");
|
|
|
|
expect(friendlyError.cause).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
2022-03-02 02:53:09 +08:00
|
|
|
});
|