Skip to main content
Skip table of contents

Translation

Overview

The NestJS Translator module is a powerful, easy-to-use tool that enables developers to implement translations in their NestJS applications seamlessly. This module provides a set of decorators and interceptors to help manage translations for different entities and fields within the application. The module allows developers to define translatable fields in entities, automatically handle translations for API calls, and update translations when necessary.

To implement the translator module, developers must add the @TranslatableFields decorator to entities, use the @UseInterceptors decorator with the TranslationsInterceptor and TranslationsUpdateInterceptor in controllers, and follow strict naming conventions for entities and controllers. Additionally, developers must set the nxt-model-lang header in their client application to specify the desired language.

Implementation Guide

Step 1: Define Translatable Fields in Entities

In your entity files, import the TranslatableFields decorator and use it to specify the fields that should be translatable. Pass an array of field names as an argument to the decorator.

For example, if you have a TireEntity with name and description fields that need translations:

CODE
import { TranslatableFields } from './translatable-fields.decorator';

@Entity('tire')
@TranslatableFields(['name', 'description'])
export class TireEntity {
  // ... Your model's fields ...
}

Step 2: Add Interceptors to Controllers

In your controller files, import the TranslationsInterceptor and TranslationsUpdateInterceptor and use them with the @UseInterceptors decorator.

CODE
import { TranslationsInterceptor, TranslationsUpdateInterceptor } from './translations.interceptors';

@Controller('tires')
@UseInterceptors(TranslationsInterceptor)
@UseInterceptors(TranslationsUpdateInterceptor)
export class TiresController {
  // ... Your controller's methods ...
}

This will ensure that the necessary translations are fetched and updated when the controller's methods are called.

Step 3: Follow Naming Conventions

When implementing the translator module, make sure to follow strict naming conventions for entities and controllers. The entity class should be named using the singular form of the entity (e.g., TireEntity), while the controller should use the plural form (e.g., TiresController). This helps the module identify the corresponding entity for a given controller.

Step 4: Set the nxt-model-lang Header

In your client application, include the nxt-model-lang header in your API requests to specify the desired language for the translations. The translator module will use this header value to fetch the appropriate translations for the specified entity and fields.

For example, to request translations in German:

CODE
const headers = new Headers({
  'nxt-model-lang': 'de',
  // ... Other headers ...
});

fetch('/api/tires/27', { headers });

Summary

The NestJS Translator module is a powerful solution for managing translations in your NestJS applications. By following this implementation guide, you can easily set up translations for your entities and fields, automatically handle translations for API calls, and update translations when necessary. Just remember to define translatable fields in entities, add interceptors to controllers, follow strict naming conventions, and set the nxt-model-lang header in your client application.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.