locale-util
TypeScript icon, indicating that this package has built-in type declarations

4.2.1 • Public • Published

locale-util

Unicode CLDR data parser and module, regularly updated based on https://github.com/unicode-org/cldr releases. Refer to https://muratgozel.github.io/locale-util/ for full api docs.

Installation

npm install locale-util

Usage

The package contains large amount of data which is not suitable for browser environment. Benefit from tree-shaking might work if you are interested in small chunks of it.

Here are the methods that you can use to interact with the data:

import {isCountryCode, findCountry, findCallingCode, findCountryLanguages, 
    isCurrencyCode, findCurrency, isLanguageCode, findLanguage, 
    findCountryTimezones, findTimezoneOffset, findTerritories, findCountryTerritory
} from 'locale-util'

isCountryCode('TR') // true
isCountryCode('XX') // false

findCountry('TR') /*
{
    'code': 'TR',
    'englishName': 'Türkiye',
    'nativeName': 'Türkiye'
}
*/
findCountry('ABC') // undefined

findCountryCallingCode('US') // 1
findCountryCallingCode('TR') // 90
findCountryCallingCode(null) // undefined

findCountryLanguages('TR') // ['tr']
findCountryLanguages('US') // ['en', 'es', 'haw']

isCurrencyCode('TRY') // true

findCurrency('TRY') /*
{
    'code': 'TRY',
    'num': 949,
    'englishName': 'Turkish',
    'nativeName': 'Türkçe'
}
*/

findCountryCurrencyCode('TR') // TRY
findCountryCurrencyCode('US') // USD

isLanguageCode('xxx') // false
isLanguageCode('tr') // true

findLanguage('tr') /*
{
    'code': 'tr',
    'nativeName': 'Türkçe',
    'englishName': 'Turkish'
}
*/

findCountryTimezones('TR') /*
[{
    'name': 'Europe/Istanbul',
    'offset': -180,
    'country': 'TR'
}]
*/

findCountryTimezones('TT') /*
[{
    'name': 'America/Port_of_Spain',
    'offset': 240,
    'country': 'TT'
}, {
    'name': 'America/Puerto_Rico',
    'offset': 240,
    'country': 'TT'
}]
*/

findTimezoneOffset('America/Puerto_Rico') // 240
findTimezoneOffset('Europe/Istanbul') // -180

findTerritories() /*
[
    {
        "code": "005",
        "name": "South America"
    },
    {
        "code": "011",
        "name": "Western Africa"
    },
    ...
    ...
]
*/

findCountryTerritory('TR') // { code: '145', name: 'Western Asia' }
findCountryTerritory('TR') // { code: '021', name: 'Northern America' }

Have a look at the tests, types and source for more info.

Keeping Data Up To Date

Data updates published regularly as minor releases so you only need to update the package as new version comes in. If you have a kind of manual setup, download the release you wish from https://github.com/unicode-org/cldr/releases and copy the common directory under cldr-data-common. Then run npm run setup, compile, build and test to parse the new data.

Contributing

If you're interested in contributing, read the CONTRIBUTING.md first, please.


Version management of this repository done by releaser 🚀


Thanks for watching 🐬

Support me on Patreon

Package Sidebar

Install

npm i locale-util

Weekly Downloads

1,305

Version

4.2.1

License

MIT

Unpacked Size

1.78 MB

Total Files

71

Last publish

Collaborators

  • muratgozel