Skip to content

creolingo/locale-id

Repository files navigation

Locale ID

Parse and normalize locale ID. ICU Locale ID

NPM version build status Test coverage

Installation

Install via npm.

npm install locale-id

Support us

Star this project on GitHub.

Usage

parse

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' }

normalize

import { normalize } from 'locale-id';

const value = normalize('sk-sk');
console.log(value); // => sk_SK

getLanguage

import { getLanguage } from 'locale-id';

const value = getLanguage('sk-sk');
console.log(value); // => sk

getCountry

import { getCountry } from 'locale-id';

const value = getCountry('cs-cz');
console.log(value); // => CZ

getScript

import { getScript } from 'locale-id';

const value = getScript('sk-latin-sk');
console.log(value); // => Latin

getVariant

import { getVariant } from 'locale-id';

const value = getVariant('sr_Latn_RS_REVISED@currency=USD');
console.log(value); // => REVISED

getKeyword

import { getKeyword } from 'locale-id';

const value = getKeyword('sk-latin-sk@currency=USD');
console.log(value); // => currency=USD

normalizeAcceptLanguage

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']

getBest

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');

Running Tests

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