2021-04-26 17:58:22 +02:00
|
|
|
/*
|
2024-09-06 18:56:18 +02:00
|
|
|
Copyright 2021-2024 New Vector Ltd.
|
2021-04-26 17:58:22 +02:00
|
|
|
|
2024-09-06 18:56:18 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-04-26 17:58:22 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
import counterpart from "counterpart";
|
2023-09-05 18:09:47 +02:00
|
|
|
import { TranslationKey as TKey } from "matrix-web-i18n";
|
2022-01-10 13:57:33 +01:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
import type Store from "electron-store";
|
2023-09-05 18:09:47 +02:00
|
|
|
import type EN from "./i18n/strings/en_EN.json";
|
2024-02-19 16:22:40 +01:00
|
|
|
import { loadJsonFile } from "./utils";
|
2021-04-23 17:56:17 +02:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const FALLBACK_LOCALE = "en";
|
2021-04-26 14:58:29 +02:00
|
|
|
|
2023-09-05 18:09:47 +02:00
|
|
|
type TranslationKey = TKey<typeof EN>;
|
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
type SubstitutionValue = number | string;
|
|
|
|
|
2023-09-05 18:09:47 +02:00
|
|
|
interface Variables {
|
2022-10-13 13:42:33 +02:00
|
|
|
[key: string]: SubstitutionValue | undefined;
|
2021-06-25 15:35:58 +02:00
|
|
|
count?: number;
|
|
|
|
}
|
|
|
|
|
2023-09-05 18:09:47 +02:00
|
|
|
export function _t(text: TranslationKey, variables: Variables = {}): string {
|
2022-10-21 17:45:34 +02:00
|
|
|
const { count } = variables;
|
2021-04-26 14:58:29 +02:00
|
|
|
|
|
|
|
// Horrible hack to avoid https://github.com/vector-im/element-web/issues/4191
|
|
|
|
// The interpolation library that counterpart uses does not support undefined/null
|
|
|
|
// values and instead will throw an error. This is a problem since everywhere else
|
|
|
|
// in JS land passing undefined/null will simply stringify instead, and when converting
|
|
|
|
// valid ES6 template strings to i18n strings it's extremely easy to pass undefined/null
|
|
|
|
// if there are no existing null guards. To avoid this making the app completely inoperable,
|
|
|
|
// we'll check all the values for undefined/null and stringify them here.
|
2022-10-21 17:45:34 +02:00
|
|
|
Object.keys(variables).forEach((key) => {
|
|
|
|
if (variables[key] === undefined) {
|
2021-04-26 14:58:29 +02:00
|
|
|
console.warn("safeCounterpartTranslate called with undefined interpolation name: " + key);
|
2022-12-15 12:00:58 +01:00
|
|
|
variables[key] = "undefined";
|
2021-04-26 14:58:29 +02:00
|
|
|
}
|
2022-10-21 17:45:34 +02:00
|
|
|
if (variables[key] === null) {
|
2021-04-26 14:58:29 +02:00
|
|
|
console.warn("safeCounterpartTranslate called with null interpolation name: " + key);
|
2022-12-15 12:00:58 +01:00
|
|
|
variables[key] = "null";
|
2021-04-26 14:58:29 +02:00
|
|
|
}
|
|
|
|
});
|
2022-10-21 17:45:34 +02:00
|
|
|
let translated = counterpart.translate(text, variables);
|
|
|
|
if (!translated && count !== undefined) {
|
|
|
|
// counterpart does not do fallback if no pluralisation exists in the preferred language, so do it here
|
|
|
|
translated = counterpart.translate(text, { ...variables, locale: FALLBACK_LOCALE });
|
2021-04-26 14:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The translation returns text so there's no XSS vector here (no unsafe HTML, no code execution)
|
|
|
|
return translated;
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
type Component = () => void;
|
|
|
|
|
2022-11-30 14:51:54 +01:00
|
|
|
type TypedStore = Store<{ locale?: string | string[] }>;
|
2021-06-25 15:35:58 +02:00
|
|
|
|
|
|
|
export class AppLocalization {
|
|
|
|
private static readonly STORE_KEY = "locale";
|
2021-05-27 15:44:41 +02:00
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
private readonly store: TypedStore;
|
2022-10-13 13:42:33 +02:00
|
|
|
private readonly localizedComponents?: Set<Component>;
|
2021-06-25 15:35:58 +02:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
public constructor({ store, components = [] }: { store: TypedStore; components: Component[] }) {
|
2022-10-21 17:45:34 +02:00
|
|
|
counterpart.registerTranslations(FALLBACK_LOCALE, this.fetchTranslationJson("en_EN"));
|
|
|
|
counterpart.setFallbackLocale(FALLBACK_LOCALE);
|
2022-12-15 12:00:58 +01:00
|
|
|
counterpart.setSeparator("|");
|
2021-04-26 14:58:29 +02:00
|
|
|
|
|
|
|
if (Array.isArray(components)) {
|
|
|
|
this.localizedComponents = new Set(components);
|
|
|
|
}
|
|
|
|
|
2021-04-26 15:13:32 +02:00
|
|
|
this.store = store;
|
2021-06-25 15:35:58 +02:00
|
|
|
if (this.store.has(AppLocalization.STORE_KEY)) {
|
|
|
|
const locales = this.store.get(AppLocalization.STORE_KEY);
|
2022-10-13 13:42:33 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
this.setAppLocale(locales!);
|
2021-04-26 14:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.resetLocalizedUI();
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:16:04 +02:00
|
|
|
// Format language strings from normalized form to non-normalized form (e.g. en-gb to en_GB)
|
|
|
|
private denormalize(locale: string): string {
|
|
|
|
if (locale === "en") {
|
|
|
|
locale = "en_EN";
|
|
|
|
}
|
|
|
|
const parts = locale.split("-");
|
|
|
|
if (parts.length > 1) {
|
|
|
|
parts[1] = parts[1].toUpperCase();
|
|
|
|
}
|
|
|
|
return parts.join("_");
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
public fetchTranslationJson(locale: string): Record<string, string> {
|
2021-04-26 14:58:29 +02:00
|
|
|
try {
|
|
|
|
console.log("Fetching translation json for locale: " + locale);
|
2024-02-20 12:36:49 +01:00
|
|
|
return loadJsonFile(__dirname, "i18n", "strings", `${this.denormalize(locale)}.json`);
|
2021-04-26 14:58:29 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log(`Could not fetch translation json for locale: '${locale}'`, e);
|
2022-10-13 13:42:33 +02:00
|
|
|
return {};
|
2021-04-26 14:58:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
public setAppLocale(locales: string | string[]): void {
|
2021-04-26 14:58:29 +02:00
|
|
|
console.log(`Changing application language to ${locales}`);
|
|
|
|
|
|
|
|
if (!Array.isArray(locales)) {
|
|
|
|
locales = [locales];
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const loadedLocales = locales.filter((locale) => {
|
2021-04-26 14:58:29 +02:00
|
|
|
const translations = this.fetchTranslationJson(locale);
|
|
|
|
if (translations !== null) {
|
|
|
|
counterpart.registerTranslations(locale, translations);
|
|
|
|
}
|
2022-10-21 17:45:34 +02:00
|
|
|
return !!translations;
|
2021-04-26 14:58:29 +02:00
|
|
|
});
|
|
|
|
|
2022-10-21 17:45:34 +02:00
|
|
|
counterpart.setLocale(loadedLocales[0]);
|
2021-06-25 15:35:58 +02:00
|
|
|
this.store.set(AppLocalization.STORE_KEY, locales);
|
2021-04-26 14:58:29 +02:00
|
|
|
|
|
|
|
this.resetLocalizedUI();
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:35:58 +02:00
|
|
|
public resetLocalizedUI(): void {
|
2021-04-26 14:58:29 +02:00
|
|
|
console.log("Resetting the UI components after locale change");
|
2022-12-15 12:00:58 +01:00
|
|
|
this.localizedComponents?.forEach((componentSetup) => {
|
2021-04-26 14:58:29 +02:00
|
|
|
if (typeof componentSetup === "function") {
|
|
|
|
componentSetup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-04-23 17:56:17 +02:00
|
|
|
}
|