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

Nested translation resolution between languages uses the fallback language for nested translations #2216

Closed
Jazzmanpw opened this issue Jul 18, 2024 · 4 comments

Comments

@Jazzmanpw
Copy link

Jazzmanpw commented Jul 18, 2024

🐛 Bug Report

Unexpected translation resolution takes place in case of translation nesting. You can see it in the example, but in a few words: English is default, French is another language; we have the one_dollar key that's translated as $$t("one"), resulting into $one in English and $un in French (expectedly), but i18next actually produces $one in both cases

To Reproduce

A codesandbox example

// /en/translations.json
{
  "one": "one",
  "one_dollar": "$$t(one)"
}
// /fr/translations.json
{
  "one": "un"
}
i18next
  .init({
    resources: {
      en: {
        translations: translationsEn,
      },
      fr: {
        translations: translationsFr,
      },
    },
    supportedLngs: ["en", "fr"],
    fallbackNS: "translations",
    defaultNS: "translations",
    fallbackLng: "en",
    defaultLng: "en",
  })
  .then((t) => {
    t("one_dollar"); // resolves to $one in both languages
  });

Expected behavior

// With the previous setup, expect this
t("one_dollar")
// to be $one in English and $un in French

Your Environment

  • runtime version: Chrome 126.0.6478.127
  • i18next version: 23.11.1
  • os: Windows
@adrai
Copy link
Member

adrai commented Jul 18, 2024

Not able to reproduce...

i18next
  .init({
    resources: {
      en: {
        translations: {
          "one": "one",
          "one_dollar": "$$t(one)"
        },
      },
      fr: {
        translations: {
          "one": "un"
        },
      },
    },
    supportedLngs: ["en", "fr"],
    fallbackNS: "translations",
    defaultNS: "translations",
    fallbackLng: "en",
    defaultLng: "en",
  })
  .then((t) => {
    console.log('en: '   t("one_dollar", { lng: 'en' })); // en: $one
    console.log('fr: '   t("one_dollar", { lng: 'fr' })); // fr: $un
  });

@Jazzmanpw
Copy link
Author

Jazzmanpw commented Jul 19, 2024

Sorry, I somehow copied wrong link to the codesandbox, now updated

I'm not talking about passing a language to t call. It's broken if I use i18next.changeLanguage()

So you can reproduce with this

i18next
  .init({
    resources: {
      en: {
        translations: {
          "one": "one",
          "one_dollar": "$$t(one)",
          "two": "two",
          "two_dollars": "$$t(two)"
        },
      },
      fr: {
        translations: {
          "one": "un",
          "two": "deux",
          "two_dollars": "$$t(two)"
        },
      },
    },
    supportedLngs: ["en", "fr"],
    fallbackNS: "translations",
    defaultNS: "translations",
    fallbackLng: "en",
    defaultLng: "en",
  })
  .then((t) => {
    console.log("en: "   t("one_dollar")); // en: $one
    console.log("en: "   t("two_dollars")); // en: $two
    i18next.changeLanguage("fr");
    console.log("fr: "   t("one_dollar")); // fr: $one
    console.log("fr: "   t("two_dollars")); // fr: $deux
  });

adrai added a commit that referenced this issue Jul 19, 2024
…k language for nested translations #2216 by preserving issue fix "`lng` is undefined when formatter used in `$t( )`" #1938
@adrai
Copy link
Member

adrai commented Jul 19, 2024

Please try with v23.12.2

@Jazzmanpw
Copy link
Author

Jazzmanpw commented Jul 19, 2024

yes, now it works! thank you 🙏 lightning-fast fix

@adrai adrai closed this as completed Jul 19, 2024
heharm pushed a commit to wanderlog/i18next that referenced this issue Aug 15, 2024
…k language for nested translations i18next#2216 by preserving issue fix "`lng` is undefined when formatter used in `$t( )`" i18next#1938
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants