我知道react-i18next适用于每个组件:功能组件(使用useTranslation)和类组件(使用withTranslation()),但我不能在像这样的基本函数中使用翻译:
const not_a_component = () => {
const { t } = useTranslation();
return t('translation')
};
const translate = not_a_component();
错误挂钩!
Thanks !
你可以只使用i18next
使用 javascript 进行翻译的库。react-i18next
只是一个包装库i18next
.
如果您已经在使用,下面是一个示例react-i18next
并已配置。
import i18next from "i18next";
const not_a_component = () => {
const result = i18next.t("key");
console.log(result);
return result;
};
export default not_a_component;
如果您选择仅使用i18next
那么你可以简单地得到t
功能。
这完全取决于您的要求。
import i18next from 'i18next';
i18next.init({
lng: 'en',
debug: true,
resources: {
en: {
translation: {
"key": "hello world"
}
}
}
}, function(err, t) {
// You get the `t` function here.
document.getElementById('output').innerHTML = i18next.t('key');
});
希望有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)