anki/ts/lib/i18n_helpers.ts

105 lines
3.0 KiB
TypeScript
Raw Normal View History

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
// 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";
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
}
}
}
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
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}`;
}
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";
}
}
weekdayLabel(n: number): string {
const firstLang = this.bundles[0].locales[0];
const now = new Date();
const daysFromToday = -now.getDay() + n;
const desiredDay = new Date(now.getTime() + daysFromToday * 86_400_000);
return desiredDay.toLocaleDateString(firstLang, {
weekday: "narrow",
});
}
/// 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
}
// global singleton
export const i18n = new I18n();
2020-06-27 11:24:49 +02: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();
i18n.bundles = [];
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
}