Skip to content

Conversation

otomad
Copy link
Contributor

@otomad otomad commented Jun 30, 2025

Close #2323 .

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • commit message and code follows the Developer's Certification of Origin

Feature

Use formatting functions with interpolation and plurals in the nesting "$t" function.

Example:

$t(ns:key, { count: {{count}}, arg1: "{{arg1}}", arg2: "{{arg2}}" }, formatter1, formatter2)

Note: you should not put formatters before the nesting option object. Because this is not only illogical, but also increases the complexity of the code. For example, the following code will not work properly.

$t(ns:key, formatter1, formatter2, { "count": {{count}}, "arg1": "{{arg1}}", "arg2": "{{arg2}}" })
$t(ns:key, formatter1, { "count": {{count}}, "arg1": "{{arg1}}", "arg2": "{{arg2}}" }, formatter2)

Additional Info

doReduce

let doReduce = false;

if (doReduce) {

Here is a variable doReduce, but I don't understand what it does, so I replaced it with formatters.length.

nestingRegexp

I found that the part that determines the $t() function currently uses a simple regular expression to match. Ignoring custom configuration, its default value is /\$t\((.+?)\)/. This means that if you include a right parenthesis inside the function, it will break the function from working properly. For example:

$t(key, { "name": "foo(bar)baz" })

This will break the function. However, since this part is beyond the scope of the target issue, I will not fixed it yet. I'm just giving a heads up.

@coveralls
Copy link

coveralls commented Jun 30, 2025

Coverage Status

coverage: 95.212% (+0.008%) from 95.204%
when pulling a4815c3 on otomad:master
into a7f7d06 on i18next:master.

@adrai
Copy link
Member

adrai commented Jun 30, 2025

Currently reviewing your PR....

I found that the part that determines the $t() function currently uses a simple regular expression to match. Ignoring custom configuration, its default value is /\$\((.+?)\)/. This means that if you include a right parenthesis inside the function, it will break the function from working properly. For example:

$t(key, { "name": "foo(bar)baz" })

This will break the function. However, since this part is beyond the scope of the target issue, I will not fixed it yet. I'm just giving a heads up.

Can you create a new PR for this if you see a nice way to improve it?

@adrai adrai merged commit dd694f2 into i18next:master Jun 30, 2025
10 checks passed
@adrai
Copy link
Member

adrai commented Jun 30, 2025

included in v25.3.0.... thank you for your contribution.

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.

Use formatting functions with interpolation and plurals in the nesting "t" function
3 participants