Skip to content

@wxt-dev/i18n: {0, n} plurals swap counts, nested and flat keys collide, and invalid message names break Chrome loading #2747

Open

Description

@trackoor

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:

  1. Plural forms are matched by part count rather than key. PluralMessage is typed as Record<'n' | number, string> and isPluralMessage accepts any finite numeric keys, generateChromeMessages serializes plurals as Object.values(message.plurals).join(' | ') (dropping the keys), and t() selects a branch solely from plural.length (2 parts treated as [1, n], 3 parts treated as [0, 1, n]). When a message defines only 0 and n (for example UI strings like 0: None selected / n: $1 selected, or locales without singular/plural distinction such as Japanese or Chinese), it builds without error, and at runtime t('items', 0) returns "0 items" while t('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 throw Unknown plural formatting; here 2- and 3-key plurals parse and silently select the wrong branch.)

  2. Nested and flat keys silently overwrite each other in messages.json. generateTypeText joins key segments with . (message.key.join('.')), emitting both "a.b" and "a_b" in GeneratedI18nStructure, whereas generateChromeMessages joins segments with _ (message.key.join('_')) and overwrites acc['a_b'] without warning. At runtime key.replaceAll('.', '_') makes t('a.b') return the a_b string (or vice versa depending on key order).

  3. 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 @ in messages.json message names (https://developer.chrome.com/docs/extensions/how-to/ui/localization-message-formats#name). Keys such as sign-in: Sign in or quoted "a.b": text are emitted verbatim into both GeneratedI18nStructure and _locales/*/messages.json, and Chromium then refuses to load the unpacked extension with Failed to load extension ... Name of a key "sign-in" is invalid. Only ASCII [a-z], [A-Z], [0-9] and "_" are allowed.

Reproduction

https://gist.github.com/trackoor/91e4260787f098bb4cc0385dbb80237b

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" and t("items", 1) returns "1 items" (or unsupported plural key sets fail at build time instead of swapping counts).
  • Colliding keys (a.b vs a_b) and invalid Chrome message names (sign-in) fail at build time rather than silently overwriting translations or producing a _locales/*/messages.json that 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

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions