> For the complete documentation index, see [llms.txt](https://guilhermesantos.gitbook.io/niro-health/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guilhermesantos.gitbook.io/niro-health/modules/i18n.md).

# i18n

The module provides a simple way to management different languages.

### Imports

{% tabs %}
{% tab title="Service" %}

```typescript
import { i18nService } from 'niro-health';
```

{% endtab %}

{% tab title="Module" %}

```typescript
import { i18nModule } from 'niro-health';
```

{% endtab %}

{% tab title="Interface" %}

```typescript
import type { Ii18nService } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Environment Variables

{% tabs %}
{% tab title="Redis" %}

<table><thead><tr><th width="260">name</th><th>description</th></tr></thead><tbody><tr><td><code>REDIS_HOST</code></td><td><code>Host of the redis.</code></td></tr><tr><td><code>REDIS_PASSWORD</code></td><td><code>Port of the redis.</code></td></tr><tr><td><code>REDIS_PORT</code></td><td><code>Password of the redis.</code></td></tr></tbody></table>

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

{% code title=".env" %}

```bash
REDIS_HOST=redis://localhost:6379
REDIS_PORT=6379
REDIS_PASSWORD=
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Detailed Section

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

{% content-ref url="/pages/2iD0l42Qv5fEhAxKaOho" %}
[Starting](/niro-health/locale-i18n/starting.md)
{% endcontent-ref %}

### Method of Use

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

{% code overflow="wrap" %}

```typescript
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 {}
```

{% endcode %}

{% hint style="info" %}
Before use you do added new locale and define properties from language.
{% endhint %}

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

Injecting the module into our service.

```typescript
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.

<table><thead><tr><th width="308">Interface</th><th>Service</th></tr></thead><tbody><tr><td><code>IConfigurationService</code></td><td><a href="/niro-health/modules/configuration.md">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="/niro-health/modules/validator-regexp.md">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="/niro-health/modules/stringex.md">Link to Service documentation</a></td></tr><tr><td><code>IRedisService</code></td><td><a href="/niro-health/modules/core/redis.md">Link to Service documentation</a></td></tr><tr><td><code>IDebugService</code></td><td><a href="/niro-health/modules/debug.md">Link to Service documentation</a></td></tr><tr><td><code>IPropStringService</code></td><td><a href="/niro-health/modules/prop-string.md">Link to Service documentation</a></td></tr></tbody></table>

### Properties

<table><thead><tr><th width="301.3333333333333">Property</th><th width="127" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><mark style="color:blue;"><strong>private</strong></mark> <code>_locale</code></td><td align="center"><code>static</code></td><td><code>Current locale.</code></td></tr><tr><td><mark style="color:blue;"><strong>private</strong></mark> <code>_locales</code></td><td align="center"><code>static</code></td><td><code>All locales.</code></td></tr><tr><td><mark style="color:blue;"><strong>private</strong></mark> <code>_path</code></td><td align="center"><code>static</code></td><td><code>The path folder for locales files.</code></td></tr><tr><td><mark style="color:blue;"><strong>private</strong></mark> <code>_driver</code></td><td align="center"><code>static</code></td><td><code>The driver used for store data (fs or redis).</code></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="301.3333333333333">Method</th><th width="129" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>_setDefaultOptions</code></td><td align="center"><code>private</code></td><td><code>Set default options.</code></td></tr><tr><td><code>_initialize</code></td><td align="center"><code>private</code></td><td><code>Initialize the module and create the locales files.</code></td></tr><tr><td><code>_redisPrefix</code></td><td align="center"><code>private</code></td><td><code>Get the prefix for redis.</code></td></tr><tr><td><code>_compress</code></td><td align="center"><code>private</code></td><td><code>Compress and decompress data.</code></td></tr><tr><td><code>_decompress</code></td><td align="center"><code>private</code></td><td><code>Decompress data from string to object.</code></td></tr><tr><td><code>_redisSerializeKey</code></td><td align="center"><code>private</code></td><td><code>Serialize key for redis.</code></td></tr><tr><td><code>_redisSave</code></td><td align="center"><code>private</code></td><td><code>Save data in redis.</code></td></tr><tr><td><code>_redisGet</code></td><td align="center"><code>private</code></td><td><code>Get data from redis.</code></td></tr><tr><td><code>_redisDelete</code></td><td align="center"><code>private</code></td><td><code>Delete data from redis.</code></td></tr><tr><td><code>_writeRelativeLocalePath</code></td><td align="center"><code>private</code></td><td><code>Write the path folder for locales files.</code></td></tr><tr><td><code>_relativeLocalePath</code></td><td align="center"><code>private</code></td><td><code>Get the path folder for locales files.</code></td></tr><tr><td><code>_fileLocaleExists</code></td><td align="center"><code>private</code></td><td><code>Check if the file locale exists.</code></td></tr><tr><td><code>_writeFileLocale</code></td><td align="center"><code>private</code></td><td><code>Write the file locale.</code></td></tr><tr><td><code>_readFileLocale</code></td><td align="center"><code>private</code></td><td><code>Read the file locale.</code></td></tr><tr><td><code>_definePropFileLocale</code></td><td align="center"><code>private</code></td><td><code>Define a property in a locale.</code></td></tr><tr><td><code>_removePropFileLocale</code></td><td align="center"><code>private</code></td><td><code>Remove a property in a locale.</code></td></tr><tr><td><code>_extractParserKeys</code></td><td align="center"><code>private</code></td><td><code>Extract parser keys from text.</code></td></tr><tr><td><code>_removeParserOptions</code></td><td align="center"><code>private</code></td><td><code>Remove parser options from text.</code></td></tr><tr><td><code>_isNoCascade</code></td><td align="center"><code>private</code></td><td><code>Check if the value is no cascade.</code></td></tr><tr><td><code>_getCascadeMatchValue</code></td><td align="center"><code>private</code></td><td><code>Get the cascade match value.</code></td></tr><tr><td><code>_getCascadeValue</code></td><td align="center"><code>private</code></td><td><code>Get the cascade value.</code></td></tr><tr><td><code>_isTrim</code></td><td align="center"><code>private</code></td><td><code>Check if the value is trim.</code></td></tr><tr><td><code>_isUppercase</code></td><td align="center"><code>private</code></td><td><code>Check if the value is uppercase.</code></td></tr><tr><td><code>_isLowercase</code></td><td align="center"><code>private</code></td><td><code>Check if the value is lowercase.</code></td></tr><tr><td><code>_isSpace</code></td><td align="center"><code>private</code></td><td><code>Check if the value is space.</code></td></tr><tr><td><code>_getSpaceValue</code></td><td align="center"><code>private</code></td><td><code>Get the space value.</code></td></tr><tr><td><code>_isRepeat</code></td><td align="center"><code>private</code></td><td><code>Check if the value is repeat.</code></td></tr><tr><td><code>_getRepeatValue</code></td><td align="center"><code>private</code></td><td><code>Get the repeat value.</code></td></tr><tr><td><code>_parserText</code></td><td align="center"><code>private</code></td><td><code>Parser text with params.</code></td></tr><tr><td><code>_removeLocaleStore</code></td><td align="center"><code>private</code></td><td><code>Remove a locale from store.</code></td></tr><tr><td><code>setPath</code></td><td align="center"><code>public</code></td><td><code>Define path folder for locales files.</code></td></tr><tr><td><code>setDriver</code></td><td align="center"><code>public</code></td><td><code>Define driver used for store data.</code></td></tr><tr><td><code>addLocale</code></td><td align="center"><code>public</code></td><td><code>Add the current locale.</code></td></tr><tr><td><code>getPath</code></td><td align="center"><code>public</code></td><td><code>Returns the path folder for locales files.</code></td></tr><tr><td><code>getDriver</code></td><td align="center"><code>public</code></td><td><code>Returns the driver used for store data.</code></td></tr><tr><td><code>getLocale</code></td><td align="center"><code>public</code></td><td><code>Get the current locale.</code></td></tr><tr><td><code>getLocales</code></td><td align="center"><code>public</code></td><td><code>Get all locales.</code></td></tr><tr><td><code>resetLocales</code></td><td align="center"><code>public</code></td><td><code>Reset all locales.</code></td></tr><tr><td><code>removeLocale</code></td><td align="center"><code>public</code></td><td><code>Remove a locale.</code></td></tr><tr><td><code>defineLocale</code></td><td align="center"><code>public</code></td><td><code>Set the current locale.</code></td></tr><tr><td><code>defineProperty</code></td><td align="center"><code>public</code></td><td><code>Define a property in a locale.</code></td></tr><tr><td><code>removeProperty</code></td><td align="center"><code>public</code></td><td><code>Remove a property in a locale.</code></td></tr><tr><td><code>translate</code></td><td align="center"><code>public</code></td><td><code>Translate a phrase in the current locale.</code></td></tr></tbody></table>
