Files
openchamber-android-native/third_party/mygo/scripts/plurals-golden.ts
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

80 lines
3.6 KiB
TypeScript

// Writes i18n/testdata/plurals_icu.json: the plural categories ICU
// (Intl.PluralRules) gives numbers in every language of CLDR's plural rules,
// cardinal and ordinal, which TestPluralsMatchICU checks package i18n's
// compiled rules against. Run it again after updating CLDR, with a Node.js
// whose ICU has the same CLDR (`node -p process.versions.cldr`):
//
// node scripts/plurals-golden.ts
//
// Node.js, not Bun: Bun on macOS uses the system's ICU, whose data Apple
// changes (it gives bg, af and es ordinal categories CLDR does not have).
//
// The languages are those of i18n/testdata/plural_samples.txt, which
// `go generate ./i18n` writes; the numbers are integers and decimals
// written with zero to three fraction digits, whose trailing zeros count.
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
const dir = join(import.meta.dirname, "..", "i18n", "testdata");
/** The numbers, as written: their fraction digits are visible ones. */
export const numbers: string[] = [];
for (let i = 0; i <= 120; i++) numbers.push(String(i));
for (const n of [
200, 201, 202, 203, 205, 211, 212, 1000, 1001, 1002, 1011, 1100, 10000, 20000, 40000, 100000, 100001,
1000000, 1000001, 2000000, 10000000, 100000000, 1000000000,
]) {
numbers.push(String(n));
}
for (let i = 0; i <= 30; i++) numbers.push((i / 10).toFixed(1));
for (const s of ["0.00", "0.01", "0.05", "0.10", "0.11", "0.25", "1.00", "1.01", "1.10", "1.25", "1.50", "2.00",
"2.05", "3.00", "5.30", "10.10", "11.00", "21.00", "101.01", "0.000", "1.000", "1.005", "2.500", "1000000.0"]) {
numbers.push(s);
}
const initials: Record<string, string> = { zero: "z", one: "o", two: "t", few: "f", many: "m", other: "x" };
const samples = await readFile(join(dir, "plural_samples.txt"), "utf8");
const langs = { cardinal: new Set<string>(), ordinal: new Set<string>() };
for (const line of samples.split("\n")) {
if (line === "" || line.startsWith("#")) continue;
const [kind, list] = line.split(" ") as [keyof typeof langs, string];
for (const l of list.split(",")) langs[kind].add(l);
}
const out: Record<string, Record<string, string>> = { cardinal: {}, ordinal: {} };
for (const kind of ["cardinal", "ordinal"] as const) {
for (const lang of [...langs[kind]].sort()) {
// Languages ICU does not have would get the root's rules.
if (Intl.PluralRules.supportedLocalesOf([lang]).length === 0) continue;
const rules = new Map<number, Intl.PluralRules>();
let cats = "";
for (const n of numbers) {
const v = n.includes(".") ? n.split(".")[1]!.length : 0;
let pr = rules.get(v);
if (!pr) {
pr = new Intl.PluralRules(lang, {
type: kind,
minimumFractionDigits: v,
maximumFractionDigits: v,
});
rules.set(v, pr);
}
cats += initials[pr.select(Number(n))];
}
out[kind]![lang] = cats;
}
}
const file = join(dir, "plurals_icu.json");
const versions = process.versions as Record<string, string | undefined>;
const icu = `${versions.icu ?? "unknown"} (CLDR ${versions.cldr ?? "unknown"})`;
// A language a line, the numbers on one.
const map = (m: Record<string, string>) =>
"{\n" + Object.entries(m).map(([k, v]) => ` ${JSON.stringify(k)}: ${JSON.stringify(v)}`).join(",\n") + "\n }";
await writeFile(
file,
`{\n "icu": ${JSON.stringify(icu)},\n "numbers": ${JSON.stringify(numbers)},\n "cardinal": ${map(out.cardinal!)},\n "ordinal": ${map(out.ordinal!)}\n}\n`,
);
console.log(`wrote ${file}: ICU ${icu}, ${Object.keys(out.cardinal!).length} cardinal and ${Object.keys(out.ordinal!).length} ordinal languages`);