2020-06-27 11:24:49 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-03-26 11:23:43 +01:00
|
|
|
// An i18n singleton and setupI18n is re-exported via the generated i18n.ts file,
|
|
|
|
// so you should not need to access this file directly.
|
|
|
|
|
2020-06-29 05:46:13 +02:00
|
|
|
import "intl-pluralrules";
|
2021-09-30 11:18:10 +02:00
|
|
|
import { FluentBundle, FluentResource, FluentNumber } from "@fluent/bundle";
|
2020-06-27 11:24:49 +02:00
|
|
|
|
2020-06-27 13:10:17 +02:00
|
|
|
type RecordVal = number | string | FluentNumber;
|
|
|
|
|
|
|
|
function formatNumbers(args?: Record<string, RecordVal>): void {
|
2020-06-27 11:24:49 +02:00
|
|
|
if (!args) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const key of Object.keys(args)) {
|
|
|
|
if (typeof args[key] === "number") {
|
2020-06-27 13:10:17 +02:00
|
|
|
args[key] = new FluentNumber(args[key] as number, {
|
2020-06-28 11:34:19 +02:00
|
|
|
maximumFractionDigits: 2,
|
2020-06-27 13:10:17 +02:00
|
|
|
});
|
2020-06-27 11:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:23:43 +01:00
|
|
|
export class I18n {
|
2020-06-27 11:24:49 +02:00
|
|
|
bundles: FluentBundle[] = [];
|
2020-06-30 07:09:20 +02:00
|
|
|
langs: string[] = [];
|
2020-06-27 11:24:49 +02:00
|
|
|
|
2021-03-26 11:23:43 +01:00
|
|
|
translate(key: string, args?: Record<string, RecordVal>): string {
|
2020-06-27 11:24:49 +02:00
|
|
|
formatNumbers(args);
|
|
|
|
for (const bundle of this.bundles) {
|
|
|
|
const msg = bundle.getMessage(key);
|
|
|
|
if (msg && msg.value) {
|
|
|
|
return bundle.formatPattern(msg.value, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `missing key: ${key}`;
|
|
|
|
}
|
|
|
|
|
2020-06-28 07:23:36 +02:00
|
|
|
supportsVerticalText(): boolean {
|
|
|
|
const firstLang = this.bundles[0].locales[0];
|
|
|
|
return (
|
|
|
|
firstLang.startsWith("ja") ||
|
|
|
|
firstLang.startsWith("zh") ||
|
|
|
|
firstLang.startsWith("ko")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-04 06:28:46 +02:00
|
|
|
direction(): string {
|
|
|
|
const firstLang = this.bundles[0].locales[0];
|
|
|
|
if (
|
|
|
|
firstLang.startsWith("ar") ||
|
|
|
|
firstLang.startsWith("he") ||
|
|
|
|
firstLang.startsWith("fa")
|
|
|
|
) {
|
|
|
|
return "rtl";
|
|
|
|
} else {
|
|
|
|
return "ltr";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 19:31:47 +01:00
|
|
|
weekdayLabel(n: number): string {
|
|
|
|
const firstLang = this.bundles[0].locales[0];
|
2021-08-24 04:17:35 +02:00
|
|
|
const now = new Date();
|
|
|
|
const daysFromToday = -now.getDay() + n;
|
|
|
|
const desiredDay = new Date(now.getTime() + daysFromToday * 86_400_000);
|
|
|
|
return desiredDay.toLocaleDateString(firstLang, {
|
2021-01-21 19:31:47 +01:00
|
|
|
weekday: "narrow",
|
|
|
|
});
|
|
|
|
}
|
2021-05-26 06:41:18 +02:00
|
|
|
|
|
|
|
/// Treat text like HTML, merging multiple spaces and converting
|
|
|
|
/// newlines to spaces.
|
|
|
|
withCollapsedWhitespace(s: string): string {
|
|
|
|
return s.replace(/\s+/g, " ");
|
|
|
|
}
|
2020-06-27 11:24:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 11:23:43 +01:00
|
|
|
// global singleton
|
|
|
|
export const i18n = new I18n();
|
2020-06-27 11:24:49 +02:00
|
|
|
|
2021-03-26 12:22:37 +01:00
|
|
|
import type { ModuleName } from "./i18n";
|
|
|
|
|
|
|
|
export async function setupI18n(args: { modules: ModuleName[] }): Promise<void> {
|
|
|
|
const resp = await fetch("/_anki/i18nResources", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(args),
|
|
|
|
});
|
2020-06-27 11:24:49 +02:00
|
|
|
if (!resp.ok) {
|
|
|
|
throw Error(`unexpected reply: ${resp.statusText}`);
|
|
|
|
}
|
|
|
|
const json = await resp.json();
|
|
|
|
|
2021-03-26 11:23:43 +01:00
|
|
|
i18n.bundles = [];
|
2020-12-14 05:23:49 +01:00
|
|
|
for (const i in json.resources) {
|
|
|
|
const text = json.resources[i];
|
|
|
|
const lang = json.langs[i];
|
|
|
|
const bundle = new FluentBundle([lang, "en-US"]);
|
|
|
|
const resource = new FluentResource(text);
|
2020-06-27 11:24:49 +02:00
|
|
|
bundle.addResource(resource);
|
|
|
|
i18n.bundles.push(bundle);
|
|
|
|
}
|
2020-06-30 07:09:20 +02:00
|
|
|
i18n.langs = json.langs;
|
2020-06-27 11:24:49 +02:00
|
|
|
}
|