Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected interpolation within variables #1487

Closed
rynti opened this issue Jul 15, 2020 · 3 comments 路 Fixed by #1488
Closed

Unexpected interpolation within variables #1487

rynti opened this issue Jul 15, 2020 · 3 comments 路 Fixed by #1488

Comments

@rynti
Copy link

rynti commented Jul 15, 2020

馃悰 Bug Report

During interpolation of variables, i18next also performs interpolation within the contents of the variables. I would not expect the variables to also be affected by interpolation, as the contents of them might not be from a trusted source (user input). At least there should be an option to disable this (which in my opinion should be the default in a future version).

I don't really understand why it would be desirable to use variables within variables, as developers can just inline the variables themselves instead of relying on i18next to derefence them.

An additional side effect of the interpolation within variables (and this side effect actually led me to find out about this issue) is that it makes it impossible to render double curly braces in a translated text. I have my own template engine in my application that also uses double braces for placeholders, and I wanted an introductory paragraph to explain how the user can use the placeholders, which required me to inline double braces within the text. But I did not find any way documented on how the double braces could be escaped, so I tried adding them through a variable, only to realize that the contents of the variable will also have interpolation applied (which leads to the removal of the "placeholder").

To Reproduce

See this codesandbox.

Or to reproduce:

  1. Add a translation like this: example: "Example Placeholder: {{examplePlaceholder}}"
  2. Invoke the translation like so: const result = t("example", { examplePlaceholder: "{{ThisShouldNotGetParsed}}" })

Expected behavior

I would expect the result to contain the string: "Example Placeholder: {{ThisShouldNotGetParsed}}". However, the current i18next implementation would result in a string like this: "Example Placeholder: " (Because it tries to resolve {{ThisShouldNotGetParsed}} but is unable to, as there is no such variable. It even prints a warning in debug mode saying that the variable ThisShouldNotGetParsed is not set)

I also enabled the new skipOnVariables option as well as disabling escaping of values (with escapeValue: false in the interpolation options). Both did not change the result.

Your Environment

  • runtime version: node v12
  • i18next version: 19.6.0
  • os: Windows
@adrai adrai mentioned this issue Jul 15, 2020
4 tasks
adrai added a commit that referenced this issue Jul 15, 2020
* extend usage of skipOnVariables, should fix #1487
@adrai
Copy link
Member

adrai commented Jul 15, 2020

thank you, fixed with skipOnVariables in v19.6.1

@adrai
Copy link
Member

adrai commented Jul 15, 2020

If you like this module don鈥檛 forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project.

There are many ways to help this project 馃檹

@rynti
Copy link
Author

rynti commented Jul 16, 2020

Thank you for the very quick fix! Unfortunately now I encountered another issue which is kind of related, at #1490.

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 a pull request may close this issue.

2 participants