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

Interpolation not working in ICU format when fetching an endpoint #52

Closed
paolapog opened this issue Jul 5, 2022 · 13 comments
Closed

Interpolation not working in ICU format when fetching an endpoint #52

paolapog opened this issue Jul 5, 2022 · 13 comments

Comments

@paolapog
Copy link

paolapog commented Jul 5, 2022

🐛 Bug Report

Hello everyone, we were trying to pass some data for this json:
{"delete_board_modal_title" : "Delete {title}"}

in this way :

  <Trans
    i18nKey="delete_board_modal_title"
    values={{ title: boardTitle }}
  />

//or like this

{t("delete_board_modal_title", { title: boardTitle })}

but it seems to not interpolate correctly the curly brace because I see this result Delete {title}.
The weirdest thing is that if we use a local folder public/locales/en_US.json is working correctly but not when is fetching from this endpoint: https://ourBeautifulEndpoint.com/v2/en_US.json.
Or it also works like this:
<Trans i18nKey="Delete {title}" values={{ title: boardTitle }} />
or it works with the double curly braces but it's not what we want because we need ICU format.
Maybe we are missing something important for sure, what should we do?

This is the I18next config:

import i18next from "i18next";
import HttpApi from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import ICU from "i18next-icu";
import { initReactI18next } from "react-i18next";

const backendOptions = {
  type: "backend",
  allowMultiLoading: true,
  loadPath: "https://ourBeautifulEndpoint.com/v2/{{lng}}.json"
};

i18next
  .use(ICU)
  .use(HttpApi)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    backend: backendOptions,
    whitelist: ["en_US", "it_IT"],
    detection: {
      order: ["path", "cookie", "localStorage", "sessionStorage"],
      lookupQuerystring: "lng",
      lookupCookie: "i18next",
      lookupLocalStorage: "i18nextLng",
      lookupSessionStorage: "i18nextLng",
      caches: ["localStorage", "cookie"]
    },
    keySeparator: true,

    debug: true,

    react: {
      useSuspense: false,
      bindI18n: "languageChanged"
    }
  });

export default i18next;

Environment

  • runtime version: node v14.19.1
  • i18next version: {
    "i18next": "^21.8.9",
    "i18next-browser-languagedetector": "^6.1.4",
    "i18next-http-backend": "^1.4.1",
    "i18next-icu": "^2.0.3"
    }
  • os: Mac chip m1 macOs Monterey

Thanks in advice and sorry if this is not a bug or it is duplicated

@adrai
Copy link
Member

adrai commented Jul 5, 2022

This should work without problems. There should be no influence based on the origin of the translation resources.
Is there any information/warning in the developer console? Or can you create a reproducible codesandbox example?

@paolapog
Copy link
Author

paolapog commented Jul 5, 2022

@adrai Thanks for the reply! No, there is no warning, unfortunately.
I provided you a codesandbox with a fake api: https://codesandbox.io/s/tender-pike-k8h87u?file=/src/App.js
Let me know if you can access to it

@adrai
Copy link
Member

adrai commented Jul 5, 2022

Sorry, but I need to see the http response, if it is a correct json. I suspect there is an issue in the http response body.

@paolapog
Copy link
Author

paolapog commented Jul 5, 2022

I cannot provide you the real endpoint, but I replicate it as real as I could and you can find it in that codesandbox.
Our http response returns a 200 status with the correct JSON. Indeed, in the console we have i18next::backendConnector: loaded namespace translation for language en_US

@adrai
Copy link
Member

adrai commented Jul 5, 2022

Just saw, you're not using suspense.
So please check for the ready flag: https://react.i18next.com/latest/usetranslation-hook#not-using-suspense

@adrai
Copy link
Member

adrai commented Jul 5, 2022

Another thing you could check is, the used language code format: https://www.i18next.com/how-to/faq#how-should-the-language-codes-be-formatted

Try en-US instead of en_US

@paolapog
Copy link
Author

paolapog commented Jul 6, 2022

I've just used Suspense but it's not working. If there was a problem with a "ready state", nothing would have loaded, not even the "Delete" part.
BUT, with the other format, it works 👍🏼 🎉 it's weird because we used the same format in our public folder and it worked but with an endpoint to fetch, nope. What it could be?
Thanks btw 🚀

@adrai
Copy link
Member

adrai commented Jul 6, 2022

Did you try to use en-US and not en-US?
intl-messageformat uses BCP 47 language tags: https://formatjs.io/docs/intl-messageformat#intlmessageformat-constructor
image

@adrai
Copy link
Member

adrai commented Jul 6, 2022

@paolapog
Copy link
Author

paolapog commented Jul 6, 2022

Yes, as I said before: with the other format, it works 👍🏼 I was just curious about the explanation, but you gave to me with the intl message format constructor. Thanks again! We can close this pr

@adrai adrai closed this as completed Jul 6, 2022
@the-line
Copy link

I also have this problem. Using the language codes "en", "de" and "nl". As soon as I remove the .use(ICU) line, interpolation is working again. There seems to be some problem with interpolation and ICU.

@adrai
Copy link
Member

adrai commented Nov 22, 2022

I also have this problem. Using the language codes "en", "de" and "nl". As soon as I remove the .use(ICU) line, interpolation is working again. There seems to be some problem with interpolation and ICU.

please create a reproducible example and open a new issue

@the-line
Copy link

I will do that if I'm able to make a reproducible example. 👍

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

3 participants