Giter Site home page Giter Site logo

muratgozel / locale-util Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 5.86 MB

Unicode CLDR data parser and module, regularly updated based on unicode.org releases.

Home Page: https://muratgozel.github.io/locale-util/

License: MIT License

JavaScript 39.39% TypeScript 60.61%
cldr-json cldr locale-gen locale i18n localization phone-number country-codes languages timezones

locale-util's Introduction

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

locale-util's People

Contributors

muratgozel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

locale-util's Issues

Version 3.1.1 broken with CJS require()

Version 3.1.0 worked fine, but version 3.1.1 broke CJS require() imports. Sample script:

$ npm i [email protected]
$ node -e "const { findCountryTimezones } = require('locale-util')"
node:internal/modules/cjs/loader:553
      throw e;
      ^

Error: Cannot find module 'node_modules/locale-util/dist/locale-util.js'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1070:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1063:15)
    at resolveExports (node:internal/modules/cjs/loader:547:14)
    at Module._findPath (node:internal/modules/cjs/loader:621:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1034:27)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:119:18)
    at [eval]:1:34
    at Script.runInThisContext (node:vm:122:12) {
  code: 'MODULE_NOT_FOUND',
  path: 'node_modules/locale-util/package.json'
}

Node.js v20.4.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.