Giter Site home page Giter Site logo

country-language's Introduction

build status code coverage code style styled with prettier made with lass license

Maintained fork of country-language with zero-dependencies. Query any country's spoken languages or countries where a language is spoken.

Table of Contents

Install

npm:

npm install @ladjs/country-language

Usage

Once you require @ladjs/country-language, the following API will be available.

const CountryLanguage = require('@ladjs/country-language');

.getLanguageCodes (languageCodeType, cb)

  • @param {String} language code type. Acceptable values: 1, 2 or 3.
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} array String with language codes

Acceptable language code type parameter values: 1, 2, 3 for returning ISO-639-1, ISO-639-2, ISO-639-3 codes respectively. If not provided, ISO-639-1 codes will be returned.

const allLanguageCodes = CountryLanguage.getLanguageCodes(2);

.getCountryCodes (countryCodeType, cb)

  • @param {String} country code type. Acceptable values: 1, 2 or 3.
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} array String with country codes

Acceptable country code type parameter values: 1, 2, 3 for returning numerical code, alpha-2, alpha-3 codes respectively. If not provided, alpha-2 codes will be returned.

const allCountryCodes = CountryLanguage.getCountryCodes(2);

.languageCodeExists (languageCode)

  • @param {String} language code to check.

Returns Boolean indicating language existance. Language code parameter can be either a ISO-639-1, ISO-639-2 or ISO-639-3 code.

const languageExists = CountryLanguage.languageCodeExists('en');

.countryCodeExists (countryCode)

  • @param {String} country code to check.

Returns Boolean indicating country existance. Country code parameter can be either an alpha-2, alpha-3 or numerical code.

const countryExists = CountryLanguage.countryCodeExists('GB');

.getCountry (code, cb)

  • @param {String} country code
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object containing country info

Country code can be either an Alpha-2 or Alpha-3 code. The returned object includes the following info:

  • code_2: Country alpha-2 code (2 letters)
  • code_3: Country alpha-3 code (3 letters)
  • numCode: Country numeric code
  • name: Country name
  • languages: Array of language objects for each language spoken in the country
  • langCultureMs: Array of language cultures for the country supported by Microsoft©

Each language object in languages property includes the following info:

  • iso639_1: language iso639-1 code (2 letters)
  • iso639_2: language iso639-2 code (3 letters)
  • iso639_2en: language iso639-2 code with some codes derived from English names rather than native names of languages (3 letters)
  • iso639_3: language iso639-3 code (3 letters)
  • name: String array with one or more language names (in English)
  • nativeName: String array with one or more language names (in native language)
  • direction: Language script direction (either "LTR" or "RTL") - Left-to-Right, Right-to-Left
  • family: language family
  • countries: Array of country objects where this language is spoken

Each Microsoft© language culture object in langCultureMs property icludes the following info:

  • langCultureName: language culture name
  • displayName: language culture dispaly name
  • cultureCode: language culture code
CountryLanguage.getCountry('GB', function (err, country) {
  if (err) {
    console.log(err);
  } else {
    const languagesInGB = country.languages;
  }
});

.getLanguage (code, cb)

  • @param {String} language code
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object containing language info

Language code can be either iso639-1, iso639-2, iso639-2en or iso639-3 code. Contents of the returned language object are described in .getCountry method.

CountryLanguage.getLanguage('en', function (err, language) {
  if (err) {
    console.log(err);
  } else {
    const countriesSpeakingEN = language.countries;
  }
});

.getCountryLanguages (code, cb)

  • @param {String} country code
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object array containing country languages info

Country code can be either an Alpha-2 or Alpha-3 code. Each language object contains the following info:

  • iso639_1: language iso639-1 code (2 letters)
  • iso639_2: language iso639-2 code with some codes derived from English names rather than native names of languages (3 letters)
  • iso639_3: language iso639-3 code (3 letters)
CountryLanguage.getCountryLanguages('GB', function (err, languages) {
  if (err) {
    console.log(err);
  } else {
    languages.forEach(function (languageCodes) {
      console.log(languageCodes.iso639_1);
    });
  }
});

.getLanguageCountries (code, cb)

  • @param {String} language code
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object array containing country info

Language code can be either iso639-1, iso639-2, iso639-2en or iso639-3 code. Each Country object contains the following info:

  • code_2: Country alpha-2 code (2 letters)
  • code_3: Country alpha-3 code (3 letters)
  • numCode: Country numeric code
CountryLanguage.getLanguageCountries('en', function (err, countries) {
  if (err) {
    console.log(err);
  } else {
    countries.forEach(function (countryCodes) {
      console.log(countryCodes.code_3);
    });
  }
});

