【发布时间】:2019-11-09 19:32:51
【问题描述】:
我关注了react-i18next docs 并将我的 React 应用程序国际化。
我使用useTranslation 钩子,它给了我“t”对象。目前一切顺利,但我有一些非 Hook 实用程序函数,它们位于组件树之外。
如何访问那里的翻译对象?
【问题讨论】:
标签: reactjs internationalization i18next react-i18next
我关注了react-i18next docs 并将我的 React 应用程序国际化。
我使用useTranslation 钩子,它给了我“t”对象。目前一切顺利,但我有一些非 Hook 实用程序函数,它们位于组件树之外。
如何访问那里的翻译对象?
【问题讨论】:
标签: reactjs internationalization i18next react-i18next
这似乎可行,导入i18next 而不是react-i18next:
// import { useTranslation } from "react-i18next";
import i18next from "i18next";
function hello() {
return i18next.t("hello");
}
【讨论】:
这个对我有用,
//导入i18next
import i18next from 'i18next';
//那么你需要像这样尝试的地方
i18next.t('common:messages.errorMessage')
【讨论】:
不确定它是否会起作用,但你可以试试这个:
import i18n from "react-i18next";
// Then where ever you need try to this
i18n.t("target")
【讨论】: