0

I have react-intl set up on my webpage and I have verified that my intl prop contains the following:

{
    locale: 'it',
    messages: {
        it: {
            app.homepage.title: 'Casa'
        }
    }
}

Yes I get the error message:

[React Intl] Missing message: "app.homepage.title" for locale: "it", using default message as fallback.

I'm not sure what I'm doing wrong. I am using intl in the following way:

const title = intl.formatMessage({
  id: "app.homepage.title",
  defaultMessage: "Home"
});

Importing the messages

I am loading the messages from a locally-stored json file like so:

import Italian from "app/translations/it.json";
import locale_en from "react-intl/locale-data/en";
import locale_it from "react-intl/locale-data/it";

addLocaleData([...locale_en, ...locale_it]);

const loadTranslation = () => {
  const urlParams = new URLSearchParams(window.location.search);
  const locale = urlParams.has("locale")
    ? urlParams.get("locale")
    : navigator.language;
  const messages = {
    it: Italian,
    en: null
  };

  return { locale, messages };
};

const { locale, messages } = loadTranslation();

...

<IntlProvider locale={locale} messages={messages}>
    <Provider store={store}>
      <App />
    </Provider>
</IntlProvider>

My translation file contains the following:

{
  "app.signup": "Iscriviti",
  "app.login": "Accesso",
  "app.homepage.title": "Casa"
}
K G
  • 1,715
  • 6
  • 21
  • 29

1 Answers1

3

Ok so I figured it out. I need to modify the loadTranslations function like this and it works:

const loadTranslation = () => {
  const urlParams = new URLSearchParams(window.location.search);
  const locale = urlParams.has("locale")
    ? urlParams.get("locale")
    : navigator.language;
  const messages = {
    it: Italian,
    en: null
  };

  return { locale, messages: messages[locale] };
};
K G
  • 1,715
  • 6
  • 21
  • 29