Parse and normalize locale ID. ICU Locale ID
Install via npm.
npm install locale-id
Star this project on GitHub.
import parse from 'locale-id';
const locale = parse('sk-sk');
console.log(result); // => { language: 'sk', country: 'SK' }
const value = parse('sk_Latin_SK');
console.log(value); // => { language: 'sk', script: 'Latin', country: 'SK' }
import { normalize } from 'locale-id';
const value = normalize('sk-sk');
console.log(value); // => sk_SK
import { getLanguage } from 'locale-id';
const value = getLanguage('sk-sk');
console.log(value); // => sk
import { getCountry } from 'locale-id';
const value = getCountry('cs-cz');
console.log(value); // => CZ
import { getScript } from 'locale-id';
const value = getScript('sk-latin-sk');
console.log(value); // => Latin
import { getVariant } from 'locale-id';
const value = getVariant('sr_Latn_RS_REVISED@currency=USD');
console.log(value); // => REVISED
import { getKeyword } from 'locale-id';
const value = getKeyword('sk-latin-sk@currency=USD');
console.log(value); // => currency=USD
import { normalizeAcceptLanguage } from 'locale-id';
const value = normalizeAcceptLanguage('da, en-gb;q=0.8, en;q=0.7');
console.log(value); // => ['da', 'en_GB', 'en']
import { getBest } from 'locale-id';
getBest(['en', 'en_US', 'en_UK', 'sk_SK'], 'en-uk').should.equal('en_UK');
getBest(['en', 'en_US', 'en_UK', 'sk_SK'], 'en-br').should.equal('en');
getBest(['en', 'en_US', 'en_UK', 'sk_SK'], 'sk-SK').should.equal('sk_SK');
should(getBest(['en', 'en_US', 'en_UK', 'sk_SK'], 'sk-md')).equal(void 0);
should(getBest(['en', 'en_US', 'en_UK', 'sk_SK'], 'sk-md', 'en')).equal('en');
should(getBest(['en', 'en_US', 'en_UK', 'sk_SK'], 'sk-md', 'en', true)).equal('sk_SK');
should(getBest(['en_US', 'en_UK', 'sk_SK'], 'en-US', 'sk_SK', true)).equal('en_US');
should(getBest(['en_US', 'en_UK', 'sk_SK'], 'en', 'sk_SK', true)).equal('en_US');
You can speed up getBest function:
import { getBest, prepareSupported } from 'locale-id';
const supported = prepareSupported(['en', 'en_US', 'en_UK', 'sk_SK']);
getBest(supported, 'en-uk').should.equal('en_UK');
To run the test suite, first invoke the following command within the repo, installing the development dependencies:
npm install
Then run the tests:
npm test