Skip to content

Commit b9b36f3

Browse files
committed
add multi language
1 parent 99422ff commit b9b36f3

File tree

9 files changed

+70
-5
lines changed

9 files changed

+70
-5
lines changed

nest-cli.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
"deleteOutDir": true,
77
"watchAssets": true,
88
"assets": [
9-
"src/service-account.json"
9+
{
10+
"include": "service-account.json",
11+
"watchAssets": true
12+
},
13+
{
14+
"include": "i18n/**/*",
15+
"watchAssets": true,
16+
"outDir": "dist"
17+
}
1018
]
1119
}
1220
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"ejs": "^3.1.8",
4040
"ioredis": "^5.3.2",
4141
"jsonwebtoken": "^9.0.1",
42+
"nestjs-i18n": "^10.2.6",
4243
"passport": "^0.6.0",
4344
"passport-jwt": "^4.0.1",
4445
"pg": "^8.11.1",

public/openapi.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ paths:
103103
parameters:
104104
- $ref: "#/components/parameters/AppVersionParameter"
105105
- $ref: "#/components/parameters/AppPlatformParameter"
106+
- $ref: "#/components/parameters/AppLangParameter"
106107
tags:
107108
- "Authentication"
108109
description: |
@@ -228,6 +229,13 @@ components:
228229
example: "Android"
229230
schema:
230231
type: string
232+
AppLangParameter:
233+
name: 'X-APP-LANG'
234+
in: header
235+
required: true
236+
example: "en"
237+
schema:
238+
type: string
231239
securitySchemes:
232240
ApiKey:
233241
type: apiKey

src/apps/app.module.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@ import { AppController } from './app.controller';
77
import { AppService } from './app.service';
88
import { AuthModule } from './auth/auth.module';
99
import { FileModule } from './file/file.module';
10-
10+
import { I18nModule, QueryResolver, HeaderResolver, AcceptLanguageResolver } from 'nestjs-i18n'
11+
import { join } from 'path';
1112
@Module({
1213
imports: [
1314
ConfigModule.forRoot({
1415
isGlobal: true
1516
}),
17+
I18nModule.forRoot({
18+
fallbackLanguage: 'en',
19+
loaderOptions: {
20+
path: join(__dirname, '..', 'i18n'),
21+
watch: true,
22+
},
23+
resolvers: [
24+
new QueryResolver(["lang", "l"]),
25+
new HeaderResolver(["x-app-lang"]),
26+
AcceptLanguageResolver,
27+
]
28+
}),
1629
TypeOrmModule.forRootAsync(defaultTypeOrmConfig),
1730
JwtModule.registerAsync({
1831
global: true,

src/apps/auth/auth.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Response } from 'src/utils/response.utils';
55
import { AuthService } from './auth.service';
66
import { LoginDTO } from './dto/login.dto';
77
import { RegisterDTO } from './dto/register.dto';
8+
import { I18n, I18nContext } from 'nestjs-i18n';
89

910
@Controller('auth')
1011
export class AuthController {
@@ -16,13 +17,14 @@ export class AuthController {
1617
@Post('/login')
1718
@HttpCode(HttpStatus.OK)
1819
async login(
20+
@I18n() i18n: I18nContext,
1921
@Req() req: Request,
2022
@Body() data: LoginDTO
2123
) {
2224
const loginResponse = await this.authService.login(data);
2325

2426
return Response.success({
25-
message: "Your identity accepted, login successful.",
27+
message: i18n.t('response.login.success'),
2628
data: loginResponse,
2729
});
2830
}

src/apps/file/file.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Body, Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
22
import { FileInterceptor } from '@nestjs/platform-express';
33
import { Express } from 'express';
4+
import { UseJwtGuard } from 'src/filters/jwt.guard';
45
import { FileService } from './file.service';
56

67
@Controller('file')

src/i18n/en/response.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"login": {
3+
"success": "Your identity accepted, login successful."
4+
}
5+
}

src/i18n/id/response.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"login": {
3+
"success": "Login berhasil"
4+
}
5+
}

yarn.lock

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,11 @@ abort-controller@^3.0.0:
15371537
dependencies:
15381538
event-target-shim "^5.0.0"
15391539

1540+
accept-language-parser@^1.5.0:
1541+
version "1.5.0"
1542+
resolved "https://registry.yarnpkg.com/accept-language-parser/-/accept-language-parser-1.5.0.tgz#8877c54040a8dcb59e0a07d9c1fde42298334791"
1543+
integrity sha512-QhyTbMLYo0BBGg1aWbeMG4ekWtds/31BrEU+DONOg/7ax23vxpL03Pb7/zBmha2v7vdD3AyzZVWBVGEZxKOXWw==
1544+
15401545
accepts@~1.3.8:
15411546
version "1.3.8"
15421547
resolved "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz"
@@ -2398,7 +2403,7 @@ [email protected]:
23982403
resolved "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz"
23992404
integrity sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==
24002405

2401-
2406+
[email protected], cookie@^0.5.0:
24022407
version "0.5.0"
24032408
resolved "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz"
24042409
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==
@@ -4128,7 +4133,7 @@ istanbul-reports@^3.1.3:
41284133
html-escaper "^2.0.0"
41294134
istanbul-lib-report "^3.0.0"
41304135

4131-
4136+
[email protected], iterare@^1.2.1:
41324137
version "1.2.1"
41334138
resolved "https://registry.npmjs.org/iterare/-/iterare-1.2.1.tgz"
41344139
integrity sha512-RKYVTCjAnRthyJes037NX/IiqeidgN1xc3j1RjFfECFp28A1GVwK9nA+i0rJPaHqSZwygLzRnFlzUuHFoWWy+Q==
@@ -5468,6 +5473,18 @@ neo-async@^2.6.0, neo-async@^2.6.2:
54685473
resolved "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz"
54695474
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==
54705475

5476+
nestjs-i18n@^10.2.6:
5477+
version "10.2.6"
5478+
resolved "https://registry.yarnpkg.com/nestjs-i18n/-/nestjs-i18n-10.2.6.tgz#ea15e863731036ddd5f4eb5068c56dab1355ebdc"
5479+
integrity sha512-vWjOXNz3ohJcKybtgdCWruNqreNOkVGfbTzGFxxdZ6Y9VfVJERT2+/30KAcBwJTpjuStoTVhMpILAKkXMk8KLQ==
5480+
dependencies:
5481+
accept-language-parser "^1.5.0"
5482+
chokidar "^3.5.3"
5483+
cookie "^0.5.0"
5484+
iterare "^1.2.1"
5485+
js-yaml "^4.1.0"
5486+
string-format "^2.0.0"
5487+
54715488
netmask@^2.0.2:
54725489
version "2.0.2"
54735490
resolved "https://registry.yarnpkg.com/netmask/-/netmask-2.0.2.tgz#8b01a07644065d536383835823bc52004ebac5e7"
@@ -6778,6 +6795,11 @@ streamsearch@^1.1.0:
67786795
resolved "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz"
67796796
integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==
67806797

6798+
string-format@^2.0.0:
6799+
version "2.0.0"
6800+
resolved "https://registry.yarnpkg.com/string-format/-/string-format-2.0.0.tgz#f2df2e7097440d3b65de31b6d40d54c96eaffb9b"
6801+
integrity sha512-bbEs3scLeYNXLecRRuk6uJxdXUSj6le/8rNPHChIJTn2V79aXVTR1EH2OH5zLKKoz0V02fOUKZZcw01pLUShZA==
6802+
67816803
string-length@^4.0.1:
67826804
version "4.0.2"
67836805
resolved "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz"

0 commit comments

Comments
 (0)