nestjs-i18n

website·Indexed 32 days ago

https://nestjs-i18n.com

An internationalization (i18n) library for NestJS providing support for translation loaders, resolvers, and type-safe translations. It includes features for DTO validation, pluralization, and integration with view engines like Pug, EJS, and Handlebars, as well as support for GraphQL and gRPC.

Tokens
15.4K
Snippets
111
Records
151
Agent score
61%

What's inside nestjs-i18n

  1. Overview of nestjs-i18n features

    nestjs-i18n is a highly configurable internationalization library for NestJS. It provides tools for adding multi-language support, including type safety, variable formatting, fallback languages, live reloading, plurals support, and DTO validation. It supports GraphQL, gRPC, and various view engines (hbs, pug, ejs). Developers can extend the library by implementing custom resolvers, loaders, and formatters.
  2. Overview of nestjs-i18n features

    nestjs-i18n is a highly configurable internationalization library for NestJS. It provides a suite of tools for adding multi-language support, including type safety, variable formatting, nested translations, fallback languages, and live reloading. It supports Plurals, GraphQL, gRPC, and DTO validation. Additionally, it integrates with view engines such as Handlebars (hbs), Pug, and EJS, and allows for custom resolvers, loaders, and formatters.
  3. I18nService Class Overview

    The I18nService is the core service for handling internationalization in nestjs-i18n. It implements I18nTranslator<K> and OnModuleDestroy. It uses a type parameter K (of type Record<string, unknown>) to provide type safety for translation keys.
  4. I18nValidationPipe class overview

    The I18nValidationPipe is a specialized validation pipe that extends the standard NestJS ValidationPipe. It is used to provide internationalized validation error messages within a NestJS application.
  5. I18nAbstractLoader class overview

    I18nAbstractLoader is an abstract base class that extends I18nLoader and implements OnModuleDestroy. It provides a foundation for specific translation loaders, such as I18nJsonLoader and I18nYamlLoader. It handles the core logic for loading translations and managing language lists.
  6. I18nTranslator Interface Overview

    The I18nTranslator interface defines the contract for translation services in nestjs-i18n. It is implemented by I18nContext and I18nService. It uses a type parameter K (of type Record<string, unknown>) to represent the translation resource structure.
  7. I18nContext class overview

    The I18nContext class implements the I18nTranslator interface and is used to manage translation state for a specific request or context. It holds the current language and a reference to the I18nService.
  8. Enforce valid translation paths with I18nPath

    The generated file provides an I18nPath type. Use this type when you need to ensure a string is a valid translation key, such as when defining custom exception classes that handle translation keys.
    import { I18nPath } from './generated/i18n.generated.ts';
    
    export class ApiException extends Error {
      get translation(): I18nPath {
        return this.message as I18nPath;
      }
    
      constructor(key: I18nPath, private readonly _args?: any) {
        super(key);
      }
    }
  9. Use arrays in nestjs-i18n translations

    You can define arrays in your translation JSON files. To retrieve a specific element from an array, use the translation key followed by a dot and the zero-based index of the element. To retrieve the entire array as a translated list, use the base key of the array.
    // src/i18n/en/test.json
    {
      "animals": [
        "cat",
        "dog",
        "elephant"
      ]
    }
    // Retrieve individual elements by index
    await i18n.t('test.animals.0'); // 'cat'
    await i18n.t('test.animals.1'); // 'dog'
    await i18n.t('test.animals.2'); // 'elephant'
    
    // Retrieve the entire translated array
    await i18n.t('test.animals'); // ['cat', 'dog', 'elephant']
  10. Resolve language using QueryResolver.resolve()

    The resolve method is the primary function of the QueryResolver class. It takes an ExecutionContext as an argument and returns the resolved language as a string. This method fulfills the requirement of the I18nResolver interface.
    // The resolve method signature
    resolve(context: ExecutionContext): string