@anchan828/nest-i18n-i18next

![npm](https://img.shields.io/npm/v/@anchan828/nest-i18n-i18next.svg) ![NPM](https://img.shields.io/npm/l/@anchan828/nest-i18n-i18next.svg)

Usage no npm install needed!

<script type="module">
  import anchan828NestI18nI18next from 'https://cdn.skypack.dev/@anchan828/nest-i18n-i18next';
</script>

README

@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