Repository navigation
Description
Describe the bug
In @wxt-dev/i18n (packages/i18n/src/build.ts and packages/i18n/src/index.ts), three issues occur when compiling locale files into _locales/*/messages.json and GeneratedI18nStructure:
-
Plural forms are matched by part count rather than key.
PluralMessageis typed asRecord<'n' | number, string>andisPluralMessageaccepts any finite numeric keys,generateChromeMessagesserializes plurals asObject.values(message.plurals).join(' | ')(dropping the keys), andt()selects a branch solely fromplural.length(2parts treated as[1, n],3parts treated as[0, 1, n]). When a message defines only0andn(for example UI strings like0: None selected/n: $1 selected, or locales without singular/plural distinction such as Japanese or Chinese), it builds without error, and at runtimet('items', 0)returns"0 items"whilet('items', 1)returns"No items". Likewise{1, 2, n}is read as{0, 1, n}. (i18n - support defining plural counts dynamically #1848 requests dynamic plural counts where >3 keys throwUnknown plural formatting; here 2- and 3-key plurals parse and silently select the wrong branch.) -
Nested and flat keys silently overwrite each other in
messages.json.generateTypeTextjoins key segments with.(message.key.join('.')), emitting both"a.b"and"a_b"inGeneratedI18nStructure, whereasgenerateChromeMessagesjoins segments with_(message.key.join('_')) and overwritesacc['a_b']without warning. At runtimekey.replaceAll('.', '_')makest('a.b')return thea_bstring (or vice versa depending on key order). -
Keys outside Chrome's valid message-name character set pass type generation and build, then fail extension loading. Chrome only allows
A-Z,a-z,0-9,_, and@inmessages.jsonmessage names (https://developer.chrome.com/docs/extensions/how-to/ui/localization-message-formats#name). Keys such assign-in: Sign inor quoted"a.b": textare emitted verbatim into bothGeneratedI18nStructureand_locales/*/messages.json, and Chromium then refuses to load the unpacked extension withFailed to load extension ... Name of a key "sign-in" is invalid. Only ASCII [a-z], [A-Z], [0-9] and "_" are allowed.
Reproduction
Steps to reproduce
Run npm install @wxt-dev/[email protected] && node wxt-tslib.repro.mjs with:
import {
parseMessagesText,
generateChromeMessages,
generateTypeText,
} from "@wxt-dev/i18n/build";
const parsed = parseMessagesText(
`
items:
0: No items
n: $1 items
a:
b: nested
a_b: flat
sign-in: Sign in
`,
"YAML",
);
const msgs = generateChromeMessages(parsed);
console.log("messages.json:", JSON.stringify(msgs));
console.log(
"types:\n" + generateTypeText(parsed).split("\n").slice(1, 5).join("\n"),
);
// Stand-in for browser.i18n.getMessage with $1 substitution
globalThis.chrome = globalThis.browser = {
runtime: { id: "x" },
i18n: {
getMessage: (k, s = []) =>
(msgs[k]?.message ?? "").replace(/\$(\d)/g, (_, d) => s[d - 1] ?? ""),
},
};
const { createI18n } = await import("@wxt-dev/i18n");
const i18n = createI18n();
console.log('t("items", 0):', JSON.stringify(i18n.t("items", 0)));
console.log('t("items", 1):', JSON.stringify(i18n.t("items", 1)));
console.log('t("a.b"):', JSON.stringify(i18n.t("a.b")));Actual output:
messages.json: {"items":{"message":"No items | $1 items"},"a_b":{"message":"flat"},"sign-in":{"message":"Sign in"}}
types:
"items": { substitutions: 1, plural: true };
"a.b": { substitutions: 0, plural: false };
"a_b": { substitutions: 0, plural: false };
"sign-in": { substitutions: 0, plural: false };
t("items", 0): "0 items"
t("items", 1): "No items"
t("a.b"): "flat"
Expected:
t("items", 0)returns"No items"andt("items", 1)returns"1 items"(or unsupported plural key sets fail at build time instead of swapping counts).- Colliding keys (
a.bvsa_b) and invalid Chrome message names (sign-in) fail at build time rather than silently overwriting translations or producing a_locales/*/messages.jsonthat Chrome refuses to load.
System Info
System:
OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
Memory: 199.48 GB / 251.88 GB
Container: Yes
Shell: 5.2.15 - /bin/bash
Binaries:
Node: 26.13.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/node
npm: 11.20.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/npm
pnpm: 12.18.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/pnpm
Browsers:
Chromium: 156.0.8078.79
npmPackages:
@wxt-dev/i18n: ^0.2.8 => 0.2.8
wxt: ^0.21.5 => 0.21.5
Also reproduced on main at 81721cc.Used Package Manager
npm
Validations
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Credit: https://ucsbplse.github.io/
Activity