在页面/组件之外访问 next-i18next 翻译

问题描述

我正在使用 Typescript 构建 Next.JS 应用程序,并希望访问页面/组件之外的 next-i18next 翻译,以便本地化验证消息,而所有验证引擎都在单独的实用程序类中定义。检查 next-i18next 是否有任何类型的静态访问器,但没有运气。有什么建议吗?

解决方法

我认为唯一的方法是将 i18n 作为参数传递给您的实用程序类。

import { withTranslation,WithTranslation } from 'i18n';
import { checkFunction } from '../utils';

type Props = WithTranslation;

const Page: React.FC<Props> = (props) => {
  const result = checkFunction(props.i18n);
}

export default withTranslation(['common'])(Page);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...