回归报告 我无法使用 LanguageDetector 包更改语言,当我将后备语言设置为 en 或 ar 时,它工作得很好,但在设置之后,如果我尝试从标头更改语言,它不会改变即使我在设置 debug: true 时看到此日志
// logs on each request that i have Accept-Language Header set to ar
i18next: languageChanged en
i18next: languageChanged ar
i18next::translator: missingKey en zod email email
配置
// configure dotenv before every thing, even imports
import * as dotenv from 'dotenv';
import { join, resolve } from 'path';
dotenv.config({ path: resolve(__dirname, '.env') });
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import i18next from 'i18next';
import enTranslations from 'zod-i18n-map/locales/en/zod.json';
import { z } from 'zod';
import { makeZodI18nMap } from 'zod-i18n-map';
import languageDetector from 'i18next-http-middleware';
import i18nextBackend from 'i18next-fs-backend';
import arTranslations from 'zod-i18n-map/locales/ar/zod.json';
import enTranslations from 'zod-i18n-map/locales/en/zod.json';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(languageDetector.handle(i18next));
await i18next
.use(i18nextBackend)
.use(languageDetector.LanguageDetector)
.init({
debug: true,
supportedLngs: ['en', 'ar'],
fallbackLng: 'en',
resources: {
ar: { zod: arTranslations },
en: { zod: enTranslations },
},
});
z.setErrorMap(makeZodI18nMap({ ns: 'zod', t: i18next.t }));
app.use(compression());
await app.listen(3000);
}
bootstrap();
预期行为 将消息返回为“ar”而不是fallbackLng
您的环境 运行时版本:最新的 Nestjs i18下一个版本:23.5.1 操作系统:Windows zod-i18n-map:2.20.0 调试
i18next: languageChanged en
i18next: initialized {
debug: true,
initImmediate: true,
ns: [ 'translation' ],
defaultNS: [ 'translation' ],
fallbackLng: [ 'en' ],
fallbackNS: false,
supportedLngs: [ 'en', 'ar', 'cimode' ],
nonExplicitSupportedLngs: false,
load: 'all',
preload: false,
simplifyPluralSuffix: true,
keySeparator: '.',
nsSeparator: ':',
pluralSeparator: '_',
contextSeparator: '_',
partialBundledLanguages: false,
saveMissing: false,
updateMissing: false,
saveMissingTo: 'fallback',
saveMissingPlurals: true,
missingKeyHandler: false,
missingInterpolationHandler: false,
postProcess: false,
postProcessPassResolved: false,
returnNull: false,
returnEmptyString: true,
returnObjects: false,
joinArrays: false,
returnedObjectHandler: false,
parseMissingKeyHandler: false,
appendNamespaceToMissingKey: false,
appendNamespaceToCIMode: false,
overloadTranslationOptionHandler: [Function: handle],
interpolation: {
escapeValue: true,
format: [Function: bound format],
prefix: '{{',
suffix: '}}',
formatSeparator: ',',
unescapePrefix: '-',
nestingPrefix: '$t(',
nestingSuffix: ')',
nestingOptionsSeparator: ',',
maxReplaces: 1000,
skipOnVariables: true
},
resources: { ar: { zod: [Object] }, en: { zod: [Object] } },
ignoreJSONStructure: true
}
我通过使用这个中间件解决了这个问题,很确定有更好的方法,但这对我有用,我愿意接受任何更好的建议!
app.use(
(
req: Request & { language: string },
res: Response,
next: NextFunction,
) => {
z.setErrorMap(
makeZodI18nMap({
t: i18next.getFixedT(req.language),
}),
);
next();
},
);