2020-09-18 20:43:19 +02:00
|
|
|
// For documentation on i18n in Zulip, see:
|
|
|
|
// https://zulip.readthedocs.io/en/latest/translating/internationalization.html
|
|
|
|
|
2021-04-14 21:12:07 +02:00
|
|
|
import {DEFAULT_INTL_CONFIG, IntlErrorCode, createIntl, createIntlCache} from "@formatjs/intl";
|
2021-04-10 03:11:59 +02:00
|
|
|
import _ from "lodash";
|
2016-05-13 12:44:03 +02:00
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
|
|
|
|
2021-04-10 03:11:59 +02:00
|
|
|
const cache = createIntlCache();
|
|
|
|
export const intl = createIntl(
|
|
|
|
{
|
|
|
|
locale: page_params.default_language,
|
|
|
|
defaultLocale: "en",
|
|
|
|
messages: page_params.translation_data,
|
2021-04-14 21:12:07 +02:00
|
|
|
onError: /* istanbul ignore next */ (error) => {
|
|
|
|
// Ignore complaints about untranslated strings that were
|
|
|
|
// added since the last sync-translations run.
|
|
|
|
if (error.code !== IntlErrorCode.MISSING_TRANSLATION) {
|
|
|
|
DEFAULT_INTL_CONFIG.onError(error);
|
|
|
|
}
|
|
|
|
},
|
2021-04-10 03:11:59 +02:00
|
|
|
},
|
|
|
|
cache,
|
|
|
|
);
|
|
|
|
|
|
|
|
export const $t = intl.formatMessage;
|
|
|
|
|
|
|
|
export const default_html_elements = Object.fromEntries(
|
|
|
|
["b", "code", "em", "i", "kbd", "p", "strong"].map((tag) => [
|
|
|
|
tag,
|
|
|
|
(content_html) => `<${tag}>${content_html}</${tag}>`,
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
export function $t_html(descriptor, values) {
|
|
|
|
return intl.formatMessage(descriptor, {
|
|
|
|
...default_html_elements,
|
|
|
|
...Object.fromEntries(
|
|
|
|
Object.entries(values ?? {}).map(([key, value]) => [
|
|
|
|
key,
|
|
|
|
typeof value === "function" ? value : _.escape(value),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|