i18n-smart
Smart and lightweight module for localization with dynamic storage.
Features
- Easily expandable
- Easily configurable
- Pluralization
- Plugins support
- TypeScript
- React integration (optional)
!IMPOTANT!
The library always relies on the globalMap
object to handle data storage. For browsers that do not support that, you'll need to include a polyfill, such as core-js
:
;
The current version has temporarily limited pluralization support. Read docs.
Methods
-
setLocale(localeCode: string): void
- set specified locale for the storage -
setValues = (values: { [key: string]: string }, localeCode?: string): void
- set translations for the specified locale, if no locale is specified, the previously set locale is used -
setDefaultValues = (values: Value): void
- set default translations -
getValue(key: string, params?: Array<any>, cb?: void): any
- get value for interpolation -
getValues(): { [key: string]: string }
- get all data from storage -
getLocale(): string | void
- get current locale -
getValueByKey(key: string): string | undefined
- get raw value by key -
hasTranslations(localeCode: string): boolean
- check are there translations for the specified locale -
hasValue(key: string): boolean
- check is there a value in the storage -
clear(): void
- clear the data storage -
value(key: string, params: Array<any>): string
- interpolate values with specified parameters
API
configure(options: Options): Instanse
- configure(extends) instance
Types
; ;
Quick start
npm i i18n-smart
; const locale = 'en';const values = key1: 'Some text' key2: 'Some {0} for {1}'; i18n;i18nvalue'key1'; // Some texti18nvalue'key2' 'text' 'interpolation'; // Some text for interpolation
Configure example (extends with puralize plugin)
npm i i18n-smart
;; ; const values = keyPlural: one: '1 file' // singular other: '{0} files' // plural i18n;i18n; // 1 filei18n; // 2 files
Configure example (extends with react plugin)
npm i react i18n-smart
;;; ; const element = <span>text</spam>;i18n // Some text
Run tests
npm i
npm run test