2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-04-10 03:11:59 +02:00
|
|
|
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,
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2022-11-03 20:14:03 +01:00
|
|
|
(content_html) => `<${tag}>${content_html.join("")}</${tag}>`,
|
2021-04-10 03:11:59 +02:00
|
|
|
]),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
cache,
|
|
|
|
);
|
|
|
|
|
2024-05-21 00:26:26 +02:00
|
|
|
exports.$t = (descriptor, values) => {
|
|
|
|
descriptor = {
|
|
|
|
id: `${descriptor.defaultMessage}#${descriptor.description}`,
|
|
|
|
...descriptor,
|
|
|
|
};
|
|
|
|
return "translated: " + exports.intl.formatMessage(descriptor, values);
|
|
|
|
};
|
2021-04-10 03:11:59 +02:00
|
|
|
|
|
|
|
const default_html_elements = Object.fromEntries(
|
|
|
|
["b", "code", "em", "i", "kbd", "p", "strong"].map((tag) => [
|
|
|
|
tag,
|
2022-11-03 20:14:03 +01:00
|
|
|
(content_html) => `<${tag}>${content_html.join("")}</${tag}>`,
|
2021-04-10 03:11:59 +02:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2024-05-21 00:26:26 +02:00
|
|
|
exports.$t_html = (descriptor, values) => {
|
|
|
|
descriptor = {
|
|
|
|
id: `${descriptor.defaultMessage}#${descriptor.description}`,
|
|
|
|
...descriptor,
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
"translated HTML: " +
|
|
|
|
exports.intl.formatMessage(descriptor, {
|
2021-04-10 03:11:59 +02:00
|
|
|
...default_html_elements,
|
|
|
|
...Object.fromEntries(
|
|
|
|
Object.entries(values ?? {}).map(([key, value]) => [
|
|
|
|
key,
|
|
|
|
typeof value === "function" ? value : _.escape(value),
|
|
|
|
]),
|
|
|
|
),
|
2024-05-21 00:26:26 +02:00
|
|
|
})
|
2021-04-10 03:11:59 +02:00
|
|
|
);
|
2024-05-21 00:26:26 +02:00
|
|
|
};
|