Skip to main content

Translator

class Translator is a main class used to format numbers, dates and strings.

Constructor arguments#

parametertype
languageString
locales(optional)Locale[] (by default [])

Methods#

formatDate#

Format value of a date. This is based on Intl.DateTimeFormat. You can find supported options on MDN.

parametertype
valueDate
options(optional)Intl.DateTimeFormatOptions

Example#

import { Translator } from '@eo-locale/core';
const date = new Date(2020, 4, 1);
const englishTranslator = new Translator('en');
console.log(englishTranslator.formatDate(date)); // 5/1/2020
const spanishTranslator = new Translator('es');
console.log(spanishTranslator.formatDate(date)); // 1/5/2020
const germanTranslator = new Translator('de');
console.log(germanTranslator.formatDate(date, {
day:"numeric",
month: "long",
weekday: "long",
year: "numeric",
})); // Freitag, 1. Mai 2020

formatNumber#

Format value of a number. This is based on Intl.NumberFormat. You can find supported options on MDN.

parametertype
valueNumber
options(optional)Intl.NumberFormatOptions

Example#

import { Translator } from '@eo-locale/core';
const number = 123456789.321;
const englishTranslator = new Translator('en');
console.log(englishTranslator.formatNumber(number)); // 123,456,789.321
const spanishTranslator = new Translator('es');
console.log(spanishTranslator.formatNumber(number)); // 123.456.789,321
const germanTranslator = new Translator('de');
console.log(germanTranslator.formatNumber(number, { style: 'currency', currency: 'EUR' })); // 123.456.789,32 €

translate#

Get a message paired with the ID and format that message. It can replace {key} inside the message with values and support plural formatting.

parametertype
idString
options(optional)FormatMessageOptions (by default {})

Example#

import { Translator } from '@eo-locale/core';
const locales = [
{
language: 'en',
messages: {
hello: 'Hello, {name}!'
}
},
{
language: 'es',
messages: {
hello: 'Hola, {name}!'
}
}
];
const englishTranslator = new Translator('en', locales);
console.log(englishTranslator.translate('hello', { name: 'World'})); // Hello, World!
const spanishTranslator = new Translator('es', locales);
console.log(spanishTranslator.translate('hello', { name: 'World'})); // Hola, World!
const germanTranslator = new Translator('de', locales);
// console.log(germanTranslator.translate('hello', { name: 'World'})); // Error: [eo-locale] id missing "hello"
console.log(germanTranslator.translate('hello', { defaultMessage: 'Hello {name}!', name: 'World'})); // Hello World!
console.log(germanTranslator.translate('hello', { name: 'User'})); // Hello User! (Memorized message template)

getMessageById#

Get a raw message paired with the ID.

parametertype
idString
defaultMessage(optional)String

Example#

import { Translator } from '@eo-locale/core';
const locales = [
{
language: 'en',
messages: {
hello: 'Hello, {name}!'
}
},
{
language: 'es',
messages: {
hello: 'Hola, {name}!'
}
}
];
const englishTranslator = new Translator('en', locales);
console.log(englishTranslator.getMessageById('hello')); // Hello, {name}!
const spanishTranslator = new Translator('es', locales);
console.log(spanishTranslator.getMessageById('hello')); //Hola, {name}!
const germanTranslator = new Translator('de', locales);
// console.log(germanTranslator.getMessageById('hello')); // Error: [eo-locale] id missing "hello"
console.log(germanTranslator.getMessageById('hello','Hello!')); // Hello!
console.log(germanTranslator.getMessageById('hello')); // Hello! (Memorized message)