所以我目前正在从事一个正在进行的 NestJS 项目,我的领导给我分配了一项任务,以实现一种根据请求标头中的语言向前端返回不同错误的方法,经过一些研究后我得出的结论是 i18n 会这是我最好的选择,在按照 T 的文档进行操作后,我在邮递员中尝试了它,但它没有返回消息,而是返回了变量名称,所以假设 JSON 文件中有一个名为 test = HelloWorld 的变量,而不是HelloWorld,返回测试了。
我尝试在上一篇文章中搜索修复或建议,再次查看文档,甚至尝试从我的领导其他具有 i18n 设置的项目中复制代码,但仍然什么也没有,我不知道我缺少什么,所以我会如果有人可以提供帮助,请非常感激。
我将提供项目中的一些相关文件,以便有人能够发现问题所在。
package.json
{
"name": "nest-experiments",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mongoose": "^9.2.2",
"@nestjs/platform-express": "^9.0.0",
"axios": "^1.4.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"mongodb": "^5.5.0",
"mongoose": "^7.2.1",
"nestjs-i18n": "^10.2.6",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "29.5.1",
"@types/mongoose": "^5.11.97",
"@types/node": "18.16.12",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "29.5.0",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "29.1.0",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.2.0",
"typescript": "^5.0.0"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
nest-cli.json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"assets": [
{ "include": "i18n/**/*", "watchAssets": true }
]
}
}
app.module.ts 我知道这里的代码与当前文档有点不同,但这段代码来自我的主导项目
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import {
AcceptLanguageResolver,
CookieResolver,
HeaderResolver,
I18nJsonLoader,
I18nModule,
QueryResolver,
} from 'nestjs-i18n';
import * as path from 'path';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PointsModule } from './points/points.module';
import { SlackModule } from './slack/slack.module';
import { UsersModule } from './users/users.module';
@Module({
imports: [PointsModule, MongooseModule.forRoot('mongodb+srv://sharbelabousabha:[email protected]/?retryWrites=true&w=majority'), SlackModule, UsersModule,
I18nModule.forRoot({
fallbackLanguage: 'en',
loaderOptions: {
path: path.join(__dirname, '/i18n/'),
watch: true,
},
resolvers: [
// eslint-disable-next-line sort-keys
{ use: QueryResolver, options: ['lang', 'locale', 'l'] },
new HeaderResolver(['x-custom-lang']),
AcceptLanguageResolver,
new CookieResolver(['lang', 'locale', 'l']),
],
})
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
app.service.ts
import { Injectable } from '@nestjs/common';
import { I18nContext, I18nService } from 'nestjs-i18n';
@Injectable()
export class AppService {
constructor(private readonly i18n: I18nService) {}
getHello(): string {
console.log({ lang: I18nContext.current().lang })
return this.i18n.translate('Created.Success');
}
}
原来你还需要将 JSON 文件的名称放在开头,所以在这种情况下,它必须是
Created.Success
,而不是
user.Created.Success