@anchan828/nest-i18n-i18next
TypeScript icon, indicating that this package has built-in type declarations

1.0.70 • Public • Published

@anchan828/nest-i18n-i18next

npm NPM

Description

i18n module for i18next

Installation

$ npm i --save @anchan828/nest-i18n-i18next i18next

Quick Start

Importing module

@Module({
  imports: [
    I18nextModule.register({
      fallbackLng: ["en"],
      resources: {
        en: {
          translation: { test: "Test" },
        },
        jp: {
          translation: { test: "テスト" },
        },
      },
    }),
  ],
})
export class AppModule {}

Basic usage

@Controller()
class TestController {
  @Get("test")
  public test(@Headers("accept-language") acceptLanguage: string): string {
    return i18next.t("test", { lng: acceptLanguage });
  }
}

Using with controller

Use I18nExceptionFilter

import { I18nExceptionFilter, I18nNotFoundException } from "@anchan828/nest-i18n-i18next";

@Controller()
@UseFilters(I18nExceptionFilter)
class TestController {
  @Get("error")
  public i18nError(): Promise<string> {
    throw new I18nNotFoundException({ key: "test" });
  }
}

Using with GraphQL

Use I18nExceptionFilter

import { I18nExceptionFilter, I18nNotFoundException } from "@anchan828/nest-i18n-i18next";

@Resolver("User")
@UseFilters(I18nExceptionFilter)
export class RootUserQueryResolver {
  @Query()
  public async user(@Args("id") id: string): Promise<Nullable<UserEntity>> {
    throw new I18nNotFoundException({ key: "test" });
  }
}

Set Accept-Language header

You need to set Accept-Language header for detecting language.

Note

There are wrapped exception classes for i18n. See ./src/exceptions.t

This package is using i18next-fs-backend for loading translations from static files.

I18nextModule.register({
  fallbackLng: ["en"],
  backend: {
    loadPath: "/locales/{{lng}}/{{ns}}.json",
  },
});

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @anchan828/nest-i18n-i18next

Weekly Downloads

0

Version

1.0.70

License

MIT

Unpacked Size

25 kB

Total Files

15

Last publish

Collaborators

  • anchan828