通常react组件中使用t变量的方式如下
import React from 'react';
import { useTranslation } from 'react-i18next';
function MyComponent( ) {
const { t } = useTranslation("namespace");
return (
<div>
<p>{t('key')}</p>
</div>
);
}
export default MyComponent;
在纯函数中,不方便使用hooks,获取i18n实例的方法
export function i18nFunc() {
const t = (key: string) =>
getI18n().t(key, { ns: "namespace" });
return { t };
}