NestJS-Localization
Hi friends, In this post, I am explaining how to implement the localization of NestJS using nestjs-i18n.
In our application, we are using the i18n library for NestJS. It's working with languages in your NestJS project. We can write and plug-in your own language resolvers or loaders.
Install the nestjs-i18n package in your application:
npm install --save nestjs-i18n
After that, we should be stored the language strings in files within the directory and also defined them in JSON files. When taking this approach, each language supported by your application would have a corresponding JSON file within this directory. This approach is recommended for applications that have a large number of translatable strings. By default nestjs-i18n
uses the I18nJsonLoader
loader class. This loader reads translations from json
files. Create a folder named i18n
in the src
folder of your project.
src
└── i18n
├── en
│ └── test.json
└── nl
└── test.json
src/i18n/en/test.json:
{
"here": "here",
"greet": "Dear",
"click": "Please click",
"greet_hello": "Hello there,"
}
src/i18n/nl/test.json:
{
"here": "hier",
"greet": "Beste",
"click": "Klik",
"greet_hello": "Hello there, "
}
Initially try to get the JSON file by absolute path, but I couldn’t get it. The reason is that the i18n
directory is not automatically copied to our dist
folder while starting the server. That's why To enable nestjs
to do this modify the compilerOptions
inside nest-cli.json
.
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [
{ "include": "i18n/**/*", "watchAssets": true }
]
}
}
We should register the i18nModule
module once in the root module. After that, it’s accessible throughout the whole application. InloaderOption
, To enable the watch option to be true, it will be enabling live reloading. resolvers
are used for getting the current language of our request. In my web applications, this is done via the Accept-Language
header. The way nestjs-i18n
works it's going to resolve the language in order. So in this case it tries the QueryResolver
first, if it can't resolve a language it'll jump to the next one.
import * as path from 'path';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AcceptLanguageResolver, I18nModule, QueryResolver } from 'nestjs-i18n';
@Module({
imports: [
I18nModule.forRoot({
fallbackLanguage: 'en',
loaderOptions: {
path: path.join(__dirname, '/i18n/'),
watch: true,
},
resolvers: [
{ use: QueryResolver, options: ['lang'] },
AcceptLanguageResolver,
],
}),
],
controllers: [AppController],
})
export class AppModule {}
setup everything is done, and now we can start to do translations! The easiest way to do this is in controller
level.
import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';
import { I18n, I18nContext } from 'nestjs-i18n';
@Controller()
export class AppController {
@Get()
getHello(@I18n() i18n: I18nContext) {
return i18n.t(`test.here`);
}
}
Conclusion
I hope, In this post, we have learned about NestJS localization, Thank you for reading this post, See you soon in the next post.