Skip to content

feat(intl): let rich and markup take plural and match as the preformat - #398

Merged
dangreen merged 1 commit into
mainfrom
feat/intl-format-combinations
Nov 22, 2026
Merged

dangreen merged 1 commit into
mainfrom
feat/intl-format-combinations

Conversation

@dangreen

Copy link
Copy Markdown
Member

Why

A plural message with tags could not be written. rich(tags, plural('count')) and rich(tags, params({ count: number() }, plural('count'))) rendered an empty message with anonymous translations and failed to type-check with typed ones: the preformat of rich got one text piece at a time, while plural and match choose from the whole message.

A check of 92 combinations of formats turned up more that compiled but rendered wrong: plural('count', forms({ one: rich(tags) })) threw, params({ count: number() }, wrap(plural('count'), fn)) printed the raw count, params({ name: text() }, uppercase(text())) printed {NAME}, and uppercase(params(...)) or wrap(params({ count: number() }, plural('count')), (message, values) => message(values)) did not type-check.

What

  • rich and markup take plural, match and params around them as the preformat. The preformat gets the whole message and chooses the form, and only the tags of the translation map to chunks: they are marked before values go in, so a value is still never read as a tag. The input type comes from the preformat, so typed translations with forms fit.
  • The preformat runs once per call instead of once per text piece, so a rich message with values renders about 25% faster.
  • The formats of a surrounding params reach plural and match through the format context instead of a second argument of the message function, so they pass through wrap, markup and text transforms.
  • uppercase, lowercase and capitalize take a message function too and transform the text it renders.
  • Placeholders match regardless of case, so a translation transformed before params fills it in keeps them.
  • wrap reads back a param that both params and plural list as their common type.
  • forms(...) and cases(...) take only formats that render text: a rich case fails to compile instead of throwing.
  • Docs: rich on the Intl API page with a plural example, a paragraph on text transforms of message functions, a point in the nano-kit-intl skill, DOCS.md regenerated.

Left as they are: a params with a preformat inside another params still hides the formats of the outer one (merging them costs 8 B more in the minimal set, and the formats fit into one params), and tags in a fallback of a format inside the preformat, like forms({ 0: text('<b>No</b> plants') }), stay text.

Size

Entry Before After Limit
All publics (Gzip) 2451 B 2535 B 2.50 → 2.55 kB
All publics (Brotli) 2268 B 2344 B 2.30 → 2.35 kB
Minimal set (Gzip) 627 B 632 B 650 B
Minimal set (Brotli) 583 B 596 B 600 B
Basic set (Gzip) 1073 B 1082 B 1.10 kB
Basic set (Brotli) 995 B 1008 B 1.00 → 1.05 kB

{ intl, rich, params, plural } grows by 71 B gzip, { intl, text, uppercase, lowercase, capitalize } by 23 B.

Tests

rich.spec.ts: a plural form and a case chosen by the preformat, a formatted count, nested cases with plural forms, tags in values of a plural form, a fallback message, a missing form. markup.spec.ts: a plural form. params.spec.ts: the value of plural inside wrap and markup, a translation with transformed case. plural.spec.ts: uppercased forms. text.spec.ts: transforms of message functions. wrap.spec.ts: values of a param both params and plural list. match.spec.ts: a rich case fails to type-check. The 13 new runtime tests fail on main. oxlint, tsc and vitest are clean in @nano_kit/intl (189 tests), size-limit passes and skills-docs --check is clean.

…preformat

`rich(tags, plural('count'))` and `rich(tags, params({ count: number() }, plural('count')))` rendered nothing: the preformat got one text piece at a time, while `plural` and `match` choose from the whole message. The preformat of `rich` and `markup` now gets the whole message, and the tags of the translation are marked before values go in, so a value is still never read as a tag.

Other combinations of formats that compiled but rendered wrong now work or fail to compile:

- `uppercase`, `lowercase` and `capitalize` transform the text of a message function, like `params(...)`.
- A surrounding `params` formats the value of `plural` and `match` inside `wrap`, `markup` and text transforms too.
- Placeholders match regardless of case, so a translation transformed by `uppercase(text())` keeps them.
- `wrap` takes back the values of a param that both `params` and `plural` list.
- `forms(...)` and `cases(...)` take only formats that render text, so tags go around `plural` and `match`.
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.81%. Comparing base (acd2817) to head (f2c23b5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #398      +/-   ##
==========================================
+ Coverage   85.78%   85.81%   +0.03%     
==========================================
  Files         242      242              
  Lines        5155     5173      +18     
  Branches     1131     1134       +3     
==========================================
+ Hits         4422     4439      +17     
- Misses        514      515       +1     
  Partials      219      219              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dangreen
dangreen merged commit af500ff into main Nov 22, 2026
11 checks passed
@dangreen
dangreen deleted the feat/intl-format-combinations branch November 22, 2026 16:04
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
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.

1 participant