2017-05-23 14:16:31 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2019-2022 The Matrix.org Foundation C.I.C.
|
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2017-05-23 14:16:31 +00:00
|
|
|
Copyright 2017 MTRNord and Cooperative EITA
|
2017-05-25 10:24:17 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd.
|
2017-05-23 14:16:31 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2017-05-23 14:16:31 +00:00
|
|
|
*/
|
|
|
|
|
2017-05-25 10:24:17 +00:00
|
|
|
import counterpart from "counterpart";
|
2017-06-12 15:45:36 +00:00
|
|
|
import React from "react";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-01 18:53:09 +00:00
|
|
|
import { Optional } from "matrix-events-sdk";
|
2023-10-02 10:44:25 +00:00
|
|
|
import { MapWithDefault } from "matrix-js-sdk/src/utils";
|
|
|
|
import { normalizeLanguageKey, TranslationKey as _TranslationKey, KEY_SEPARATOR } from "matrix-web-i18n";
|
|
|
|
import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api";
|
|
|
|
import _ from "lodash";
|
2020-07-02 22:15:08 +00:00
|
|
|
|
2023-08-22 15:32:05 +00:00
|
|
|
import type Translations from "./i18n/strings/en_EN.json";
|
2020-07-28 17:53:43 +00:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2020-02-24 17:11:08 +00:00
|
|
|
import PlatformPeg from "./PlatformPeg";
|
2020-07-28 17:53:43 +00:00
|
|
|
import { SettingLevel } from "./settings/SettingLevel";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { retry } from "./utils/promise";
|
2022-03-01 18:53:09 +00:00
|
|
|
import SdkConfig from "./SdkConfig";
|
2022-07-05 18:26:44 +00:00
|
|
|
import { ModuleRunner } from "./modules/ModuleRunner";
|
2020-01-09 20:56:14 +00:00
|
|
|
|
2021-10-22 23:11:56 +00:00
|
|
|
// @ts-ignore - $webapp is a webpack resolve alias pointing to the output directory, see webpack config
|
2021-10-22 22:23:32 +00:00
|
|
|
import webpackLangJsonUrl from "$webapp/i18n/languages.json";
|
2021-09-21 15:48:09 +00:00
|
|
|
|
2023-10-02 10:44:25 +00:00
|
|
|
export { normalizeLanguageKey, getNormalizedLanguageKeys } from "matrix-web-i18n";
|
|
|
|
|
2017-05-23 14:16:31 +00:00
|
|
|
const i18nFolder = "i18n/";
|
|
|
|
|
2017-12-09 13:22:20 +00:00
|
|
|
// Control whether to also return original, untranslated strings
|
|
|
|
// Useful for debugging and testing
|
|
|
|
const ANNOTATE_STRINGS = false;
|
|
|
|
|
2017-05-25 10:24:17 +00:00
|
|
|
// We use english strings as keys, some of which contain full stops
|
2023-10-02 10:44:25 +00:00
|
|
|
counterpart.setSeparator(KEY_SEPARATOR);
|
2022-01-05 10:37:28 +00:00
|
|
|
|
|
|
|
// see `translateWithFallback` for an explanation of fallback handling
|
|
|
|
const FALLBACK_LOCALE = "en";
|
2022-01-10 13:54:57 +00:00
|
|
|
counterpart.setFallbackLocale(FALLBACK_LOCALE);
|
2017-05-25 10:24:17 +00:00
|
|
|
|
2023-08-22 15:32:05 +00:00
|
|
|
export interface ErrorOptions {
|
2024-08-05 07:59:27 +00:00
|
|
|
// Because we're mixing the substitution variables and `cause` into the same object
|
2023-03-31 07:30:43 +00:00
|
|
|
// below, we want them to always explicitly say whether there is an underlying error
|
|
|
|
// or not to avoid typos of "cause" slipping through unnoticed.
|
|
|
|
cause: unknown | undefined;
|
2020-07-02 22:15:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 05:09:31 +00:00
|
|
|
/**
|
2023-03-31 07:30:43 +00:00
|
|
|
* Used to rethrow an error with a user-friendly translatable message while maintaining
|
|
|
|
* access to that original underlying error. Downstream consumers can display the
|
|
|
|
* `translatedMessage` property in the UI and inspect the underlying error with the
|
|
|
|
* `cause` property.
|
|
|
|
*
|
|
|
|
* The error message will display as English in the console and logs so Element
|
|
|
|
* developers can easily understand the error and find the source in the code. It also
|
|
|
|
* helps tools like Sentry deduplicate the error, or just generally searching in
|
|
|
|
* rageshakes to find all instances regardless of the users locale.
|
|
|
|
*
|
|
|
|
* @param message - The untranslated error message text, e.g "Something went wrong with %(foo)s".
|
|
|
|
* @param substitutionVariablesAndCause - Variable substitutions for the translation and
|
|
|
|
* original cause of the error. If there is no cause, just pass `undefined`, e.g { foo:
|
|
|
|
* 'bar', cause: err || undefined }
|
2019-05-03 05:09:31 +00:00
|
|
|
*/
|
2023-03-31 07:30:43 +00:00
|
|
|
export class UserFriendlyError extends Error {
|
|
|
|
public readonly translatedMessage: string;
|
|
|
|
|
2024-08-05 07:59:27 +00:00
|
|
|
public constructor(
|
|
|
|
message: TranslationKey,
|
|
|
|
substitutionVariablesAndCause?: Omit<IVariables, keyof ErrorOptions> | ErrorOptions,
|
|
|
|
) {
|
2023-09-22 15:39:40 +00:00
|
|
|
// Prevent "Could not find /%\(cause\)s/g in x" logs to the console by removing it from the list
|
2024-08-05 07:59:27 +00:00
|
|
|
const { cause, ...substitutionVariables } = substitutionVariablesAndCause ?? {};
|
|
|
|
const errorOptions = { cause };
|
2023-03-31 07:30:43 +00:00
|
|
|
|
2024-08-05 07:59:27 +00:00
|
|
|
// Create the error with the English version of the message that we want to show up in the logs
|
2023-03-31 07:30:43 +00:00
|
|
|
const englishTranslatedMessage = _t(message, { ...substitutionVariables, locale: "en" });
|
|
|
|
super(englishTranslatedMessage, errorOptions);
|
|
|
|
|
|
|
|
// Also provide a translated version of the error in the users locale to display
|
|
|
|
this.translatedMessage = _t(message, substitutionVariables);
|
|
|
|
}
|
2019-05-03 05:09:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:08:32 +00:00
|
|
|
export function getUserLanguage(): string {
|
|
|
|
const language = SettingsStore.getValue("language", null, /*excludeDefault:*/ true);
|
2023-08-21 19:38:59 +00:00
|
|
|
if (typeof language === "string" && language !== "") {
|
2021-05-12 20:08:32 +00:00
|
|
|
return language;
|
|
|
|
} else {
|
|
|
|
return normalizeLanguageKey(getLanguageFromBrowser());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:32:05 +00:00
|
|
|
/**
|
|
|
|
* A type representing the union of possible keys into the translation file using `|` delimiter to access nested fields.
|
|
|
|
* @example `common|error` to access `error` within the `common` sub-object.
|
|
|
|
* {
|
|
|
|
* "common": {
|
|
|
|
* "error": "Error"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2023-10-02 10:44:25 +00:00
|
|
|
export type TranslationKey = _TranslationKey<typeof Translations>;
|
2023-08-22 15:32:05 +00:00
|
|
|
|
2017-09-22 19:43:27 +00:00
|
|
|
// Function which only purpose is to mark that a string is translatable
|
|
|
|
// Does not actually do anything. It's helpful for automatic extraction of translatable strings
|
2023-08-22 15:32:05 +00:00
|
|
|
export function _td(s: TranslationKey): TranslationKey {
|
2017-09-22 19:43:27 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-01-05 10:37:28 +00:00
|
|
|
/**
|
|
|
|
* to improve screen reader experience translations that are not in the main page language
|
|
|
|
* eg a translation that fell back to english from another language
|
|
|
|
* should be wrapped with an appropriate `lang='en'` attribute
|
|
|
|
* counterpart's `translate` doesn't expose a way to determine if the resulting translation
|
|
|
|
* is in the target locale or a fallback locale
|
2022-01-10 13:54:57 +00:00
|
|
|
* for this reason, force fallbackLocale === locale in the first call to translate
|
2022-01-05 10:37:28 +00:00
|
|
|
* and fallback 'manually' so we can mark fallback strings appropriately
|
|
|
|
* */
|
2023-02-16 09:38:44 +00:00
|
|
|
const translateWithFallback = (text: string, options?: IVariables): { translated: string; isFallback?: boolean } => {
|
2022-01-10 13:54:57 +00:00
|
|
|
const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() });
|
2022-05-16 12:28:24 +00:00
|
|
|
if (!translated || translated.startsWith("missing translation:")) {
|
2022-01-07 15:20:24 +00:00
|
|
|
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
|
2022-05-16 12:28:24 +00:00
|
|
|
if (
|
|
|
|
(!fallbackTranslated || fallbackTranslated.startsWith("missing translation:")) &&
|
|
|
|
process.env.NODE_ENV !== "development"
|
|
|
|
) {
|
|
|
|
// Even the translation via FALLBACK_LOCALE failed; this can happen if
|
|
|
|
//
|
|
|
|
// 1. The string isn't in the translations dictionary, usually because you're in develop
|
|
|
|
// and haven't run yarn i18n
|
|
|
|
// 2. Loading the translation resources over the network failed, which can happen due to
|
|
|
|
// to network or if the client tried to load a translation that's been removed from the
|
|
|
|
// server.
|
|
|
|
//
|
|
|
|
// At this point, its the lesser evil to show the untranslated text, which
|
|
|
|
// will be in English, so the user can still make out *something*, rather than an opaque
|
|
|
|
// "missing translation" error.
|
|
|
|
//
|
|
|
|
// Don't do this in develop so people remember to run yarn i18n.
|
|
|
|
return { translated: text, isFallback: true };
|
|
|
|
}
|
2022-01-05 10:37:28 +00:00
|
|
|
return { translated: fallbackTranslated, isFallback: true };
|
|
|
|
}
|
|
|
|
return { translated };
|
|
|
|
};
|
|
|
|
|
2017-11-13 19:19:33 +00:00
|
|
|
// Wrapper for counterpart's translation function so that it handles nulls and undefineds properly
|
2017-11-14 18:55:47 +00:00
|
|
|
// Takes the same arguments as counterpart.translate()
|
2023-02-16 09:38:44 +00:00
|
|
|
function safeCounterpartTranslate(text: string, variables?: IVariables): { translated: string; isFallback?: boolean } {
|
2022-01-05 10:37:28 +00:00
|
|
|
// Don't do substitutions in counterpart. We handle it ourselves so we can replace with React components
|
|
|
|
// However, still pass the variables to counterpart so that it can choose the correct plural if count is given
|
|
|
|
// It is enough to pass the count variable, but in the future counterpart might make use of other information too
|
2023-01-12 13:25:14 +00:00
|
|
|
const options: IVariables & {
|
|
|
|
interpolate: boolean;
|
|
|
|
} = { ...variables, interpolate: false };
|
2022-01-05 10:37:28 +00:00
|
|
|
|
2020-08-03 15:02:26 +00:00
|
|
|
// Horrible hack to avoid https://github.com/vector-im/element-web/issues/4191
|
2017-06-06 12:56:37 +00:00
|
|
|
// 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.
|
2018-01-12 16:21:30 +00:00
|
|
|
if (options && typeof options === "object") {
|
|
|
|
Object.keys(options).forEach((k) => {
|
|
|
|
if (options[k] === undefined) {
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("safeCounterpartTranslate called with undefined interpolation name: " + k);
|
2018-01-12 16:21:30 +00:00
|
|
|
options[k] = "undefined";
|
2017-06-06 12:56:37 +00:00
|
|
|
}
|
2018-01-12 16:21:30 +00:00
|
|
|
if (options[k] === null) {
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("safeCounterpartTranslate called with null interpolation name: " + k);
|
2018-01-12 16:21:30 +00:00
|
|
|
options[k] = "null";
|
2017-06-06 12:56:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-05 10:37:28 +00:00
|
|
|
return translateWithFallback(text, options);
|
2017-05-25 10:24:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:32:05 +00:00
|
|
|
/**
|
|
|
|
* The value a variable or tag can take for a translation interpolation.
|
|
|
|
*/
|
2021-05-12 18:28:22 +00:00
|
|
|
type SubstitutionValue = number | string | React.ReactNode | ((sub: string) => React.ReactNode);
|
|
|
|
|
2020-10-13 16:40:34 +00:00
|
|
|
export interface IVariables {
|
2020-07-02 22:15:08 +00:00
|
|
|
count?: number;
|
2021-05-12 18:28:22 +00:00
|
|
|
[key: string]: SubstitutionValue;
|
2020-07-02 22:15:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
export type Tags = Record<string, SubstitutionValue>;
|
2020-07-02 22:15:08 +00:00
|
|
|
|
2020-11-18 03:38:59 +00:00
|
|
|
export type TranslatedString = string | React.ReactNode;
|
|
|
|
|
2022-01-05 10:37:28 +00:00
|
|
|
// For development/testing purposes it is useful to also output the original string
|
|
|
|
// Don't do that for release versions
|
2023-08-22 15:32:05 +00:00
|
|
|
const annotateStrings = (result: TranslatedString, translationKey: TranslationKey): TranslatedString => {
|
2022-01-05 10:37:28 +00:00
|
|
|
if (!ANNOTATE_STRINGS) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof result === "string") {
|
|
|
|
return `@@${translationKey}##${result}@@`;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<span className="translated-string" data-orig-string={translationKey}>
|
|
|
|
{result}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-30 14:55:21 +00:00
|
|
|
/*
|
2017-11-13 19:19:33 +00:00
|
|
|
* Translates text and optionally also replaces XML-ish elements in the text with e.g. React components
|
|
|
|
* @param {string} text The untranslated text, e.g "click <a>here</a> now to %(foo)s".
|
|
|
|
* @param {object} variables Variable substitutions, e.g { foo: 'bar' }
|
|
|
|
* @param {object} tags Tag substitutions e.g. { 'a': (sub) => <a>{sub}</a> }
|
2017-05-30 14:55:21 +00:00
|
|
|
*
|
2017-11-14 18:55:47 +00:00
|
|
|
* In both variables and tags, the values to substitute with can be either simple strings, React components,
|
|
|
|
* or functions that return the value to use in the substitution (e.g. return a React component). In case of
|
|
|
|
* a tag replacement, the function receives as the argument the text inside the element corresponding to the tag.
|
|
|
|
*
|
|
|
|
* Use tag substitutions if you need to translate text between tags (e.g. "<a>Click here!</a>"), otherwise
|
|
|
|
* you will end up with literal "<a>" in your output, rather than HTML. Note that you can also use variable
|
|
|
|
* substitution to insert React components, but you can't use it to translate text between tags.
|
2017-05-30 14:55:21 +00:00
|
|
|
*
|
2017-11-13 19:19:33 +00:00
|
|
|
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
|
|
|
|
*/
|
2021-07-19 21:43:11 +00:00
|
|
|
// eslint-next-line @typescript-eslint/naming-convention
|
2023-08-22 15:32:05 +00:00
|
|
|
export function _t(text: TranslationKey, variables?: IVariables): string;
|
|
|
|
export function _t(text: TranslationKey, variables: IVariables | undefined, tags: Tags): React.ReactNode;
|
|
|
|
export function _t(text: TranslationKey, variables?: IVariables, tags?: Tags): TranslatedString {
|
2017-11-13 19:19:33 +00:00
|
|
|
// The translation returns text so there's no XSS vector here (no unsafe HTML, no code execution)
|
2022-01-05 10:37:28 +00:00
|
|
|
const { translated } = safeCounterpartTranslate(text, variables);
|
2018-10-12 03:05:59 +00:00
|
|
|
const substituted = substitute(translated, variables, tags);
|
2017-12-09 13:22:20 +00:00
|
|
|
|
2022-01-05 10:37:28 +00:00
|
|
|
return annotateStrings(substituted, text);
|
|
|
|
}
|
|
|
|
|
2023-09-22 15:39:40 +00:00
|
|
|
/**
|
|
|
|
* Utility function to look up a string by its translation key without resolving variables & tags
|
|
|
|
* @param key - the translation key to return the value for
|
|
|
|
*/
|
|
|
|
export function lookupString(key: TranslationKey): string {
|
|
|
|
return safeCounterpartTranslate(key, {}).translated;
|
|
|
|
}
|
|
|
|
|
2022-01-05 10:37:28 +00:00
|
|
|
/*
|
|
|
|
* Wraps normal _t function and adds atttribution for translations that used a fallback locale
|
|
|
|
* Wraps translations that fell back from active locale to fallback locale with a `<span lang=<fallback locale>>`
|
|
|
|
* @param {string} text The untranslated text, e.g "click <a>here</a> now to %(foo)s".
|
|
|
|
* @param {object} variables Variable substitutions, e.g { foo: 'bar' }
|
|
|
|
* @param {object} tags Tag substitutions e.g. { 'a': (sub) => <a>{sub}</a> }
|
|
|
|
*
|
|
|
|
* @return a React <span> component if any non-strings were used in substitutions
|
|
|
|
* or translation used a fallback locale, otherwise a string
|
|
|
|
*/
|
|
|
|
// eslint-next-line @typescript-eslint/naming-convention
|
2023-08-22 15:32:05 +00:00
|
|
|
export function _tDom(text: TranslationKey, variables?: IVariables): TranslatedString;
|
|
|
|
export function _tDom(text: TranslationKey, variables: IVariables, tags: Tags): React.ReactNode;
|
|
|
|
export function _tDom(text: TranslationKey, variables?: IVariables, tags?: Tags): TranslatedString {
|
2022-01-05 10:37:28 +00:00
|
|
|
// The translation returns text so there's no XSS vector here (no unsafe HTML, no code execution)
|
|
|
|
const { translated, isFallback } = safeCounterpartTranslate(text, variables);
|
|
|
|
const substituted = substitute(translated, variables, tags);
|
|
|
|
|
|
|
|
// wrap en fallback translation with lang attribute for screen readers
|
|
|
|
const result = isFallback ? <span lang="en">{substituted}</span> : substituted;
|
|
|
|
|
|
|
|
return annotateStrings(result, text);
|
2017-11-13 19:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 17:43:05 +00:00
|
|
|
/**
|
|
|
|
* Sanitizes unsafe text for the sanitizer, ensuring references to variables will not be considered
|
|
|
|
* replaceable by the translation functions.
|
|
|
|
* @param {string} text The text to sanitize.
|
|
|
|
* @returns {string} The sanitized text.
|
|
|
|
*/
|
|
|
|
export function sanitizeForTranslation(text: string): string {
|
|
|
|
// Add a non-breaking space so the regex doesn't trigger when translating.
|
2021-07-31 17:50:25 +00:00
|
|
|
return text.replace(/%\(([^)]*)\)/g, "%\xa0($1)");
|
2021-07-31 17:43:05 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 19:19:33 +00:00
|
|
|
/*
|
|
|
|
* Similar to _t(), except only does substitutions, and no translation
|
|
|
|
* @param {string} text The text, e.g "click <a>here</a> now to %(foo)s".
|
|
|
|
* @param {object} variables Variable substitutions, e.g { foo: 'bar' }
|
|
|
|
* @param {object} tags Tag substitutions e.g. { 'a': (sub) => <a>{sub}</a> }
|
2017-05-30 14:55:21 +00:00
|
|
|
*
|
2017-11-13 19:19:33 +00:00
|
|
|
* The values to substitute with can be either simple strings, or functions that return the value to use in
|
|
|
|
* the substitution (e.g. return a React component). In case of a tag replacement, the function receives as
|
|
|
|
* the argument the text inside the element corresponding to the tag.
|
2017-05-30 14:55:21 +00:00
|
|
|
*
|
2017-11-13 19:19:33 +00:00
|
|
|
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
|
2017-05-30 14:55:21 +00:00
|
|
|
*/
|
2020-07-02 22:15:08 +00:00
|
|
|
export function substitute(text: string, variables?: IVariables): string;
|
2023-02-16 09:38:44 +00:00
|
|
|
export function substitute(text: string, variables: IVariables | undefined, tags: Tags | undefined): string;
|
2020-07-02 22:15:08 +00:00
|
|
|
export function substitute(text: string, variables?: IVariables, tags?: Tags): string | React.ReactNode {
|
|
|
|
let result: React.ReactNode | string = text;
|
2017-11-13 19:19:33 +00:00
|
|
|
|
2017-11-16 11:19:56 +00:00
|
|
|
if (variables !== undefined) {
|
2020-07-02 22:15:08 +00:00
|
|
|
const regexpMapping: IVariables = {};
|
2017-11-13 19:19:33 +00:00
|
|
|
for (const variable in variables) {
|
|
|
|
regexpMapping[`%\\(${variable}\\)s`] = variables[variable];
|
2017-05-30 15:17:42 +00:00
|
|
|
}
|
2020-07-02 22:15:08 +00:00
|
|
|
result = replaceByRegexes(result as string, regexpMapping);
|
2017-11-13 19:19:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:19:56 +00:00
|
|
|
if (tags !== undefined) {
|
2020-07-02 22:15:08 +00:00
|
|
|
const regexpMapping: Tags = {};
|
2017-11-13 19:19:33 +00:00
|
|
|
for (const tag in tags) {
|
|
|
|
regexpMapping[`(<${tag}>(.*?)<\\/${tag}>|<${tag}>|<${tag}\\s*\\/>)`] = tags[tag];
|
2017-05-30 15:17:42 +00:00
|
|
|
}
|
2020-07-02 22:15:08 +00:00
|
|
|
result = replaceByRegexes(result as string, regexpMapping);
|
2017-05-30 14:55:21 +00:00
|
|
|
}
|
2019-05-17 10:53:44 +00:00
|
|
|
|
|
|
|
return result;
|
2017-11-13 19:19:33 +00:00
|
|
|
}
|
2017-05-30 14:55:21 +00:00
|
|
|
|
2023-09-22 15:39:40 +00:00
|
|
|
/**
|
2017-11-13 19:19:33 +00:00
|
|
|
* Replace parts of a text using regular expressions
|
2023-09-22 15:39:40 +00:00
|
|
|
* @param text - The text on which to perform substitutions
|
|
|
|
* @param mapping - A mapping from regular expressions in string form to replacement string or a
|
2017-11-13 19:19:33 +00:00
|
|
|
* function which will receive as the argument the capture groups defined in the regexp. E.g.
|
|
|
|
* { 'Hello (.?) World': (sub) => sub.toUpperCase() }
|
|
|
|
*
|
|
|
|
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
|
|
|
|
*/
|
2020-07-02 22:15:08 +00:00
|
|
|
export function replaceByRegexes(text: string, mapping: IVariables): string;
|
|
|
|
export function replaceByRegexes(text: string, mapping: Tags): React.ReactNode;
|
|
|
|
export function replaceByRegexes(text: string, mapping: IVariables | Tags): string | React.ReactNode {
|
2017-11-17 07:47:25 +00:00
|
|
|
// We initially store our output as an array of strings and objects (e.g. React components).
|
|
|
|
// This will then be converted to a string or a <span> at the end
|
2023-02-15 13:36:22 +00:00
|
|
|
const output: SubstitutionValue[] = [text];
|
2017-11-13 19:19:33 +00:00
|
|
|
|
2017-11-14 18:55:47 +00:00
|
|
|
// If we insert any components we need to wrap the output in a span. React doesn't like just an array of components.
|
|
|
|
let shouldWrapInSpan = false;
|
|
|
|
|
2017-11-13 19:19:33 +00:00
|
|
|
for (const regexpString in mapping) {
|
2017-11-14 18:55:47 +00:00
|
|
|
// TODO: Cache regexps
|
2019-08-22 17:17:08 +00:00
|
|
|
const regexp = new RegExp(regexpString, "g");
|
2017-10-11 18:02:14 +00:00
|
|
|
|
2017-11-17 07:47:25 +00:00
|
|
|
// Loop over what output we have so far and perform replacements
|
|
|
|
// We look for matches: if we find one, we get three parts: everything before the match, the replaced part,
|
|
|
|
// and everything after the match. Insert all three into the output. We need to do this because we can insert objects.
|
2019-08-22 17:17:08 +00:00
|
|
|
// Otherwise there would be no need for the splitting and we could do simple replacement.
|
2017-11-23 19:19:38 +00:00
|
|
|
let matchFoundSomewhere = false; // If we don't find a match anywhere we want to log it
|
2020-07-02 22:15:08 +00:00
|
|
|
for (let outputIndex = 0; outputIndex < output.length; outputIndex++) {
|
2017-11-17 07:47:25 +00:00
|
|
|
const inputText = output[outputIndex];
|
|
|
|
if (typeof inputText !== "string") {
|
|
|
|
// We might have inserted objects earlier, don't try to replace them
|
|
|
|
continue;
|
2017-11-14 20:34:20 +00:00
|
|
|
}
|
2017-05-30 15:17:42 +00:00
|
|
|
|
2019-08-22 17:17:08 +00:00
|
|
|
// process every match in the string
|
|
|
|
// starting with the first
|
|
|
|
let match = regexp.exec(inputText);
|
2017-11-13 19:19:33 +00:00
|
|
|
|
2019-08-22 17:17:08 +00:00
|
|
|
if (!match) continue;
|
|
|
|
matchFoundSomewhere = true;
|
2017-11-14 18:33:12 +00:00
|
|
|
|
2019-08-22 17:17:08 +00:00
|
|
|
// The textual part before the first match
|
2022-04-14 07:52:42 +00:00
|
|
|
const head = inputText.slice(0, match.index);
|
2017-11-13 19:19:33 +00:00
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
const parts: SubstitutionValue[] = [];
|
2019-08-22 17:17:08 +00:00
|
|
|
// keep track of prevMatch
|
|
|
|
let prevMatch;
|
|
|
|
while (match) {
|
|
|
|
// store prevMatch
|
|
|
|
prevMatch = match;
|
|
|
|
const capturedGroups = match.slice(2);
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
let replaced: SubstitutionValue;
|
2019-08-22 17:17:08 +00:00
|
|
|
// If substitution is a function, call it
|
|
|
|
if (mapping[regexpString] instanceof Function) {
|
2024-10-16 15:43:07 +00:00
|
|
|
replaced = ((mapping as Tags)[regexpString] as (...subs: string[]) => string)(...capturedGroups);
|
2019-08-22 17:17:08 +00:00
|
|
|
} else {
|
|
|
|
replaced = mapping[regexpString];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof replaced === "object") {
|
|
|
|
shouldWrapInSpan = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we also need to check that it actually is a string before comparing against one
|
|
|
|
// The head and tail are always strings
|
|
|
|
if (typeof replaced !== "string" || replaced !== "") {
|
|
|
|
parts.push(replaced);
|
|
|
|
}
|
|
|
|
|
|
|
|
// try the next match
|
|
|
|
match = regexp.exec(inputText);
|
|
|
|
|
|
|
|
// add the text between prevMatch and this one
|
|
|
|
// or the end of the string if prevMatch is the last match
|
2019-08-22 17:31:02 +00:00
|
|
|
let tail;
|
2019-08-22 17:17:08 +00:00
|
|
|
if (match) {
|
|
|
|
const startIndex = prevMatch.index + prevMatch[0].length;
|
2022-04-14 07:52:42 +00:00
|
|
|
tail = inputText.slice(startIndex, match.index);
|
2019-08-22 17:17:08 +00:00
|
|
|
} else {
|
2022-04-14 07:52:42 +00:00
|
|
|
tail = inputText.slice(prevMatch.index + prevMatch[0].length);
|
2019-08-22 17:31:02 +00:00
|
|
|
}
|
|
|
|
if (tail) {
|
|
|
|
parts.push(tail);
|
2019-08-22 17:17:08 +00:00
|
|
|
}
|
2017-11-17 07:47:25 +00:00
|
|
|
}
|
2017-11-13 19:19:33 +00:00
|
|
|
|
2017-11-17 07:47:25 +00:00
|
|
|
// Insert in reverse order as splice does insert-before and this way we get the final order correct
|
2019-08-22 17:17:08 +00:00
|
|
|
// remove the old element at the same time
|
|
|
|
output.splice(outputIndex, 1, ...parts);
|
2017-11-17 07:47:25 +00:00
|
|
|
|
|
|
|
if (head !== "") {
|
|
|
|
// Don't push empty nodes, they are of no use
|
|
|
|
output.splice(outputIndex, 0, head);
|
|
|
|
}
|
2017-11-14 18:33:12 +00:00
|
|
|
}
|
2017-11-23 19:19:38 +00:00
|
|
|
if (!matchFoundSomewhere) {
|
2023-03-31 07:30:43 +00:00
|
|
|
if (
|
|
|
|
// The current regexp did not match anything in the input. Missing
|
|
|
|
// matches is entirely possible because you might choose to show some
|
|
|
|
// variables only in the case of e.g. plurals. It's still a bit
|
|
|
|
// suspicious, and could be due to an error, so log it. However, not
|
|
|
|
// showing count is so common that it's not worth logging. And other
|
|
|
|
// commonly unused variables here, if there are any.
|
|
|
|
regexpString !== "%\\(count\\)s" &&
|
|
|
|
// Ignore the `locale` option which can be used to override the locale
|
|
|
|
// in counterpart
|
|
|
|
regexpString !== "%\\(locale\\)s"
|
|
|
|
) {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Could not find ${regexp} in ${text}`);
|
2017-11-23 19:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-30 14:55:21 +00:00
|
|
|
}
|
2017-05-30 15:17:42 +00:00
|
|
|
|
2017-11-16 11:19:56 +00:00
|
|
|
if (shouldWrapInSpan) {
|
2017-11-13 19:19:33 +00:00
|
|
|
return React.createElement("span", null, ...output);
|
|
|
|
} else {
|
|
|
|
return output.join("");
|
|
|
|
}
|
2017-05-30 14:55:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 14:29:11 +00:00
|
|
|
// Allow overriding the text displayed when no translation exists
|
2017-06-01 01:04:43 +00:00
|
|
|
// Currently only used in unit tests to avoid having to load
|
2020-08-03 15:02:26 +00:00
|
|
|
// the translations in element-web
|
2023-01-12 13:25:14 +00:00
|
|
|
export function setMissingEntryGenerator(f: (value: string) => void): void {
|
2017-05-26 14:29:11 +00:00
|
|
|
counterpart.setMissingEntryGenerator(f);
|
|
|
|
}
|
|
|
|
|
2022-10-13 08:22:32 +00:00
|
|
|
type Languages = {
|
2023-08-22 14:07:16 +00:00
|
|
|
[lang: string]: string;
|
2022-10-12 17:59:07 +00:00
|
|
|
};
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function setLanguage(preferredLangs: string | string[]): Promise<void> {
|
2017-05-25 15:45:32 +00:00
|
|
|
if (!Array.isArray(preferredLangs)) {
|
|
|
|
preferredLangs = [preferredLangs];
|
|
|
|
}
|
2017-05-23 14:16:31 +00:00
|
|
|
|
2020-02-24 17:11:08 +00:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
if (plaf) {
|
|
|
|
plaf.setLanguage(preferredLangs);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:59:07 +00:00
|
|
|
let langToUse: string;
|
2022-10-13 08:22:32 +00:00
|
|
|
let availLangs: Languages;
|
2017-05-25 15:45:32 +00:00
|
|
|
return getLangsJson()
|
|
|
|
.then((result) => {
|
|
|
|
availLangs = result;
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2017-05-25 15:45:32 +00:00
|
|
|
for (let i = 0; i < preferredLangs.length; ++i) {
|
|
|
|
if (availLangs.hasOwnProperty(preferredLangs[i])) {
|
|
|
|
langToUse = preferredLangs[i];
|
2017-05-26 21:28:35 +00:00
|
|
|
break;
|
2022-12-12 11:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 15:45:32 +00:00
|
|
|
if (!langToUse) {
|
2017-06-01 19:02:40 +00:00
|
|
|
// Fallback to en_EN if none is found
|
2019-08-14 11:29:48 +00:00
|
|
|
langToUse = "en";
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Unable to find an appropriate language");
|
2017-05-23 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 14:07:16 +00:00
|
|
|
return getLanguageRetry(i18nFolder + availLangs[langToUse]);
|
2022-03-01 18:53:09 +00:00
|
|
|
})
|
2023-02-15 13:36:22 +00:00
|
|
|
.then(async (langData): Promise<ICounterpartTranslation | undefined> => {
|
2017-05-25 15:45:32 +00:00
|
|
|
counterpart.registerTranslations(langToUse, langData);
|
2022-03-01 18:53:09 +00:00
|
|
|
await registerCustomTranslations();
|
2017-05-25 15:45:32 +00:00
|
|
|
counterpart.setLocale(langToUse);
|
2022-03-01 18:53:09 +00:00
|
|
|
await SettingsStore.setValue("language", null, SettingLevel.DEVICE, langToUse);
|
2021-05-20 12:34:31 +00:00
|
|
|
// Adds a lot of noise to test runs, so disable logging there.
|
|
|
|
if (process.env.NODE_ENV !== "test") {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("set language to " + langToUse);
|
2021-05-20 12:34:31 +00:00
|
|
|
}
|
2017-05-25 15:45:32 +00:00
|
|
|
|
|
|
|
// Set 'en' as fallback language:
|
2019-08-14 11:29:48 +00:00
|
|
|
if (langToUse !== "en") {
|
2023-08-22 14:07:16 +00:00
|
|
|
return getLanguageRetry(i18nFolder + availLangs["en"]);
|
2017-05-23 14:16:31 +00:00
|
|
|
}
|
2022-03-01 18:53:09 +00:00
|
|
|
})
|
2023-01-12 13:25:14 +00:00
|
|
|
.then(async (langData): Promise<void> => {
|
2017-06-01 19:10:58 +00:00
|
|
|
if (langData) counterpart.registerTranslations("en", langData);
|
2022-03-01 18:53:09 +00:00
|
|
|
await registerCustomTranslations();
|
2017-05-23 14:16:31 +00:00
|
|
|
});
|
2017-10-11 16:56:17 +00:00
|
|
|
}
|
2017-05-23 14:16:31 +00:00
|
|
|
|
2022-10-13 08:22:32 +00:00
|
|
|
type Language = {
|
|
|
|
value: string;
|
2023-08-22 14:07:16 +00:00
|
|
|
label: string; // translated
|
|
|
|
labelInTargetLanguage: string; // translated
|
2022-10-13 08:22:32 +00:00
|
|
|
};
|
|
|
|
|
2023-08-22 14:07:16 +00:00
|
|
|
export async function getAllLanguagesFromJson(): Promise<string[]> {
|
|
|
|
return Object.keys(await getLangsJson());
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllLanguagesWithLabels(): Promise<Language[]> {
|
|
|
|
const languageNames = new Intl.DisplayNames([getUserLanguage()], { type: "language", style: "short" });
|
|
|
|
const languages = await getAllLanguagesFromJson();
|
|
|
|
return languages.map<Language>((langKey) => {
|
|
|
|
return {
|
|
|
|
value: langKey,
|
|
|
|
label: languageNames.of(langKey)!,
|
|
|
|
labelInTargetLanguage: new Intl.DisplayNames([langKey], { type: "language", style: "short" }).of(langKey)!,
|
|
|
|
};
|
2017-05-25 15:45:32 +00:00
|
|
|
});
|
2017-05-23 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function getLanguagesFromBrowser(): readonly string[] {
|
2017-05-29 18:58:18 +00:00
|
|
|
if (navigator.languages && navigator.languages.length) return navigator.languages;
|
|
|
|
if (navigator.language) return [navigator.language];
|
2017-09-21 17:56:34 +00:00
|
|
|
return [navigator.userLanguage || "en"];
|
2017-05-29 18:58:18 +00:00
|
|
|
}
|
2017-05-23 14:16:31 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function getLanguageFromBrowser(): string {
|
2019-12-20 01:29:22 +00:00
|
|
|
return getLanguagesFromBrowser()[0];
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function getCurrentLanguage(): string {
|
2017-05-25 18:53:27 +00:00
|
|
|
return counterpart.getLocale();
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:51:56 +00:00
|
|
|
/**
|
|
|
|
* Given a list of language codes, pick the most appropriate one
|
|
|
|
* given the current language (ie. getCurrentLanguage())
|
|
|
|
* English is assumed to be a reasonable default.
|
|
|
|
*
|
|
|
|
* @param {string[]} langs List of language codes to pick from
|
|
|
|
* @returns {string} The most appropriate language code from langs
|
|
|
|
*/
|
2020-07-02 22:15:08 +00:00
|
|
|
export function pickBestLanguage(langs: string[]): string {
|
2019-07-09 17:51:56 +00:00
|
|
|
const currentLang = getCurrentLanguage();
|
|
|
|
const normalisedLangs = langs.map(normalizeLanguageKey);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Best is an exact match
|
|
|
|
const currentLangIndex = normalisedLangs.indexOf(currentLang);
|
|
|
|
if (currentLangIndex > -1) return langs[currentLangIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-14 11:29:48 +00:00
|
|
|
// Failing that, a different dialect of the same language
|
2022-04-14 07:52:42 +00:00
|
|
|
const closeLangIndex = normalisedLangs.findIndex((l) => l.slice(0, 2) === currentLang.slice(0, 2));
|
2019-07-09 17:51:56 +00:00
|
|
|
if (closeLangIndex > -1) return langs[closeLangIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Neither of those? Try an english variant.
|
2020-07-02 22:15:08 +00:00
|
|
|
const enIndex = normalisedLangs.findIndex((l) => l.startsWith("en"));
|
2019-07-09 17:51:56 +00:00
|
|
|
if (enIndex > -1) return langs[enIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing else, use the first
|
|
|
|
return langs[0];
|
|
|
|
}
|
|
|
|
|
2022-10-13 08:22:32 +00:00
|
|
|
async function getLangsJson(): Promise<Languages> {
|
2022-10-12 17:59:07 +00:00
|
|
|
let url: string;
|
|
|
|
if (typeof webpackLangJsonUrl === "string") {
|
|
|
|
// in Jest this 'url' isn't a URL, so just fall through
|
|
|
|
url = webpackLangJsonUrl;
|
|
|
|
} else {
|
|
|
|
url = i18nFolder + "languages.json";
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await fetch(url, { method: "GET" });
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Failed to load ${url}, got ${res.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json();
|
2017-05-25 15:45:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:44:29 +00:00
|
|
|
interface ICounterpartTranslation {
|
|
|
|
[key: string]:
|
|
|
|
| string
|
|
|
|
| {
|
|
|
|
[pluralisation: string]: string;
|
|
|
|
};
|
2017-11-01 14:55:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:44:29 +00:00
|
|
|
async function getLanguageRetry(langPath: string, num = 3): Promise<ICounterpartTranslation> {
|
2020-09-15 12:19:47 +00:00
|
|
|
return retry(
|
|
|
|
() => getLanguage(langPath),
|
|
|
|
num,
|
|
|
|
(e) => {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("Failed to load i18n", langPath);
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error(e);
|
2020-09-15 12:19:47 +00:00
|
|
|
return true; // always retry
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:59:07 +00:00
|
|
|
async function getLanguage(langPath: string): Promise<ICounterpartTranslation> {
|
|
|
|
const res = await fetch(langPath, { method: "GET" });
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(`Failed to load ${langPath}, got ${res.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json();
|
2017-05-25 15:45:32 +00:00
|
|
|
}
|
2022-03-01 18:53:09 +00:00
|
|
|
|
2023-10-02 10:44:25 +00:00
|
|
|
let cachedCustomTranslations: Optional<TranslationStringsObject> = null;
|
2022-03-01 18:53:09 +00:00
|
|
|
let cachedCustomTranslationsExpire = 0; // zero to trigger expiration right away
|
|
|
|
|
|
|
|
// This awkward class exists so the test runner can get at the function. It is
|
|
|
|
// not intended for practical or realistic usage.
|
|
|
|
export class CustomTranslationOptions {
|
2023-10-02 10:44:25 +00:00
|
|
|
public static lookupFn?: (url: string) => TranslationStringsObject;
|
2022-03-01 18:53:09 +00:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
// static access for tests only
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 10:44:25 +00:00
|
|
|
function doRegisterTranslations(customTranslations: TranslationStringsObject): void {
|
|
|
|
// We convert the operator-friendly version into something counterpart can consume.
|
2023-03-28 10:23:16 +00:00
|
|
|
// Map: lang → Record: string → translation
|
|
|
|
const langs: MapWithDefault<string, Record<string, string>> = new MapWithDefault(() => ({}));
|
2023-10-02 10:44:25 +00:00
|
|
|
for (const [translationKey, translations] of Object.entries(customTranslations)) {
|
|
|
|
for (const [lang, translation] of Object.entries(translations)) {
|
|
|
|
_.set(langs.getOrCreate(lang), translationKey.split(KEY_SEPARATOR), translation);
|
2022-07-05 18:26:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, tell counterpart about our translations
|
2023-03-28 10:23:16 +00:00
|
|
|
for (const [lang, translations] of langs) {
|
2022-07-05 18:26:44 +00:00
|
|
|
counterpart.registerTranslations(lang, translations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:53:09 +00:00
|
|
|
/**
|
2022-07-05 18:26:44 +00:00
|
|
|
* Any custom modules with translations to load are parsed first, followed by an
|
|
|
|
* optionally defined translations file in the config. If no customization is made,
|
|
|
|
* or the file can't be parsed, no action will be taken.
|
2022-03-01 18:53:09 +00:00
|
|
|
*
|
|
|
|
* This function should be called *after* registering other translations data to
|
|
|
|
* ensure it overrides strings properly.
|
|
|
|
*/
|
2023-03-31 07:30:43 +00:00
|
|
|
export async function registerCustomTranslations({
|
|
|
|
testOnlyIgnoreCustomTranslationsCache = false,
|
|
|
|
}: {
|
|
|
|
testOnlyIgnoreCustomTranslationsCache?: boolean;
|
|
|
|
} = {}): Promise<void> {
|
2022-07-05 18:26:44 +00:00
|
|
|
const moduleTranslations = ModuleRunner.instance.allTranslations;
|
|
|
|
doRegisterTranslations(moduleTranslations);
|
|
|
|
|
2022-03-01 18:53:09 +00:00
|
|
|
const lookupUrl = SdkConfig.get().custom_translations_url;
|
|
|
|
if (!lookupUrl) return; // easy - nothing to do
|
|
|
|
|
|
|
|
try {
|
2023-10-02 10:44:25 +00:00
|
|
|
let json: Optional<TranslationStringsObject>;
|
2023-03-31 07:30:43 +00:00
|
|
|
if (testOnlyIgnoreCustomTranslationsCache || Date.now() >= cachedCustomTranslationsExpire) {
|
2022-03-01 18:53:09 +00:00
|
|
|
json = CustomTranslationOptions.lookupFn
|
|
|
|
? CustomTranslationOptions.lookupFn(lookupUrl)
|
2023-10-02 10:44:25 +00:00
|
|
|
: ((await (await fetch(lookupUrl)).json()) as TranslationStringsObject);
|
2022-03-01 18:53:09 +00:00
|
|
|
cachedCustomTranslations = json;
|
|
|
|
|
|
|
|
// Set expiration to the future, but not too far. Just trying to avoid
|
|
|
|
// repeated, successive, calls to the server rather than anything long-term.
|
|
|
|
cachedCustomTranslationsExpire = Date.now() + 5 * 60 * 1000;
|
|
|
|
} else {
|
|
|
|
json = cachedCustomTranslations;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the (potentially cached) json is invalid, don't use it.
|
|
|
|
if (!json) return;
|
|
|
|
|
2022-07-05 18:26:44 +00:00
|
|
|
// Finally, register it.
|
|
|
|
doRegisterTranslations(json);
|
2022-03-01 18:53:09 +00:00
|
|
|
} catch (e) {
|
|
|
|
// We consume all exceptions because it's considered non-fatal for custom
|
|
|
|
// translations to break. Most failures will be during initial development
|
|
|
|
// of the json file and not (hopefully) at runtime.
|
|
|
|
logger.warn("Ignoring error while registering custom translations: ", e);
|
|
|
|
|
|
|
|
// Like above: trigger a cache of the json to avoid successive calls.
|
|
|
|
cachedCustomTranslationsExpire = Date.now() + 5 * 60 * 1000;
|
|
|
|
}
|
|
|
|
}
|