我正在开发组件来跟踪丢失的翻译,我的目标是使用样式组件对丢失的翻译进行样式设置,但我找不到一种方法来检测t("someKey")是否从当前设置的语言返回翻译值,或者如果它丢失,则返回默认回退。有什么想法吗?在这一点上,文档没有任何帮助。
发布于 2021-09-30 07:53:07
我们的团队部分地解决了这个问题(编辑:至少对于完全缺失的令牌,当它使用备用语言时还没有)。配置实际上是在i18next级别,请参阅:https://www.i18next.com/overview/configuration-options。
以下是使用next-i18next (在我写这篇文章时使用v8)的解决方案。注意,在此场景(https://github.com/isaachinman/next-i18next#unserialisable-configs)中,您需要i18n配置是可序列化的。
此函数预期在客户端运行,因此您可以使用@sentry/browser。
// NOTE: next-i18next.config.js is a JS file, so no TypeScript here
const Sentry = require("@sentry/browser");
module.exports = {
// @see https://www.i18next.com/overview/configuration-options
i18n: {
defaultLocale: "en",
locales: ["en", "fr", "de", "tr"],
localeDetection: false,
// Set to true to enable the missingKeyHandler callback
saveMissing: true,
// Method triggered client-side when a token is missing
// It will still respect your fallback language,
// but let you add a side-effect like calling Sentry
missingKeyHandler: (
ngs,
ns,
key,
fallbackValue,
updateMissing,
options
) => {
Sentry.captureException(new Error("Missing i18n key"), {
extra: { ngs, ns, key, fallbackValue, updateMissing, options },
});
},发布于 2021-07-13 04:40:34
我真的没有这方面的经验,所以到目前为止,我找到的唯一解决方案是禁用回退并在页面中导航。调试器将告诉您错误。
https://stackoverflow.com/questions/60492466
复制相似问题