logo

Localization

The Localization class is used to localize anything that can be localized.

  • The language will persist in the storage, by default it will be stored in memory.
  • The path of the localized string supports dot notation, for example: ‘path.to.the.value’.
  • The variables support dot notation as well and can be used inside the localized string, for example: ‘Hello {name}!’.
  • The instance also supports default variables, which can be overridden by the variables passed to the get method.

Properties

  • language (string): The language that will be used to localize.
  • packs (LocalizationPack[]): The packs that will be used to localize.
  • storage (Storage): The storage that will be used to store the language.
  • storageKey (string): The key that will be used to store the language in the storage.
  • variables (LocalizationVariables): The default variables that will be used to localize.

Initialization

The initialize method is used to retrieve the language from the storage and set it to the instance.

import { Localization } from '@aracna/core';

const localization = new Localization('en');

(async () => {
  localization.setLanguage('it');
  await localization.store();
  localization.setLanguage('en');
  await localization.initialize();

  // will log "it"
  console.log(localization.language);
})();

Manage packs

The push method is used to add localization packs to the instance. If a pack for the language already exists, the data will be merged.

import { Localization } from '@aracna/core';

const localization = new Localization('en', [
  { data: { hello: 'Hello' }, language: 'en' }
]);

localization.push({ data: { hello: 'Ciao' }, language: 'it' });

// will log both packs
console.log(localization.packs);

Get localized string

The get method is used to retrieve the localized string from the pack. Optionally, you can pass variables that will be used to replace the variables inside the localized string.

import { Localization } from '@aracna/core';

const localization = new Localization('en', [
  {
    data: { hello: 'Hello', hello_with_name: 'Hello {name}' },
    language: 'en'
  },
  {
    data: { hello: 'Ciao', hello_with_name: 'Ciao {name}' },
    language: 'it'
  }
]);

// will log "Hello"
console.log(localization.get('hello'));

// will log "Ciao"
console.log(localization.get('it', 'hello'));

// will log "Hello John"
console.log(localization.get('hello_with_name', { name: 'John' }));

// will log "Ciao Mario"
console.log(localization.get('it', 'hello_with_name', { name: 'Mario' }));

localization.setLanguage('it');

// will log "Ciao"
console.log(localization.get('hello'));

// will log "Ciao Mario"
console.log(localization.get('hello_with_name', { name: 'Mario' }));
x;

Check if a localized string exists

The has method is used to check if the localization pack has the specified path.

import { Localization } from '@aracna/core';

const localization = new Localization('en', [
  { data: { hello: 'Hello' }, language: 'en' },
  { data: { hello: 'Ciao' }, language: 'it' }
]);

// will log true
console.log(localization.has('hello'));

// will log true
console.log(localization.has('it', 'hello'));

localization.setLanguage('it');

// will log true
console.log(localization.has('hello'));

Set the language

The setLanguage method is used to set the language that will be used for localization.

import { Localization } from '@aracna/core';

const localization = new Localization('en');

// will log "en"
console.log(localization.language);

localization.setLanguage('it');

// will log "it"
console.log(localization.language);

Store the language

The store method is used to persist the current language in the storage.

import { Localization } from '@aracna/core';
import { LocalStorage } from '@aracna/web';

const localization = new Localization(
  'en',
  [
    { data: { hello: 'Hello {name}' }, language: 'en' },
    { data: { hello: 'Ciao {name}' }, language: 'it' }
  ],
  LocalStorage
);

(async () => {
  // will log "Hello John"
  console.log(localization.get('hello', { name: 'John' }));

  await localization.storeLanguage('it');

  // will log "Ciao Mario"
  console.log(localization.get('hello', { name: 'Mario' }));

  localization.setLanguage('en');

  // will log "Hello John"
  console.log(localization.get('hello', { name: 'John' }));

  await localization.initialize();

  // will log "Hello John"
  console.log(localization.get('hello', { name: 'John' }));
})();

Set default variables

The setVariables method is used to set the default variables that will be used for localization.

import { Localization } from '@aracna/core';

const localization = new Localization(
  'en',
  [{ data: { hello: 'Hello {name}' }, language: 'en' }],
  { name: 'John' }
);

// will log "Hello John"
console.log(localization.get('hello'));

localization.setVariables({ name: 'Mark' });

// will log "Hello Mark"
console.log(localization.get('hello'));

// will log "Hello Paul"
console.log(localization.get('hello', { name: 'Paul' }));