Skip to content

fix(i18n): resolve plugin-pack plurals with the pack's declared locale - #12505

Merged
nwparker merged 1 commit into
stablyai:mainfrom
smwbev:fix/plugin-pack-plural-rules
Oct 27, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
smwbev:fix/plugin-pack-plural-rules

Conversation

@smwbev

@smwbev smwbev commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Words like "1 file" and "5 files" change form with the number, and every language has its own rules for this. Russian, for example, uses different words for 2 and for 5. Plugin language packs are stored under a made-up internal name, so the app couldn't tell which language's rules to apply and quietly used English rules. Now the app reads the language the pack says it is and uses that language's rules.

What Changed

Before: a plugin pack registers its catalog under a synthetic resource language (plugin<hex> from pluginLanguageResourceId()). i18next can't build Intl.PluralRules from that tag, so it falls back to a two-form English rule. For a ru-RU pack, counts [1, 2, 5, 21] resolved to one, other, other, other, so the pack's _few/_many keys could never be reached. That fallback also skips i18next's rule cache, so every lookup threw and caught a RangeError.

After: the same counts resolve to one, few, many, one.

How it works (src/renderer/src/i18n/i18n.ts):

  • pluralResolver.getRule is wrapped once, when the module loads. The wrapper maps a pack's resource language to the pack's declared locale before i18next looks up the rule. Resource lookup still uses the synthetic tag, and the built-in en/es/ja/ko/zh locales go through unchanged.
  • setRendererPluginLanguagePacks rebuilds that map each time the packs change. The declared locale goes through Intl.PluralRules.supportedLocalesOf, and a locale ICU doesn't support maps to en. That gives the same one/other result as today, but now it is cached. The rule cache is keyed by the real locale, so it never needs clearing.
  • Packs translated from English often ship only _one/_other. Without extra handling, few/many counts would fall through to English. The registered copy of each catalog fills any missing <base>_<category> (from the locale's pluralCategories) with that key's _other text, so the text stays in the pack's language. Keys the pack ships are never overwritten, and the input catalog is not mutated.
  • supportedIntlLocale() is shared by getIntlLocale (DateTimeFormat) and the plural map (PluralRules).
  • isCatalogObject is now exported from plugin-language-pack-artifact.ts so the fill reuses it instead of reimplementing it.

Why

Maintainers chose option (1) in #12106: route plural selection through the locale the pack declares. No catalog content can fix this while selection runs on the synthetic tag. This approach changes only the code passed to the resolver, so i18next still owns how plural forms are selected, and every call site that passes count gets the fix without changes. plugin-pack-plurals.test.ts guards the dependency on i18next internals across upgrades.

Tradeoffs:

  • A pack that ships fewer categories than its locale needs shows its _other text for the missing forms. The text stays in the pack's language but may be the wrong grammatical form.
  • A pack that has _other but no _one now shows its _other text for count 1 instead of the English _one.

Linked Issue

Part of #12106. This fixes cause 2 (plugin packs never reach CLDR rules). Cause 1, the call sites that pick a form with === 1 ? … : …, is a separate follow-up.

Visual Proof

N/A. There is no new UI. The change is in plural-form selection for plugin language packs, and tests verify it directly. On origin/main's sources, both committed tests and a wider probe fail: 7 of 9 tests covering Russian nested keys, Arabic zero/two/few/many, switching locales, ordinals and _other-only packs. All 9 pass on this branch.

Testing

  • Reproduced on origin/main: a ru-RU pack with k_one/k_few/k_many/k_other resolves counts [1,2,5,21] to one, other, other, other. On this branch it resolves to one, few, many, one.

  • pnpm test src/renderer/src/i18n src/shared/plugins/plugin-language-pack-artifact: everything passes except language-detection-reconciliation.test.tsx. That file hits a 60s beforeEach timeout and fails the same way on a clean origin/main checkout under the same machine load.

  • oxlint, oxfmt --check, anti-slop oxlint, native code-quality oxlint, ast-grep, and check-changed-code-quality.mjs are clean on the touched files.

  • Tested on macOS. The change is pure JS (Intl plus a Map) with no paths, IPC, or platform APIs. It touches only the renderer, so there is no SSH/remote, mobile, or folder-workspace impact.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Notes

No security, cross-platform, remote SSH, mobile, or backwards-compatibility impact. The pack locale comes from an already-validated manifest and is only passed to Intl. Performance improves because plugin-pack plural lookups no longer throw on every call.

Original fix by @smwbev in #12505.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

  • Last reviewed commit: 6e9e88961e1b204a0ca2e1e31c798acc5c7ace2f
  • Latest commit, not reviewed: 5d9be21

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
Walkthrough

Adds locale canonicalization for plugin declarations before Intl validation. Adds plural-locale mapping for synthetic plugin resource languages and applies it after resource registration. Tests cover fallback pluralization, locale normalization, invalid locale rejection, synthetic resource lookup, locale changes, and mapping removal.

Pre-merge checks: Passed 4
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly and concisely describes the primary change: correcting plural resolution for plugin packs using their declared locale.
Description check Passed The description is detailed and covers the change, motivation, linked issue, visual proof, testing, and compatibility notes. It omits the template's Review, AI Disclosure, Agent skill upstream boundar…
Full details: Description check

Explanation

The description is detailed and covers the change, motivation, linked issue, visual proof, testing, and compatibility notes. It omits the template's Review, AI Disclosure, Agent skill upstream boundary, and Checklist sections, but the core required information is present.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/renderer/src/i18n/plugin-plural-locale.ts (1)

4-18: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten the module comment.

Keep one concise comment that states why plural resolution uses the declared locale while resource lookup uses the synthetic language. The current block restates implementation details.

As per coding guidelines, comments must be concise, limited to non-obvious information, and preferably one line.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2f7335c8-82a8-4867-a51e-e310e85db824

📥 Commits

Reviewing files that changed from the base of the PR and between 2548b81 and b20e09c533df4e6ce883f5541aa7ecd327a3f263.

📒 Files selected for processing (3)
  • src/renderer/src/i18n/i18n.ts
  • src/renderer/src/i18n/plugin-plural-locale.test.ts
  • src/renderer/src/i18n/plugin-plural-locale.ts

@smwbev

smwbev commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed in 188ca64bb.

Canonicalizing alone turned out not to be enough. Intl.getCanonicalLocales() does reject malformed tags such as ru_RU or not_a_locale, but a well-formed yet unknown tag (xx, tlh) passes it, and Intl.PluralRules then silently falls back to the host locale — on my machine new Intl.PluralRules('xx').resolvedOptions().locale is ru-RU. A pack with a typo would pluralize by whatever language the user's computer runs, which its author could never reproduce.

So the declared tag is now canonicalized (underscores folded first, so a ru_RU slip still lands on ru-RU) and kept only when Intl.PluralRules.supportedLocalesOf() reports real data for it. Anything unresolvable leaves the lookup empty, so the resolver stays on the synthetic plugin<hex> tag and the pack degrades to i18next's default instead of borrowing the host's rules.

Tests cover both directions: ru_RU and RU-ru resolve to ru-RU, while not_a_locale, xx and tlh register no mapping and leave rendering on the default forms. Module comment shortened as suggested.

@smwbev
smwbev force-pushed the fix/plugin-pack-plural-rules branch 2 times, most recently from 188ca64 to 6e9e889 Compare August 4, 2026 11:47
smwbev added a commit to smwbev/orca that referenced this pull request Aug 4, 2026
The manifest accepts any string as a pack's locale, so a pack can declare
ru_RU, not_a_locale, or xx. Two silent failures follow. Intl rejects a
malformed tag outright, and for a well-formed but unknown one it falls back to
the host locale: the pack would then select plural forms by whichever language
the user's machine runs, which is impossible to reproduce for its author.

Canonicalize the declared tag before it reaches the resolver and keep it only
when Intl.PluralRules actually has data for it. A slip like ru_RU still lands
on ru-RU; anything unresolvable leaves the lookup empty, so the resolver stays
on the synthetic tag and the pack degrades to i18next's default instead of
borrowing the host's rules.

Addresses the review note on stablyai#12505.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/renderer/src/i18n/pack-locale.ts (1)

1-14: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Condense the locale rationale comments.

Keep the non-obvious rationale in one short comment at each site.

  • src/renderer/src/i18n/pack-locale.ts#L1-L14: replace the multi-paragraph block with one sentence that states canonicalization occurs before caller-specific Intl support checks.
  • src/renderer/src/i18n/intl-locale.test.ts#L1-L9: replace the multi-paragraph block with one sentence that states ICU support is stubbed for deterministic tests.

As per coding guidelines, comments must be concise, limited to non-obvious information, and preferably one line.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7a07260b-b824-4f64-b421-ffa85a191c0f

📥 Commits

Reviewing files that changed from the base of the PR and between 188ca64bb09f32d36999540ebe004f3982d3f7c3 and 6e9e88961e1b204a0ca2e1e31c798acc5c7ace2f.

📒 Files selected for processing (5)
  • src/renderer/src/i18n/i18n.ts
  • src/renderer/src/i18n/intl-locale.test.ts
  • src/renderer/src/i18n/pack-locale.ts
  • src/renderer/src/i18n/plugin-plural-locale.test.ts
  • src/renderer/src/i18n/plugin-plural-locale.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/renderer/src/i18n/i18n.ts
  • src/renderer/src/i18n/plugin-plural-locale.ts
  • src/renderer/src/i18n/plugin-plural-locale.test.ts

@smwbev

smwbev commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Scope note: while rebasing this branch onto current main I found that getIntlLocale() — which landed via #12514 — reads the same author-supplied field and trips over the same tags, so a pack declaring ru_RU silently formats its dates in English.

That fix and the shared canonicalizePackLocale() helper are now open separately as #12533, since the date bug is real regardless of whether this prototype is the approach you want. The top commit here is the same change; reviewing #12533 first leaves this PR as a two-file diff, and once it merges I'll rebase so the duplicate disappears.

@smwbev
smwbev force-pushed the fix/plugin-pack-plural-rules branch from 6e9e889 to e219481 Compare August 4, 2026 14:26
@smwbev

smwbev commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@nwparker — this and #12533 have no reviewer, and the reason looks like a gap worth fixing rather than something to ping around each time.

CODEOWNERS routes /src/renderer/src/i18n/locales/ and the localization scripts, but not /src/renderer/src/i18n/ itself. So a PR touching a catalog file gets auto-assigned, while a PR touching i18n.ts — the code that decides which catalog and which locale rules apply — gets nobody. That is the third time in a week: #12105 sat unassigned until it was raised manually, #12455 needed a direct request, and now these two.

If you agree the whole directory belongs to the same owner, one line does it:

/src/renderer/src/i18n/ @brennanb2025

Happy to send that as a one-line PR if you'd rather have it in the queue than in a comment.

On the two PRs themselves, both are green and the split is deliberate:

No rush on either — mainly flagging that the routing gap is what's keeping them invisible, not the content.

@smwbev

smwbev commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Heads-up on the red check here, since it is misleading: the PR Checks failure is an expiry, not a test failure.

Run 34408566433 was created 9 Sep 21:44 and concluded failure on 9 Oct 21:47 — exactly 30 days later, with zero jobs executed. It never ran; it sat awaiting the manual approval that fork PRs need, and GitHub expired it. The same thing has happened to every one of these branches, each at the 30-day mark:

PR run created expired jobs run
#12505 9 Sep 9 Oct 0
#12533 8 Aug 7 Sep 0
#14121 13 Aug 12 Sep 0
#14127 17 Aug 16 Sep 0

So the red mark carries no information about the code — but it does make these PRs look broken to anyone scanning the queue, which seemed worth stating explicitly rather than leaving to inference.

What I can confirm locally for this branch at b2b17c73, run on 9 Sep after rebasing onto then-current main:

  • vitest on intl-locale.test.ts + plugin-plural-locale.test.ts — 13 passed
  • pnpm typecheck — exit 0
  • pnpm lint — exit 0, including verify:localization-catalog, verify:localization-runtime-catalog, verify:localization-extraction and verify:localization-coverage

That rebase resolved one conflict: upstream replaced the eagerly bundled English catalog with en-runtime-required.json, so I took upstream's import and kept canonicalizePackLocale and applyPluginPluralLocales, both still used at their call sites. All four branches are MERGEABLE / mergeStateStatus: CLEAN as of today.

The one thing that would help: a single "Approve and run" on these four would let CI actually execute, and the checks would then say something real either way. If any of them is unwanted, closing it is just as useful to me — #13020 was closed as superseded by #13118 last month and that was the right call, no complaints from me.

smwbev added a commit to smwbev/orca-russian that referenced this pull request Oct 11, 2026
…e tools

Plural forms. The catalog now carries `_few` and `_many` for the 18
plural families whose form i18next picks from a `count`: 36 entries.
Orca resolves a pack's plurals with the two English forms today
(stablyai/orca#12106), so nothing changes on screen; an i18next probe
under the synthetic resource language gives the same output as before
for every family. Under ru-RU the forms read "сохранены 2 ветки",
"сохранено 5 веток", "сохранена 21 ветка", which is what users get the
day stablyai/orca#12505 lands. The nine families where the component
picks the suffixed key itself get no extra forms: nothing would read
them. One of those, the Warp theme import limit, had an `_other` that
was wrong for 21; it is now count-neutral like the rest.

Tools. The build, the release check and a pack audit move from an
untracked working directory into tools/. audit-pack.py is new and
works on any Orca language pack: engine verdict, coverage, stale keys,
placeholder drift, all against a release tag rather than main. The
build accepts a `_few`/`_many` form only while its family lives in
Orca's catalog; the string count and coverage leave those forms out.

READMEs, checked against the v1.4.224 UI code:
- the marketplace card is titled "Russian", from the plugin key, not
  "Русский";
- installing is not enabling: the Review plugin step was missing from
  all three install paths;
- sources are added through Manage sources, and updates are fetched
  with Check for update; Orca does not announce them;
- the catalog file is 1.4 MB, not 1 MB;
- the coverage figure is of what a pack may translate, not of the
  whole catalog;
- the Russian README names the buttons as they appear before the pack
  is active, and its term counts follow the release.

15,433 strings, unchanged, plus 36 extra plural forms.
Plugin language packs register under synthetic plugin<hex> resource
languages, so i18next's plural resolver fell back to English rules and
Russian/Polish/etc. packs never selected their _few/_many forms. Map each
pack's resource language to its declared (Intl-supported) locale inside
the plural resolver, and fill missing plural categories from the key's
_other form so packs that only ship _one/_other stay in-language.

Co-authored-by: Evgenii <[email protected]>
@nwparker
nwparker force-pushed the fix/plugin-pack-plural-rules branch from b9ecbaa to 5d9be21 Compare October 27, 2026 05:21
@nwparker nwparker changed the title fix(i18n): resolve plugin-pack plurals with the declared locale fix(i18n): resolve plugin-pack plurals with the pack's declared locale Oct 27, 2026
@nwparker
nwparker merged commit 8d72873 into stablyai:main Oct 27, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants