meikidd / locale-code

locale code based on ISO-639-1 and ISO-3166-1
Other
36 stars 5 forks source link

locale-code

NPM Version Build Status Download Count

Simple interface for ISO-639-1 language codes and ISO-3166-1-alpha-2 country codes.

Install

npm install locale-code

Methods

getLanguageCode(code)

Lookup language code from locale code

getLanguageName(code)

Lookup language english name by code

getLanguageNativeName(code)

Lookup language native name by code

validateLanguageCode(code)

Check whether the given code is in the list of ISO-639-1

getLanguages(codes)

Get the array of the language objects by the given codes

getCountryCode(code)

Lookup country code from locale code

getCountryName(code)

Lookup country name by code

validateCountryCode(code)

Check whether the given code is in the list of ISO-3166-1-alpha-2

validate(code)

Check whether the given code is in the list of ISO-639-1 and ISO-3166-1-alpha-2

Usage

var  LocaleCode = require('locale-code')

console.log(LocaleCode.getLanguageCode('en-US')); // 'en'

console.log(LocaleCode.getLanguageName('zh-CN')); // 'Chinese'

console.log(LocaleCode.getLanguageNativeName('zh-CN')); // '中文'

console.log(LocaleCode.getLanguageZhName('es-ES')); // '西班牙语'

console.log(LocaleCode.validateLanguageCode('en-US')); // true

console.log(LocaleCode.validateLanguageCode('bb-US')); // false

console.log(LocaleCode.getLanguages(['en-US','zh-CN'])); 
// [{code:'en-US', name:'English', nativeName:'English', zhName:'英语'}, {code:'zh-CN', name:'Chinese', nativeName:'中文', zhName:'中文'}]

console.log(LocaleCode.getCountryCode('en-US')); // 'US'

console.log(LocaleCode.getCountryName('en-US')); // 'United States'

console.log(LocaleCode.validateCountryCode('en-US')); // true

console.log(LocaleCode.validateCountryCode('en-ZZ')); // false

console.log(LocaleCode.validate('en-US')); // true

console.log(LocaleCode.validate('en_US')); // false

console.log(LocaleCode.validate('bb-XX')); // false