OpusCapita i18n
Synopsis
Provides data <-> value converters for:
- Dates
- Numbers
- Empty values (Strip to null converter)
Provides simple i18n mechanism for JS applications/modules.
Installation
Using npm
$ npm i --save opuscapita-i18n
Npm scripts
Linting
$ npm run lint
Runing tests
$ npm run test // with coverage
$ npm run testonly // only test resylts
Cleaning work directory (removing built docs, etc.)
$ npm run clean
Usage & API
Converters
Converter is a class that converts a value from its object representation to string one and reverse. All converters (Date, Number, StripToNull) implement the same interface that provides two methods valueToString and stringToValue.
Date Converter
import DateConverter from 'opuscapita-i18n/lib/converters/DateConverter';
let dc = new DateConverter(''MM/dd/yyyy'', 'en');
dc.valueToString(new Date(2001, 0, 15)) === '01/15/2001'
dc.stringToValue('01/15/2001').toISOString() === new Date(2001, 0, 15).toISOString()
NumberConverter
Format definition is similar to Java's DecimalFormat class, but exponent is not supported
import NumberConverter from 'opuscapita-i18n/lib/converters/NumberConverter';
let nc = new NumberConverter('#,##0.00', ',', '.');
nc.valueToString(10000000) === '10,000,000.00'
nc.stringToValue('10,000.00') === 10000
Strip to null converter
import StripToNullConverter from 'opuscapita-i18n/lib/converters/StripToNullConverter';
let stnc = new StripToNullConverter();
stnc.valueToString(null) === ''
converter.stringToValue('') === null
I18nManager
Provides mechanism for internationalization according to the locale (with fallback), passed in the constructor.
Also provides facade function for operating with converters, according to format patterns.
import I18nManager from 'opuscapita-i18n/utils/I18nManager';
const formatInfos = {
'en': {
datePattern: 'dd/MM/yyyy',
dateTimePattern: 'dd/MM/yyyy HH:mm:ss',
integerPattern: '#,##0',
numberPattern: '#,##0.00#######',
numberDecimalSeparator: '.',
numberGroupingSeparator: ',',
numberGroupingSeparatorUse: true,
}
};
let i18n = = new I18nManager('en', [{
locales: ['en'],
messages: {
test: 'test',
format: 'min={min}, max={max}',
subcomponent: {
hint: 'nested hint'
}
},
}], formatInfos);
i18n.getMessage('test') === 'test'
i18n.getMessage('format', { min: 10, max: 100 }) === 'min=10, max=100'
i18n = new I18nManager('de-DE', [], {});
i18n.register('test_component', [
{
locales: ['en'],
messages: {
component: {
testMessage1: 'en test message 1',
testMessage2: 'en test message 2',
},
},
},
{
locales: ['de'],
messages: {
component: {
testMessage2: 'de test message 2',
},
},
},
]);
i18n.getMessage('component.testMessage1') === 'en test message 1'
i18n.getMessage('component.testMessage2') === 'de test message 2'
i18n.getMessage('component.testMessage3') === 'component.testMessage3'
i18n.formatDate(new Date(2001, 0, 10)) === '10/01/2001'
i18n.parseDate('10/01/2001').toISOString() === new Date(2001, 0, 10).toISOString()
i18n.formatDateTime(new Date(2001, 0, 10)) === '10/01/2001 00:00:00'
i18n.formatNumber(10000) === '10,000'
i18n.parseNumber('10,000') === 10000
i18n.formatDecimalNumber(10000) === '10,000.00'
i18n.parseDecimalNumber('10,000.00') === 10000
i18n.dateFormat === 'YY'
Contributors
License
OpusCapita i18n is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
v1.0.12 (Wed, 05 Apr 2017 13:09:00 GMT)
-
Freeze dependency version (Kirill Volkovich volkovich@scand.com, ea90d02)
-
Add babel-plugin-lodash
It should reduce size of bundle and transpiled components (Kirill Volkovich volkovich@scand.com, 58d53ef)