Repository navigation
feat(intl): let rich and markup take plural and match as the preformat - #398
Merged
Merged
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A plural message with tags could not be written.
rich(tags, plural('count'))andrich(tags, params({ count: number() }, plural('count')))rendered an empty message with anonymous translations and failed to type-check with typed ones: the preformat ofrichgot one text piece at a time, whilepluralandmatchchoose 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}, anduppercase(params(...))orwrap(params({ count: number() }, plural('count')), (message, values) => message(values))did not type-check.What
richandmarkuptakeplural,matchandparamsaround 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.paramsreachpluralandmatchthrough the format context instead of a second argument of the message function, so they pass throughwrap,markupand text transforms.uppercase,lowercaseandcapitalizetake a message function too and transform the text it renders.paramsfills it in keeps them.wrapreads back a param that bothparamsandplurallist as their common type.forms(...)andcases(...)take only formats that render text: a rich case fails to compile instead of throwing.richon the Intl API page with a plural example, a paragraph on text transforms of message functions, a point in thenano-kit-intlskill,DOCS.mdregenerated.Left as they are: a
paramswith a preformat inside anotherparamsstill hides the formats of the outer one (merging them costs 8 B more in the minimal set, and the formats fit into oneparams), and tags in a fallback of a format inside the preformat, likeforms({ 0: text('<b>No</b> plants') }), stay text.Size
{ 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 ofpluralinsidewrapandmarkup, a translation with transformed case.plural.spec.ts: uppercased forms.text.spec.ts: transforms of message functions.wrap.spec.ts: values of a param bothparamsandplurallist.match.spec.ts: a rich case fails to type-check. The 13 new runtime tests fail onmain.oxlint,tscandvitestare clean in@nano_kit/intl(189 tests),size-limitpasses andskills-docs --checkis clean.