You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

nestjs-i18n

Package Overview
Dependencies
Maintainers
2
Versions
115
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-i18n

The i18n module for Nest.


Version published
Weekly downloads
113K
decreased by-12.07%
Maintainers
2
Created
Weekly downloads
 

Package description

What is nestjs-i18n?

nestjs-i18n is a powerful internationalization (i18n) module for NestJS applications. It provides a comprehensive set of tools to manage translations, handle localization, and support multiple languages in a NestJS application.

What are nestjs-i18n's main functionalities?

Basic Setup

This code sets up the basic configuration for nestjs-i18n. It initializes the I18nModule with a fallback language and specifies the parser and the path to the translation files.

```typescript
import { Module } from '@nestjs/common';
import { I18nModule, I18nJsonParser } from 'nestjs-i18n';
import * as path from 'path';

@Module({
  imports: [
    I18nModule.forRoot({
      fallbackLanguage: 'en',
      parser: I18nJsonParser,
      parserOptions: {
        path: path.join(__dirname, '/i18n/'),
      },
    }),
  ],
})
export class AppModule {}
```

Translation Service

This code demonstrates how to use the I18nService to translate a message. The `translate` method is used to fetch the translation for the given key ('HELLO_MESSAGE').

```typescript
import { Injectable } from '@nestjs/common';
import { I18nService } from 'nestjs-i18n';

@Injectable()
export class AppService {
  constructor(private readonly i18n: I18nService) {}

  async getHello(): Promise<string> {
    return this.i18n.translate('HELLO_MESSAGE');
  }
}
```

Request Scoped Translations

This code shows how to use request-scoped translations. By injecting the I18nContext into the controller method, you can translate messages based on the current request's language.

```typescript
import { Controller, Get } from '@nestjs/common';
import { I18n, I18nContext } from 'nestjs-i18n';

@Controller()
export class AppController {
  @Get()
  async getHello(@I18n() i18n: I18nContext): Promise<string> {
    return i18n.translate('HELLO_MESSAGE');
  }
}
```

Other packages similar to nestjs-i18n

Readme

Source

nestjs-i18n Logo

The i18n library for nestjs. It makes working with languages in your nestjs project easy. Everything is made to be highly configurable. You can write and plug-in your own language resolvers or loaders.

npm version NPM npm downloads coverage

Features

nestjs-i18n comes with a bunch of tools to help add multiple language support to your project.

Getting started

To get started follow the quickstart, or take a look at the docs.

License

nestjs-i18n is MIT licensed.

Breaking changes:

  • from V10.0.0 on we removed the I18nRequestScopeService. This because we now have async_hooks support. This allows you to get the current I18nContext by doing I18nContext.current().

  • from V9.0.0 on we renamed the parser property to loader. The translate function no longer returns a promise 🎉. A lot of new features and new docs, see nestjs-i18n.com.

  • from V8.0.0 on we changed the internal I18n-middleware for an interceptor this way we can provide the ExecutionContext so that nestjs-i18n works on different protocols as well, such as gRPC and WebSockets. This contains one breaking change. It only applies to your code if you've made a custom resolver. To resolve this breaking change take a look at this example. Instead of providing the req in the resolve method, change this to take the ExecutionContext as argument.

  • from V6.0.0 on we implemented the I18nParser, by using this we can easily support different formats other than JSON. To migrate to this change look at the quickstart. There are some changes in the declaration of the I18nModule. Note: the translate function returns a Promise<string>. So you need to call it using await i18n.translate('HELLO');

  • from V4.0.0 on we changed the signature of the translate function, the language is now optional, if no language is given it'll fallback to the fallbackLanguage.

  • from V3.0.0 on we load translations based on their directory name instead of file name. Change your translations files to the structure found in the quickstart.

Keywords

FAQs

Package last updated on 16 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc