[英]Get i18next to fallback to 'NO TRANSLATION'
The i18next translation library by default seems to fallback to the translation key if no translation was found for the key, eg 如果未找到翻译密钥,则默认情况下,i18next翻译库似乎会回退到翻译密钥。
// No translation defined for CANCEL yet
i18next.t('CANCEL') // Returns 'CANCEL'
If no translation is found for the key, I would prefer to fallback to a distinctive message that makes it obvious that we've missed a translation or mistyped a translation key. 如果没有找到该密钥的翻译,我希望回退到一条独特的消息,该消息清楚地表明我们错过了翻译或键入了错误的翻译密钥。 Preferably mentioning the key in the message eg
最好在消息中提及密钥,例如
i18next.t('CANCEL') // Returns 'No translation found for "CANCEL"'
Some sort of fallback callback function would be ideal because then we could also log to console/remote service any missing translations. 某种后备回调函数将是理想的,因为这样我们也可以登录到控制台/远程服务来查找所有缺少的翻译。
How can I achieve something like this using the i18next library? 如何使用i18next库实现类似的功能?
So it turns out i18next accepts a handler in the options when initialising that lets you set the value for missing keys. 因此,事实证明,i18next会在初始化时在选项中接受一个处理程序,使您可以为缺少的键设置值。
i18n.init({
...
saveMissing: true, // Must be set to true
parseMissingKeyHandler: (key: string) => {
return `No translation found for "${key}"`;
}
})
According to the official documentation saveMissing
is not required for parseMissingKeyHandler
to work. 按照官方文档
saveMissing
不需要 parseMissingKeyHandler
工作。 My code works fine without it. 没有它,我的代码可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.