Skip to content

fix(extract): quote adjacent literal braces as one ICU quoted text - #340

Merged
adrai merged 1 commit into
i18next:mainfrom
zzoraemon043:fix/icu-adjacent-braces
Nov 27, 2026
Merged

adrai merged 1 commit into
i18next:mainfrom
zzoraemon043:fix/icu-adjacent-braces

Conversation

@zzoraemon043

Copy link
Copy Markdown
Contributor

With extract.messageFormat: 'icu', toMessageFormatPlaceholders quotes each literal brace on its own. Two braces next to each other come out as '{''}', and ICU reads the '' between them as an apostrophe inside one quoted text:

text written rendered
Use {} for an empty object Use '{''}' for an empty object Use {'} for an empty object
a}{b a'}''{'b a}'{b
{'} '{''''}' {''}
{'x {{name}} '{''x {name} {'x {name} (the placeholder is text)

The last one is an apostrophe right after a quoted brace: it is not doubled, so the quote stays open over the placeholder.

The fix quotes a run of braces together with the apostrophes next to it as one quoted text, with the apostrophes doubled inside. Other apostrophes are doubled as before.

Checked by parsing the output with @formatjs/icu-messageformat-parser for 30,000 random strings of a, space, ', {, }, <, >, <1>, </1> and placeholders: about 10% did not read back as the original text before, none after. Outputs that already read back correctly stay the same, apart from a closing quote now written after a trailing apostrophe ('}''' instead of '}'', same text).

The new test fails on main and passes with the fix; npm test (lint and 1713 tests) and tsc --noEmit pass.

Quoting each brace on its own turned {} into '{''}', which ICU reads as {'}
since '' inside quoted text is an apostrophe. An apostrophe right after a
quoted brace also kept the quote open over a following placeholder.
@adrai
adrai merged commit d754618 into i18next:main Nov 27, 2026
11 checks passed
adrai added a commit that referenced this pull request Nov 27, 2026
@adrai

adrai commented Nov 27, 2026

Copy link
Copy Markdown
Member

Thanks a lot for this one, and for the parser round-trip check. I could reproduce it with a <Trans> containing {'{}'}. Included in v1.95.4.

One note for next time: we now ask for an issue first, and once a maintainer has confirmed it, a PR is very welcome.

@zzoraemon043

Copy link
Copy Markdown
Contributor Author

Thanks. Will open an issue first next time.

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.

2 participants