mirror of
https://github.com/zulip/zulip.git
synced 2025-11-13 10:26:28 +00:00
i18n: Initialize FormatJS.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
6cf398f157
commit
4b4cea90a3
@@ -1,5 +1,59 @@
|
||||
"use strict";
|
||||
|
||||
const {createIntl, createIntlCache} = require("@formatjs/intl");
|
||||
const _ = require("lodash");
|
||||
|
||||
const cache = createIntlCache();
|
||||
|
||||
exports.intl = createIntl(
|
||||
{
|
||||
locale: "en",
|
||||
defaultLocale: "en",
|
||||
defaultRichTextElements: Object.fromEntries(
|
||||
["b", "code", "em", "i", "kbd", "p", "strong"].map((tag) => [
|
||||
tag,
|
||||
(content_html) => `<${tag}>${content_html}</${tag}>`,
|
||||
]),
|
||||
),
|
||||
},
|
||||
cache,
|
||||
);
|
||||
|
||||
exports.$t = (descriptor, values) =>
|
||||
"translated: " +
|
||||
exports.intl.formatMessage(
|
||||
{
|
||||
...descriptor,
|
||||
id: `${descriptor.defaultMessage}#${descriptor.description}`,
|
||||
},
|
||||
values,
|
||||
);
|
||||
|
||||
const default_html_elements = Object.fromEntries(
|
||||
["b", "code", "em", "i", "kbd", "p", "strong"].map((tag) => [
|
||||
tag,
|
||||
(content_html) => `<${tag}>${content_html}</${tag}>`,
|
||||
]),
|
||||
);
|
||||
|
||||
exports.$t_html = (descriptor, values) =>
|
||||
"translated HTML: " +
|
||||
exports.intl.formatMessage(
|
||||
{
|
||||
...descriptor,
|
||||
id: `${descriptor.defaultMessage}#${descriptor.description}`,
|
||||
},
|
||||
{
|
||||
...default_html_elements,
|
||||
...Object.fromEntries(
|
||||
Object.entries(values ?? {}).map(([key, value]) => [
|
||||
key,
|
||||
typeof value === "function" ? value : _.escape(value),
|
||||
]),
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
exports.i18n = {};
|
||||
exports.i18n.t = function (str, context) {
|
||||
// HAPPY PATH: most translations are a simple string:
|
||||
|
||||
Reference in New Issue
Block a user