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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trans context handled differently from t function #1729

Closed
stefan-schweiger opened this issue Mar 6, 2024 · 10 comments
Closed

Trans context handled differently from t function #1729

stefan-schweiger opened this issue Mar 6, 2024 · 10 comments

Comments

@stefan-schweiger
Copy link

🐛 Bug Report

When using a translation with a context and a "fallback" (= key without context) the results for using t('key', { context: 'non-existing-context' ) and <Trans i18nKey="key" context="non-existing-context" />.

Here I'm using undefined and 'asdf' as the context values:

image

If used like this <Trans i18nKey="key" values={{ context: 'non-existing-context' }} /> the resulting output matches the one from t(...).

To Reproduce

https://stackblitz.com/edit/vitejs-vite-dfveac?file=src%2FApp.tsx&terminal=dev

export const resources = {
  en: {
    translation: {
      testContext1: 'EN: {{context}}',
      testContext1_Test1: 'EN: Context1 Test1',
      testContext1_Test2: 'EN: Context1 Test2',
      // ...
    },
  },
};

i18n.use(initReactI18next).init({
  resources,
  fallbackLng: 'en',
  interpolation: {
    escapeValue: false, // react already safes from xss
  },
});
<tr>
  <td>{t('testContext1', { context: undefined })}</td>
  <td>
    <Trans i18nKey="testContext1" context={undefined} />
  </td>
</tr>
<tr>
  <td>{t('testContext1', { context: 'asdf' })}</td>
  <td>
    <Trans i18nKey="testContext1" context={'asdf'} />
  </td>
</tr>

Expected behavior

<Trans context="..." /> should work the same as t('asdf', { context: 'asdf' }) as described in the doc.

@adrai adrai closed this as completed in 9684fab Mar 6, 2024
@adrai
Copy link
Member

adrai commented Mar 6, 2024

should be addressed with v14.0.6

@stefan-schweiger
Copy link
Author

wow, thank you for the fast response!

@adrai
Copy link
Member

adrai commented Mar 6, 2024

If you like this module don’t 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 🙏

@stefan-schweiger
Copy link
Author

stefan-schweiger commented Mar 6, 2024

I just tried to update my reproduction to 14.0.6 but I still get a different output when I use the context property of Trans :(

https://stackblitz.com/edit/vitejs-vite-9ntmun?file=package.json&terminal=dev

image

@adrai
Copy link
Member

adrai commented Mar 6, 2024

This test is green: https://github.com/i18next/react-i18next/blob/master/test/trans.render.spec.jsx#L880
Can you try to setup a failing test here?

@adrai
Copy link
Member

adrai commented Mar 6, 2024

ahh I know why....

@adrai
Copy link
Member

adrai commented Mar 6, 2024

That's because of the override of the prefix and suffix here: https://github.com/i18next/react-i18next/blob/master/src/TransWithoutContext.js#L357 but I don't remember why this was done like that... maybe @jamuhl remembers why?
Beside that, I can only say that normally the context interpolation for Trans component should by done with tags and children (<1></1>) and not like with the t function with {{context}}...

@adrai
Copy link
Member

adrai commented Mar 6, 2024

fyi: I locally just removed to pass the interpolationOverride object to... and all tests are still green... so it may be we can simplify that to no have the interpolation option override? @jamuhl ?

@adrai
Copy link
Member

adrai commented Mar 6, 2024

I just released v14.0.7 that removes the internal interpolationOverride handling...
if the community opens new issues because of that, we will probably revert this change

@stefan-schweiger
Copy link
Author

Seems to work now, thanks! :)

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

No branches or pull requests

2 participants