Git Product home page Git Product logo

i18n-nationality's Introduction

CircleCI npm version GitHub All Releases Known Vulnerabilities GitHub issues License

i18n-nationality

Nationalities as adjectives mapped to Country codes. This repository is heavily based on the i18n-iso-countries repository by michaelwittig. https://github.com/michaelwittig/node-i18n-iso-countries

i18n for nationalities of ISO 3166-1 country codes. We support Alpha-2, Alpha-3 and Numeric codes from http://en.wikipedia.org/wiki/ISO_3166-1#Officially_assigned_code_elements

Installing

Install it using npm: npm install i18n-nationality

var nationalities = require("i18n-nationality");

If you use i18n-nationality with Node.js your are done. If you use the package in a browser environment you also have to register the languages you want to use to minimize file size.

// Support german & english languages.
nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
nationalities.registerLocale(require("i18n-nationality/langs/de.json"));

Code to Nationality

Get the name of a nationality by its ISO 3166-1 Alpha-2, Alpha-3 or Numeric code

var nationalities = require("i18n-nationality");
// in a browser environment: countries.registerLocale(require("i18n-nationality/langs/en.json"));
console.log("US (Alpha-2) => " + nationalities.getName("US", "en")); // American
console.log("US (Alpha-2) => " + nationalities.getName("US", "de")); // Amerikanisch
console.log("USA (Alpha-3) => " + nationalities.getName("USA", "en")); // American
console.log("USA (Numeric) => " + nationalities.getName("840", "en")); // American

Get all names by their ISO 3166-1 Alpha-2 code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.getNames("en")); // { 'AF': 'Afghan', 'AL': 'Albanian', [...], 'ZM': 'Zambian', 'ZW': 'Zimbabwean' }

Supported languages (ISO 639-1)

  • de: German
  • en: English
  • fr: French
  • no: Norwegian
  • it: Italian
  • fi: Finnish
  • nl: Dutch
  • es: Español

List of ISO 639-1 codes

Nationality to Code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log("American => " + nationalities.getAlpha2Code('American', 'en'));
// American => US

console.log("American => " + nationalities.getAlpha3Code('American', 'en'));
// American => USA

Codes

Convert Alpha-3 to Alpha-2 code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log("USA (Alpha-3) => " + nationalities.alpha3ToAlpha2("USA") + " (Alpha-2)");
// USA (Alpha-3) => US (Alpha-2)

Convert Numeric to Alpha-2 code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log("840 (Numeric) => " + nationalities.numericToAlpha2("840") + " (Alpha-2)");
// 840 (Numeric) => US (Alpha-2)

Convert Alpha-2 to Alpha-3 code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log("DE (Alpha-2) => " + nationalities.alpha2ToAlpha3("DE") + " (Alpha-3)");
// DE (Alpha-2) => DEU (Alpha-3)

Convert Numeric to Alpha-3 code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log("840 (Numeric) => " + nationalities.numericToAlpha3("840") + " (Alpha-3)");
// 840 (Numeric) => USA (Alpha-3)

Convert Alpha-3 to Numeric code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.alpha3ToNumeric("SWE"));
// 752

Convert Alpha-2 to Numeric code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.alpha2ToNumeric("SE"));
// 752

Get all Alpha-2 codes

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.getAlpha2Codes());
// { 'AF': 'AFG', 'AX': 'ALA', [...], 'ZM': 'ZMB', 'ZW': 'ZWE' }

Get all Alpha-3 codes

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.getAlpha3Codes());
// { 'AFG': 'AF', 'ALA': 'AX', [...], 'ZMB': 'ZM', 'ZWE': 'ZW' }

Get all Numeric codes

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.getNumericCodes());
// { '004': 'AF', '008': 'AL', [...], '887': 'YE', '894': 'ZM' }

Validate country code

var nationalities = require("i18n-nationality");
// in a browser environment: nationalities.registerLocale(require("i18n-nationality/langs/en.json"));
console.log(nationalities.isValid("US"), nationalities.isValid("USA"), nationalities.isValid("XX")));
// true, true, false

Contribution

To add a language:

  • add a json file under langs/
  • add the language to the data object in entry-node.js at the top
  • add language to section Supported languages in README.md
  • run npm install && make test to make sure that tests are passing
  • open a PR on GitHub

You can check codes here: https://www.iso.org/obp/ui/#home

i18n-nationality's People

Contributors

ansaroll-ephatta avatar aronhoyer avatar causztic avatar chris-jamieson avatar damngamerz avatar dependabot[bot] avatar jbaranguan avatar json-nan avatar lucinity-atli avatar nicolasigot avatar richardwem avatar rrdlpl avatar sirtawast avatar sourcecode911 avatar valstu avatar yoannma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

i18n-nationality's Issues

Spanish as a supported language

Hello, I'm not seeing the Spanish support when I install the library, but I do see it on the master branch. Is the npm library not synced with the code? And if that's the case, any idea when we can see it updated?

Typescript program return 'undefined'

Hi there,
i tried to modify the code to run in typescript

import { Component, VERSION } from "@angular/core";
import * as nationalities from "i18n-nationality";
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular " + VERSION.major;

  ngOnInit() {
    console.log(nationalities.getName("US", "en")); 

  }
}

Result:
"undefined"

There ain't any error in my program. Any clue?

Support for plural, gender alternatives adjectives / names

In some languages — like mine, french — adjective could be plural or according to gender.

I'm men « français » my wife is « française » but my daughters are « françaises » but we both are « français »
masculin pluriel for « français » is unvariable.

but it's quite difficult to globalize to other languages ?

.git folder included

When installing i18n-nationality a .git folder is included in node_modules/i18n-nationality which causes this error when installing or uninstalling other modules.

npm ERR! code EISGIT
npm ERR! path [...]/node_modules/i18n-nationality
npm ERR! git [...]/node_modules/i18n-nationality: Appears to be a git repo or submodule.
npm ERR! git     [...]/node_modules/i18n-nationality
npm ERR! git Refusing to remove it. Update manually,
npm ERR! git or move it out of the way first.

If I remove the .git folder I don't get this error.

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.