.getCountryMsLocales (code, cb)

  • @param {String} country code
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object array containing Language Cultures info for the country

Country code can be either an Alpha-2 or Alpha-3 code. Contents of each Language Culture object are described in .getCountry method.

CountryLanguage.getCountryMsLocales('GB', function (err, locales) {
  if (err) {
    console.log(err);
  } else {
    locales.forEach(function (locale) {
      console.log(locale.langCultureName);
    });
  }
});

.getLanguageMsLocales (code, cb)

  • @param {String} language code
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object array containing Language Cultures info for the language

Language code can be either iso639-1, iso639-2, iso639-2en or iso639-3 code. Contents of each Language Culture object are described in .getCountry method.

CountryLanguage.getLanguageMsLocales('en', function (err, locales) {
  if (err) {
    console.log(err);
  } else {
    locales.forEach(function (locale) {
      console.log(locale.langCultureName);
    });
  }
});

.getCountries ()

Returns an array object with info for every country in the world having an ISO 3166 code. Contents of each country object in the array is described in .getCountry method.

const allCountries = CountryLanguage.getCountries();

.getLanguages ()

Returns an array object with info for every language in the world having an ISO 639-2 code (and a few more). Contents of each language object in the array is described in .getCountry method.

const allLanguages = CountryLanguage.getLanguages();

.getLanguageFamilies ()

Returns an array of strings with the names of each language family.

const allLanguageFamilies = CountryLanguage.getLanguageFamilies();

.getLocales (mode)

  • @param {Boolean} locale symbols mode

Returns an array of strings with all locale codes. If mode ommited or false, locales with 3 parts will be returned like: az-Cyrl-AZ

const localesSymbols = CountryLanguage.getLocales();

If mode is set to true, they will be returned like: az-AZ-Cyrl

const localesSymbols = CountryLanguage.getLocales(true);

.getLanguageFamilyMembers (family, cb)

Returns an array object with info for every language in the world having an ISO 639-2 code (and a few more). Contents of each language object in the array is described in .getCountry method.

  • @param {String} language family name (
  • @param {Function} callback on complete or error
  • @cb {Error|null} if error
  • @cb {Object} object array containing languages info for each language member in the family.

Contents of the returned language object are described in .getCountry method.

CountryLanguage.getLanguageFamilyMembers('Indo-European', function (err, languages) {
  if (err) {
    console.log(err);
  } else {
    languages.forEach(function (language) {
      console.log(language.name);
    });
  }
});

## Notes

For the following methods:

  • .getLanguageCodes
  • .getCountryCodes
  • .getCountry
  • .getLanguage
  • .getCountryLanguages
  • .getLanguageCountries
  • .getCountryMsLocales
  • .getLanguageMsLocales
  • .getLanguageFamilyMembers

the cb parameter is optional. When not provided, each method returns either an Object when there is no error, or a String in case of an error.

Any input parameter (country code, language code, language family name) is case insensitive.

Language#nativeName string is not displayed correclty on the console for Right-to-Left (RTL) languages. However, there is no issue on string rendering (either on the browser or any text editor).

Contributors

Name
titanism

License

MIT © Tassos Diamantidis

country-language's People

Contributors

alatzidis avatar joebowbeer avatar niftylettuce avatar pauliusuza avatar tassosd avatar teyou avatar titanism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

country-language's Issues

[fix] langs is not iterable

Describe the bug

Node.js version: v18.12.1

OS version: OSX Venture 13.1

Description:
TypeError exception triggered on some countries without languages array within countries object in data.json

Actual behavior

Exception triggered

      for (const l of langs) {
                      ^

TypeError: langs is not iterable

Expected behavior

should just return [] // empty array

Code to reproduce

CountryLanguage.getCountryLanguages("PS")

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Add support for typescript

Describe the feature

I cannot fined types for this package. I want to use it in a typescript website. Any idea where i can find the types?

This is the error i get:

   djs/country-language'. 'D:/deploy/node_modules/@ladjs/country-language/index.js' implicitly has an 'any' type.
   Try `npm i --save-dev @types/ladjs__country-language` if it exists or add a new declaration (.d.ts) file containing `declare module 
  '@ladjs/country-language';`

Checklist

✅ I have searched through GitHub issues for similar issues.
✅ I have completely read through the README and documentation.

[fix] Dash confusion in data.json

Describe the bug

vscode displays numerous warnings regarding the presence of en dashes in place of hyphens

The character U+2013 "–" could be confused with the character U+002d "-", which is more common in source code

Code to reproduce

https://github.dev/ladjs/country-language/blob/master/data.json

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

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.