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

LocaleStructure not compatibility single brackets for namespace #1090

Closed
vechnii2007 opened this issue Mar 22, 2021 · 6 comments · Fixed by #1248
Closed

LocaleStructure not compatibility single brackets for namespace #1090

vechnii2007 opened this issue Mar 22, 2021 · 6 comments · Fixed by #1248

Comments

@vechnii2007
Copy link

Describe the bug

I faced localeStructure and interpolation problem
my json is for variables with single brackets.
like this
"test": "Why {appName}?",
and i used in code
{t("test", { appName: process.env.NEXT_PUBLIC_APP_NAME })}

in the config it was stated that

localeStructure: "{lng} / {ns}",
    interpolation: {
        prefix: "{",
        suffix: "}",
    },

now such an entry does not work, I looked in the code and found that in
node_modules/next-i18next/dist/commonjs/config/createConfig.js
you are doing a replacement
var defaultLocaleStructure = localeStructure.replace ('{{lng}}', lng) .replace ('{{ns}}', defaultNS);
If you change it to single brackets or add an entry, then everything will work.

Occurs in next-i18next version

8.1.2

Steps to reproduce

like this. Use single brackets in common.json
"test": "Why {appName}?",
and i used in code
{t("test", { appName: process.env.NEXT_PUBLIC_APP_NAME })}

Expected behaviour

Expected result, that i can use single or more brackets for localeStructure and interpolation

OS (please complete the following information)

MacOS
Chrome 89.0.4389.90

@isaachinman
Copy link
Contributor

isaachinman commented Apr 5, 2021

@vechnii2007 The localeStructure config option is completely separate to what character i18next uses for interpolation.

@zachguo
Copy link

zachguo commented May 14, 2021

I ran into the exact same issue.

A simple config like this would turn t(...) off across the site without warning or error.

module.exports = {
  i18n: {
    locales: ['en'],
    defaultLocale: 'en'
  },
  interpolation: {
    prefix: "{",
    suffix: "}",
  }
}

To make custom prefix and suffix work, I had to change localeStructure to match the new prefix and suffix pattern like the suggestion here #620
However, this broke next-i18next and I had to change the hardcoded replace as @vechnii2007 said. Then everything started to work properly.

@sbekrin
Copy link

sbekrin commented May 27, 2021

hey @isaachinman, the underlying issue is that next-i18next validates localeStructure assuming it uses double brackets syntax, however it depends on interpolation config on i18next side. It results in defaultNSExists check to always fail in dev mode with non-standard interpolation.[prefix|suffix].

The only workaround for it at the moment is to create an empty file based on custom [prefix|suffix] options e.g. ./public/locales/%{lng}/%{ns}.json (yes, with literlly that being the filesystem path) to trick it into passing the check.

@isaachinman
Copy link
Contributor

@sbekrin @vechnii2007 Yep, that's correct – I misspoke when first commenting on this issue. The interpolation option for the i18next config is not consumed in localeStructure, but it probably makes sense to do so. A PR would be welcome!

@isaachinman isaachinman reopened this May 30, 2021
@ahakem
Copy link

ahakem commented May 31, 2021

@sbekrin your workaround worked with me, thank you.

@ahakem
Copy link

ahakem commented May 31, 2021

@zachguo this worked with me as well as you tried, but @sbekrin workaround is nice for now, till we made a PR.

joostdecock added a commit to joostdecock/next-i18next that referenced this issue Feb 10, 2022
It took me a while to figure out how to get a custom
interpolation prefix/suffix to work.

I think it's a little unintuitive that changing an option
that relates to how the text inside the translation files
is structured has a knock-on effect to how files are loaded.

After doing some digging, I came across i18next#1090 that clarifies
the issue, but I feel it's worth putting this in the README.

This PR does that. It's not much, but I hope you find it useful.
isaachinman added a commit that referenced this issue Feb 13, 2022
It took me a while to figure out how to get a custom
interpolation prefix/suffix to work.

I think it's a little unintuitive that changing an option
that relates to how the text inside the translation files
is structured has a knock-on effect to how files are loaded.

After doing some digging, I came across #1090 that clarifies
the issue, but I feel it's worth putting this in the README.

This PR does that. It's not much, but I hope you find it useful.

Co-authored-by: Isaac Hinman <[email protected]>
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.

5 participants