📝i18n

The module provides a simple way to management different languages.

Imports

import { i18nService } from 'niro-health';

Environment Variables

name
description

REDIS_HOST

Host of the redis.

REDIS_PASSWORD

Port of the redis.

REDIS_PORT

Password of the redis.

Write .env file with environment variable. Below has an example:

.env
REDIS_HOST=redis://localhost:6379
REDIS_PORT=6379
REDIS_PASSWORD=

Detailed Section

This is an important module in the system and contains a detailed section.

📝Starting

Method of Use

To use this module, you need to inject it into the desired service.

import { Module } from '@nestjs/common';
import {
  i18nService,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
  RedisService,
  DebugService,
  PropStringService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'Ii18nService',
      useClass: i18nService,
    },
    {
      provide: 'IConfigurationService',
      useClass: ConfigurationService,
    },
    {
      provide: 'IValidatorRegexpService',
      useClass: ValidatorRegexpService,
    },
    {
      provide: 'IStringExService',
      useClass: StringExService,
    },
    {
      provide: 'IRedisService',
      useClass: RedisService,
    },
    {
      provide: 'IDebugService',
      useClass: DebugService,
    },
    {
      provide: 'IPropStringService',
      useClass: PropStringService,
    },
  ],
})
export class AppModule {}

Before use you do added new locale and define properties from language.

It is recommended to create a new location and set the properties in the root module (main.ts).

Injecting the module into our service.

import { Inject, Injectable } from '@nestjs/common';
import type { Ii18nService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('Ii18nService')
    private readonly i18nService: Ii18nService,
  ) {}

  async hello() {
    await this.i18nService.setDriver('fs');
    await this.i18nService.addLocale('en');
    await this.i18nService.defineProperty('en', {
      "message": {
        "hello": "Hello World $1"
      },
      "erros": {
        "default": "Something went wrong 😔"
      }
    });
    return await this.i18nService.translate('message.hello', "GuilhermeSantos001");
  }
}

With dependencies

You will need to inject the following interfaces.

Interface
Service

Properties

Property
Scope
Description

private _locale

static

Current locale.

private _locales

static

All locales.

private _path

static

The path folder for locales files.

private _driver

static

The driver used for store data (fs or redis).

Methods

Method
Scope
Description

_setDefaultOptions

private

Set default options.

_initialize

private

Initialize the module and create the locales files.

_redisPrefix

private

Get the prefix for redis.

_compress

private

Compress and decompress data.

_decompress

private

Decompress data from string to object.

_redisSerializeKey

private

Serialize key for redis.

_redisSave

private

Save data in redis.

_redisGet

private

Get data from redis.

_redisDelete

private

Delete data from redis.

_writeRelativeLocalePath

private

Write the path folder for locales files.

_relativeLocalePath

private

Get the path folder for locales files.

_fileLocaleExists

private

Check if the file locale exists.

_writeFileLocale

private

Write the file locale.

_readFileLocale

private

Read the file locale.

_definePropFileLocale

private

Define a property in a locale.

_removePropFileLocale

private

Remove a property in a locale.

_extractParserKeys

private

Extract parser keys from text.

_removeParserOptions

private

Remove parser options from text.

_isNoCascade

private

Check if the value is no cascade.

_getCascadeMatchValue

private

Get the cascade match value.

_getCascadeValue

private

Get the cascade value.

_isTrim

private

Check if the value is trim.

_isUppercase

private

Check if the value is uppercase.

_isLowercase

private

Check if the value is lowercase.

_isSpace

private

Check if the value is space.

_getSpaceValue

private

Get the space value.

_isRepeat

private

Check if the value is repeat.

_getRepeatValue

private

Get the repeat value.

_parserText

private

Parser text with params.

_removeLocaleStore

private

Remove a locale from store.

setPath

public

Define path folder for locales files.

setDriver

public

Define driver used for store data.

addLocale

public

Add the current locale.

getPath

public

Returns the path folder for locales files.

getDriver

public

Returns the driver used for store data.

getLocale

public

Get the current locale.

getLocales

public

Get all locales.

resetLocales

public

Reset all locales.

removeLocale

public

Remove a locale.

defineLocale

public

Set the current locale.

defineProperty

public

Define a property in a locale.

removeProperty

public

Remove a property in a locale.

translate

public

Translate a phrase in the current locale.

Last updated