【问题标题】:Access next-i18next translations outside of page/component访问页面/组件之外的 next-i18next 翻译
【发布时间】:2021-06-06 04:04:51
【问题描述】:

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

【问题讨论】:

    标签: next.js next-i18next


    【解决方案1】:

    我认为唯一的方法是将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);
    

    【讨论】:

    • 谢谢,我知道可以使用 withTranslation,但我希望有更可靠的解决方案,因为在这种情况下,如果翻译命名空间很少,很容易忘记包含其中一个翻译命名空间。我想在不传递翻译功能的情况下找到解决方案。
    猜你喜欢
    • 2019-11-28
    • 2021-08-14
    • 1970-01-01
    • 2016-04-01
    • 2012-10-27
    • 2021-06-26
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多