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-05-27 20:08:50 +02:00
|
|
|
import type {MessageDescriptor} from "@formatjs/intl";
|
2021-04-14 21:12:07 +02:00
|
|
|
import {DEFAULT_INTL_CONFIG, IntlErrorCode, createIntl, createIntlCache} from "@formatjs/intl";
|
2021-05-27 20:08:50 +02:00
|
|
|
import type {FormatXMLElementFn, PrimitiveType} from "intl-messageformat";
|
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(
|
|
|
|
{
|
2021-04-24 01:32:32 +02:00
|
|
|
locale: page_params.request_language,
|
2021-04-10 03:11:59 +02:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
|
2021-09-23 00:27:09 +02:00
|
|
|
export const $t = intl.formatMessage.bind(intl);
|
2021-04-10 03:11:59 +02:00
|
|
|
|
|
|
|
export const default_html_elements = Object.fromEntries(
|
|
|
|
["b", "code", "em", "i", "kbd", "p", "strong"].map((tag) => [
|
|
|
|
tag,
|
2021-05-27 20:08:50 +02:00
|
|
|
(content_html: string) => `<${tag}>${content_html}</${tag}>`,
|
2021-04-10 03:11:59 +02:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2021-05-27 20:08:50 +02:00
|
|
|
export function $t_html(
|
|
|
|
descriptor: MessageDescriptor,
|
|
|
|
values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>,
|
|
|
|
): string {
|
2021-04-10 03:11:59 +02:00
|
|
|
return intl.formatMessage(descriptor, {
|
|
|
|
...default_html_elements,
|
|
|
|
...Object.fromEntries(
|
|
|
|
Object.entries(values ?? {}).map(([key, value]) => [
|
|
|
|
key,
|
2021-05-27 20:08:50 +02:00
|
|
|
typeof value === "function" ? value : _.escape(value?.toString()),
|
2021-04-10 03:11:59 +02:00
|
|
|
]),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
2021-06-15 22:37:07 +02:00
|
|
|
|
2021-05-27 20:08:50 +02:00
|
|
|
export let language_list: typeof page_params["language_list"];
|
2021-06-18 02:16:48 +02:00
|
|
|
|
2021-05-27 20:08:50 +02:00
|
|
|
export function get_language_name(language_code: string): string {
|
|
|
|
const language_list_map: Record<string, string> = {};
|
2021-06-18 02:16:48 +02:00
|
|
|
|
|
|
|
// One-to-one mapping from code to name for all languages
|
|
|
|
for (const language of language_list) {
|
|
|
|
language_list_map[language.code] = language.name;
|
|
|
|
}
|
|
|
|
return language_list_map[language_code];
|
|
|
|
}
|
2021-06-15 22:37:07 +02:00
|
|
|
|
2021-05-27 20:08:50 +02:00
|
|
|
export function initialize(language_params: {language_list: typeof language_list}): void {
|
2021-06-18 02:16:48 +02:00
|
|
|
const language_list_raw = language_params.language_list;
|
|
|
|
|
|
|
|
// Limit offered languages to options with percentage translation >= 5%
|
|
|
|
language_list = [];
|
|
|
|
for (const language of language_list_raw) {
|
2021-06-15 22:37:07 +02:00
|
|
|
if (language.percent_translated === undefined || language.percent_translated >= 5) {
|
|
|
|
language_list.push({
|
|
|
|
code: language.code,
|
|
|
|
locale: language.locale,
|
|
|
|
name: language.name,
|
|
|
|
percent_translated: language.percent_translated,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 02:16:48 +02:00
|
|
|
}
|
2021-06-15 22:37:07 +02:00
|
|
|
|
2021-06-18 02:16:48 +02:00
|
|
|
// This formats language data for the language selection modal in a
|
|
|
|
// 2-column format.
|
2021-05-27 20:08:50 +02:00
|
|
|
type LanguageListColumn = {
|
|
|
|
[prop in "first" | "second"]?: {
|
|
|
|
code: string;
|
|
|
|
name: string;
|
|
|
|
name_with_percent: string;
|
|
|
|
selected: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export function get_language_list_columns(default_language: string): LanguageListColumn[] {
|
|
|
|
const formatted_list: LanguageListColumn[] = [];
|
2021-06-15 22:37:07 +02:00
|
|
|
const language_len = language_list.length;
|
|
|
|
const firsts_end = Math.floor(language_len / 2) + (language_len % 2);
|
|
|
|
const firsts = _.range(0, firsts_end);
|
|
|
|
const seconds = _.range(firsts_end, language_len);
|
2021-05-27 20:08:50 +02:00
|
|
|
const longest_zip: [number, number][] = [];
|
2021-06-15 22:37:07 +02:00
|
|
|
|
|
|
|
// Create a zip (itertool.zip_longest in python)
|
|
|
|
for (const value of firsts) {
|
|
|
|
longest_zip.push([value, seconds[value]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const row of longest_zip) {
|
2021-05-27 20:08:50 +02:00
|
|
|
const item: LanguageListColumn = {};
|
2021-06-15 22:37:07 +02:00
|
|
|
const zip_row = [
|
|
|
|
["first", row[0]],
|
|
|
|
["second", row[1]],
|
2021-05-27 20:08:50 +02:00
|
|
|
] as const;
|
2021-06-15 22:37:07 +02:00
|
|
|
for (const zip_value of zip_row) {
|
|
|
|
if (zip_value[1] !== undefined) {
|
|
|
|
const lang = language_list[zip_value[1]];
|
|
|
|
const name = lang.name;
|
|
|
|
let name_with_percent = name;
|
|
|
|
if (lang.percent_translated !== undefined) {
|
2021-09-22 23:34:58 +02:00
|
|
|
name_with_percent = `${name} (${lang.percent_translated}%)`;
|
2021-06-15 22:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let selected = false;
|
|
|
|
|
|
|
|
if (default_language === lang.code || default_language === lang.locale) {
|
|
|
|
selected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
item[zip_value[0]] = {
|
|
|
|
name,
|
|
|
|
code: lang.code,
|
|
|
|
name_with_percent,
|
|
|
|
selected,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted_list.push(item);
|
|
|
|
}
|
|
|
|
return formatted_list;
|
|
|
|
}
|