Skip to content

LanguagesModule

The LanguagesModule provides a list of languages and their corresponding information such as name in the "Area/Location" format, title and offset. It can be used to retrieve language data for various purposes such as displaying language options in a user interface or converting time between different languages.

Key Components

The module provides a set of reusable components that can be easily integrated into a NestJS project:

Language Entity

The Language entity represents a language with its code in various formats, such as BCP-47, ISO 639-1, ISO 639-2, localizations, and the language name in the localization language and in its native tongue. By default, English localization ("en") is used, but this value can be adjusted.

NestJS Languages Service

LanguagesService is a service in the Languages API that provides methods for fetching languages data.

GraphQL Languages Resolver

The GraphQL LanguagesResolver in NestKit provides a convenient way to retrieve languages data in a GraphQL API.

How to install LanguagesModule to my app

To create a LanguagesModule in NestJS, you can follow these steps:

  1. Create a languages folder in your project's src/modules directory.

  2. Inside the languages folder, create a languages.service.ts file and extends BaseLanguagesService from NestKit with the following code:

    ts
    // src/modules/languages/languages.service.ts
    
    import { BaseLanguagesService } from '@deeepvision/nest-kit/dist/modules/languages';
    
    export class LanguagesService extends BaseLanguagesService {}
  3. Inside the languages folder, create a languages.resolver.ts file and extends BaseLanguagesResolver with the following code:

    ts
    // src/modules/languages/languages.resolver.ts
    
    import { BaseLanguagesResolver, Language } from '@deeepvision/nest-kit/dist/modules/languages';
    import { Resolver } from '@nestjs/graphql';
    
    @Resolver(() => Language)
    export class LanguagesResolver extends BaseLanguagesResolver() {}
  4. Inside the languages folder, create a languages.module.ts file with the following code:

    ts
    // src/modules/languages/languages.module.ts
    
    import { LANGUAGES_SERVICE_TOKEN } from '@deeepvision/nest-kit/dist/modules/languages';
    import { Module } from '@nestjs/common';
    import { LanguagesResolver } from './languages.resolver';
    import { LanguagesService } from './languages.service';
    
    @Module({
      providers: [
        LanguagesService,
        {
          provide: LANGUAGES_SERVICE_TOKEN,
          useExisting: LanguagesService,
        },
        LanguagesResolver,
      ],
      exports: [
        LanguagesService,
        {
          provide: LANGUAGES_SERVICE_TOKEN,
          useExisting: LanguagesService,
        },
      ],
    })
    export class LanguagesModule {}
  5. Finally, import the LanguagesModule into your main AppModule module:

    ts
    // src/modules/app.module.ts
    
    import { Module } from '@nestjs/common';
    import { LanguagesModule } from './modules/languages/languages.module.ts';
    
    @Module({
      imports: [LanguagesModule],
    })
    export class AppModule {}

In this code, we are importing the LanguagesModule and adding it to the imports array of our AppModule.

In this example, we are importing the LanguagesModule and adding it to OrdersModule imports array. This makes the LanguagesService available for injection in any service provided to OrdersModule.

How to get languages list

Use the getMany() method in other parts of your application to retrieve a list of languages.

ts
const [languages, meta] = await this.languagesService.getMany({
  filter: {
    ...
  },
  orderBy: LanguagesOrderBy.title_ASC,
  locale: "uk",
});

Use the locale parameter to get language names in the specified language, default is "en".

See LanguagesFilter and LanguagesOrderBy for more information on how to filter and order languages.

Fetch language list with GraphQL

Here's an example GraphQL query to fetch languages.

Created by DeepVision Software.