@universal-packages/localization v1.4.2
Localization
Dynamic localization with fallbacks and variable replacement.
Install
npm install @universal-packages/localizationLocalization
Localization class is the high level representation of the localization object, it provides tools to internationalize your app.
import { Localization } from '@universal-packages/localization'
const localization = new Localization()
console.log(localization.translate('hello'))
//> Hello
console.log(localization.translate('world'))
//> World
console.log(localization.translate('name.hello', null, { name: 'John', emoji: 'π' }))
//> Hello John π
console.log(localization.translate('hello', 'en-US'))
//> Howdy
console.log(localization.translate('world', 'en-US'))
//> World
console.log(localization.translate('name.hello', 'en-US', { name: 'John', emoji: 'π' }))
//> Howdy John π
console.log(localization.translate('hello', 'es'))
//> Hola
console.log(localization.translate('world', 'es'))
//> Mundo
console.log(localization.translate('name.hello', 'es', { name: 'Juan', emoji: 'π' }))
//> Hola Juan π
console.log(localization.translate('hello', 'es-MX'))
//> Que onda
console.log(localization.translate('world', 'es-MX'))
//> Mundo
console.log(localization.translate('name.hello', 'es-MX', { name: 'Juanito', emoji: 'π' }))
//> Que onda Juanito πOptions
dictionaryLocalizationDictionaryThe dictionary to use for the localization, in case you aleady have a dictionary, you can pass it here.useFileNamebooleandefault: trueIf you want your translations to be namespaced by the name of the file they are in, you can set this to true.localizationsLocationstringdefault: './src'The path to the folder where the localizations are located. Files can bejson,yaml,js,tsand can be nested deeply in the folder structure and should be prefixed with the locale they are for alongsidelocal- src |- general.en-US.local.yaml |- general.es-MX.local.yaml |- mailers |- welcome.en-US.local.yaml |- welcome.es-MX.local.yaml |- pages |- home.en-US.local.yaml |- home.es-MX.local.yamldefaultLocaleLocaledefault: 'enThe default locale to use when no locale is provided.
Instance methods
.translate(subject: string | string[], locale: string, [locales: Object])
subjectstring | string[]The path to the translation you want to get, Use dot notation to access nested translations or an array of string.localestringA valid locale string, it will be used to look for the translation in the dictionary.localesObjectIf the target translation needs variables to be replaced, you can pass them as an object here. use{{ <variableName> }}in the translation to indicate where the variable should be placed.
Looks for a translation in the current locale, if it doesnβt find it, it will look for it in the fallback locale, if it doesnβt find it there either, it will return the subject.
Events
Warning and errors related to missing dictionaries and translations are emitted as events.
const localization = new Localization()
localization.on('waring', (event) => console.log(event))
localization.on('error', (event) => console.log(event))Typescript
This library is developed in TypeScript and shipped fully typed.
Contributing
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
License
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago