Reputation: 21390
I followed the react-i18next docs and internationalized my React app.
I use the useTranslation
hook, which gives me the "t" object. All nice and smooth for now, but I have some non-Hook utility functions which are outside the component tree.
How to get access to the translation object there?
Upvotes: 8
Views: 6970
Reputation: 359
A few steps that worked for me in a similar situation:
i18n.use(initReactI18next).init({ ns: "en", ......
import i18next from "i18next";
import en from "../assets/locales/en.json";
i18next.addResourceBundle("en", "en", en);
const { t } = i18next;
t("some.test.key");
Upvotes: 0
Reputation: 69
This one worked for me,
//import i18next
import i18next from 'i18next';
//Then where ever you need to try like this
i18next.t('common:messages.errorMessage')
Upvotes: 6
Reputation: 564
This seems to work, import i18next
rather than react-i18next
:
// import { useTranslation } from "react-i18next";
import i18next from "i18next";
function hello() {
return i18next.t("hello");
}
Upvotes: 7
Reputation: 360
Not sure if it will work, but can you try this:
import i18n from "react-i18next";
// Then where ever you need try to this
i18n.t("target")
Upvotes: -3