next-i18next - 挂钩丢失的密钥处理

问题描述 投票:0回答:0

我在我的 Next JS 项目中使用 next-i18next 来处理翻译。我想在缺少

key
时调用后端将其存储在数据库中。

我发现 i18next 中有这方面的功能,特别是通过设置

saveMissing
missingKeyHandler
功能(见这里:https://www.i18next.com/overview/configuration-options

但是,将

missingKeyHandler
添加到我的
next-i18next.config.js
文件时,出现此错误:“错误 - SerializableError:序列化从
._nextI18Next.userConfig.missingKeyHandler
返回的
getServerSideProps
在“/dashboard/assettrack”中出错。 原因:
function
无法序列化为JSON。请仅返回 JSON 可序列化数据类型。”

这是我的 next-i18next.config.js 文件:

module.exports = {
i18n: {
    defaultLocale: "en",
    locales: ["en","es","ca"]
},
localePath: path.resolve('./appdata/locales'),
localeStructure: '{{lng}}/{{ns}}',
debug: process.env.NODE_ENV === 'development',
reloadOnPrerender: process.env.NODE_ENV === 'development',
saveMissing: true,
missingKeyHandler: (lng, ns, key, fallbackValue) => {
    const message = `Missing translation key [${key}] for [${lng}/${ns}], fallbackValue: ${fallbackValue}`;
    console.warn(message);
    return { message };
}

}

这当然有道理,但问题来了,如何添加自己的

missingKeyHandler
功能?

我还尝试创建一个自定义 useTranslation 钩子来扩展本机 useTranslation 钩子,但我还没有取得任何成功。

next.js i18next next-i18next
© www.soinside.com 2019 - 2024. All rights reserved.