我们的团队部分解决了它(编辑:至少对于完全丢失的令牌,当它使用后备语言时还没有)。配置其实在i18next级别,见:https://www.i18next.com/overview/configuration-options。
这是使用next-i18next 时的解决方案(我写这篇文章时是v8)。请注意,在这种情况下,您需要 i18n 配置可序列化 (https://github.com/isaachinman/next-i18next#unserialisable-configs)。
此函数预计在客户端运行,因此您可以使用@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 },
});
},