
The Localization class is used to localize anything that can be localized.
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);
})();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);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;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'));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);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' }));
})();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' }));