michaelwittig / node-i18n-iso-countries Goto Github PK
View Code? Open in Web Editor NEWi18n for ISO 3166-1 country codes
License: MIT License
i18n for ISO 3166-1 country codes
License: MIT License
There are a few exceptional reserved codes that have been assigned and should be used to allow users to choose. I'm not sure if these should be optional or a separate module, but some transitional codes are still useful because they need to be chosen for VAT purposes etc (like the Canary Islands).
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Reserved_code_elements
I'm trying to use this module with create-react-app but I get a compilation error:
Failed to minify the code from this file:
./node_modules/i18n-iso-countries/index.js:46
Read more here: http://bit.ly/2tRViJ9
Following the link brings to this page: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-build-fails-to-minify
Open an issue on the dependency's issue tracker and ask that the package be published pre-compiled.
Any chance the module could be pre-compiled before it's published?
I think you can extend the current language code to use IETF language tags https://en.wikipedia.org/wiki/IETF_language_tag
For example
en_us = US English
en_gb = UK English
en_au = Australian English
zh_hk = Hong Kong Traditional Chinese
zh_tw = Taiwan Traditional Chinese
zh_cn = Mainland Simplified Chinese
Using this library for a form in a Vue.js application the following getNames
function call returns an empty object, however, the rest of the functions seem to work as expected.
var countries = require('i18n-iso-countries')
var codes = countries.getNames('en')
var alpha2 = countries.getAlpha2Codes()
console.log(codes) // Empty {}
console.log(alpha2)) // Works as expected
It works fine on 3.7.1 but 3.7.2 gives following compile errors:
node_modules/i18n-iso-countries/index.d.ts(11,16): error TS7010: '(Missing)', which lacks return-type annotation, implicitly has an 'any' return type.
node_modules/i18n-iso-countries/index.d.ts(11,17): error TS1003: Identifier expected.
node_modules/i18n-iso-countries/index.d.ts(11,24): error TS2552: Cannot find name 'lang'. Did you mean 'langs'?
node_modules/i18n-iso-countries/index.d.ts(11,28): error TS1005: ',' expected.
node_modules/i18n-iso-countries/index.d.ts(11,30): error TS2693: 'string' only refers to a type, but is being used as a value here.
node_modules/i18n-iso-countries/index.d.ts(11,37): error TS1005: ';' expected.
node_modules/i18n-iso-countries/index.d.ts(11,43): error TS1109: Expression expected.```
Can I know why do I receive weird characters in countries name in German language?
Südafrika
São Tomé und PrÃncipe
Südsudan
Russische Föderation
Südkorea
Französisch-Guayana
Vereinigtes Königreich Gro�britannien und Nordirland
Have anyone experienced this problem?
Hey :)
First of all, thanks for this package :) 👍
It would be nice, if kosovo is between "Kongo" and "Nordkorea" in de.json / en.json etc. for correct sort :)
Hi there,
we are using i18n-iso-countries with webpack. Maybe someone else stumbles upon this and receives the following:
2018-06-26T14:50:41.5178144Z WARNING in ./node_modules/i18n-iso-countries/index.js
2018-06-26T14:50:41.5178337Z 46:18-63 Critical dependency: the request of a dependency is an expression
2018-06-26T14:50:41.5178523Z @ ./node_modules/i18n-iso-countries/index.js
2018-06-26T14:50:41.5178679Z @ ./src/i18n/i18nCountries.js
2018-06-26T14:50:41.5178825Z @ ./src/index.jsx
2018-06-26T14:50:41.5178965Z @ multi ./src/index.jsx
2018-06-26T14:50:41.5179055Z
2018-06-26T14:50:41.5179351Z ERROR in bundle.js from UglifyJs
2018-06-26T14:50:41.5179512Z Unexpected token: name (localData) [bundle.js:29002,6]
We had now one day broken builds because of that. The reason was, that in our package.json
we allowed also minor version upgrades with "i18n-iso-countries": "^3.6.2"
. Changing it to "i18n-iso-countries": "~3.6.2" to ensure only patching is done and it stays at version 3.6.x solved it.
I actually even commented out the minification with UglifyJs, but yet the error still persists. I guess it has to do with missing ES6 transpilation or something like that in i18n-iso-countries and thus UglifyJs fails. However, I do not want to include the node_modules
folder in my ES6 transpilation process.
I am using release 3.7.1 and I am trying to use the import function, but I got the error
import is not a function
Checking the index.d.ts looks like the mapping to that function is missing
export type LocalizedCountryNames = {
[alpha2Key: string]: string
};
export type LocaleData = {
locale: string,
countries: LocalizedCountryNames
};
export function registerLocale(localeData: LocaleData): void;
export function alpha2ToAlpha3(alpha2: string): string;
export function alpha2ToNumeric(alpha2: string): number;
export function alpha3ToAlpha2(alpha3: string): string;
export function alpha3ToNumeric(alpha3: string): number;
export function numericToAlpha2(numeric: number | string): string;
export function numericToAlpha3(numeric: number | string): string;
/**
* Returns object map where key is alpha 2 code and value is alpha 3 code
*/
export function getAlpha2Codes(): { [alpha2Key: string]: string };
/**
* Returns object map where key is alpha 3 code and value is alpha 2 code
*/
export function getAlpha3Codes(): { [alpha3Key: string]: string };
/**
* Returns object map where key is numeric code and value is alpha 2 code
*/
export function getNumericCodes(): { [numericKey: number]: string };
export function getName(alpha2orAlpha3orNumeric: string | number, lang: string): string;
export function getNames(lang: string): LocalizedCountryNames;
export function toAlpha3(alpha2orNumeric: number | string): string;
export function toAlpha2(alpha3orNumeric: number | string): string;
export function getAlpha2Code(name: string, lang: string): string;
export function getAlpha3Code(name: string, lang: string): string;
export function langs(): string[];
export function isValid(alpha2orAlpha3orNumeric: string | number): boolean;
The documentation for getAlpha2Codes()
, getAlpha3Codes()
, and getNumericCodes()
say they return arrays, but they actually return objects mapping to/from alpha-2 codes.
for Thailand,
pls.
ths.
Hi,
is there any easy way to sort the return of getNames by the value of the country name?
"IT": "意大利"
Following statement seems invalid:
If you use i18n-iso-countries 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.
I'm using nodejs and it will show undefined for my queries until I add the countries.registerLocale specifically. I use it in angular like this:
import * as countries from 'i18n-iso-countries';
Seems it was already mentioned here: #92
Any idea?
countries.alpha2ToNumeric('AL')
returns 8
instead of 008
Hi @michaelwittig!
I was using this library and figured I'd let you know why I had to switch to another library for ISO 3166 lookup, in case there is an opportunity for improvement.
So far as I can tell, the actual ISO specification does indeed list the name for the US as "United States of America", and I see the issue #33 where this was changed. The problem is that every other ISO 3166 library I've used (and I've tried a bunch) all return "United States".
This is affecting interoperability for me: for example, when I query the Last.fm geo API, the country
parameter expects a country name, not a code (extremely weird decision on their part, I know). But now I can't use this library to do the conversion, because the API expects "United States" and fails for "United States of America".
Are all the other libraries and Last.fm are just using outdated ISO 3166 databases? Or is there a short & long name and each library is only exposing one or the other? If this library's purpose is for printing nice i18n strings that's fine, I just figured I'd let you know about this use case in case you want more ISO 3166 interoperability.
The BCP47 Standard is the one where the locale is represented as follow:
language
-region
For example: us-EN, us-UK
This great lib should handle that case. A simple split('-')[0]
can do the trick
Is it possible to complete the list with some essential ISO 3166-2 subdivision codes like https://en.wikipedia.org/wiki/ISO_3166-2:GB
I can make a PR if this is something you want to add.
This is a really cool library but bundling it for usage in a browser results in a massive bundle (146KB for a bundle containing only this library, post-minification, pre-gzip).
Optimizing it wouldn't be too hard and, even while it would require a tiny bit more work on the developers that use this library I believe it would be definitely worth it.
First optimization that can be done is not loading all locales at once but only those needed by the app. There are a couple ways you can do this:
const countries = require('i18n-iso-countries');
countries.registerLocale(require('i18n-iso-countries/locales/en.json'));
countries.getNames('en'); // returns the list of names in english
countries.getNames('fr'); // throws "unknown locale, please register it first"
const countries = require('i18n-iso-countries');
const en = require('i18n-iso-countries/locales/en.json');
countries.getNames(en); // returns the list of names in english
If you wish to keep the "all locales are pre-loaded" behavior on node, you can specify a different entrypoint for browsers than for node in package.json
and have the node entrypoint register all locales then call the browser entrypoint.
Second optimization is getting rid of the pad
dependency. The reason behind it is that this library is itself dependant on wcwidth
, which itself is dependant on defaults
, which itself uses clone
.
That's a massive amount of code for something like pad
, even more now that String.prototype.pad{Start|End}
is standard and really easy to polyfill.
The best way to optimize this would be to simply use String.prototype.pad{Start|End}
instead of pad
, and add a note in the documentation stating that the environment needs to provide a polyfill for that function if it is not natively available. (core-js
can provide these polyfills)
I can submit a PR with all this if you are interested.
Thank you :)
I just installed i18n-iso-countries
and get thee following error:
W20170129-12:30:52.372(1)? (STDERR) Error: Cannot find module './codes.json'
W20170129-12:30:52.373(1)? (STDERR) at require (packages/modules-runtime.js:114:19)
W20170129-12:30:52.373(1)? (STDERR) at meteorInstall.node_modules.i18n-iso-countries.index.js (packages/modules.js:1207:13)
W20170129-12:30:52.373(1)? (STDERR) at fileEvaluate (packages/modules-runtime.js:188:9)
W20170129-12:30:52.373(1)? (STDERR) at Module.require (packages/modules-runtime.js:111:16)
W20170129-12:30:52.373(1)? (STDERR) at Module.Mp.import (/Users/ap/.meteor/packages/modules/.0.7.7.iu0idl++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20170129-12:30:52.373(1)? (STDERR) at meteorInstall.imports.api.jotForm.server.worker.js (imports/api/jotForm/server/worker.js:1:1)
W20170129-12:30:52.373(1)? (STDERR) at fileEvaluate (packages/modules-runtime.js:188:9)
W20170129-12:30:52.374(1)? (STDERR) at Module.require (packages/modules-runtime.js:111:16)
W20170129-12:30:52.374(1)? (STDERR) at Module.Mp.import (/Users/ap/.meteor/packages/modules/.0.7.7.iu0idl++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20170129-12:30:52.374(1)? (STDERR) at meteorInstall.imports.api.jotForm.server.publications.js (imports/api/jotForm/server/publications.js:1:1)
W20170129-12:30:52.374(1)? (STDERR) at fileEvaluate (packages/modules-runtime.js:188:9)
W20170129-12:30:52.374(1)? (STDERR) at Module.require (packages/modules-runtime.js:111:16)
W20170129-12:30:52.374(1)? (STDERR) at Module.Mp.import (/Users/ap/.meteor/packages/modules/.0.7.7.iu0idl++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20170129-12:30:52.375(1)? (STDERR) at meteorInstall.imports.startup.server.register-api.js (imports/startup/server/register-api.js:1:1)
W20170129-12:30:52.375(1)? (STDERR) at fileEvaluate (packages/modules-runtime.js:188:9)
W20170129-12:30:52.375(1)? (STDERR) at Module.require (packages/modules-runtime.js:111:16)
W20170129-12:30:52.375(1)? (STDERR) at Module.Mp.import (/Users/ap/.meteor/packages/modules/.0.7.7.iu0idl++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20170129-12:30:52.375(1)? (STDERR) at meteorInstall.imports.startup.server.index.js (imports/startup/server/index.js:1:13)
W20170129-12:30:52.375(1)? (STDERR) at fileEvaluate (packages/modules-runtime.js:188:9)
W20170129-12:30:52.375(1)? (STDERR) at Module.require (packages/modules-runtime.js:111:16)
W20170129-12:30:52.375(1)? (STDERR) at Module.Mp.import (/Users/ap/.meteor/packages/modules/.0.7.7.iu0idl++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20170129-12:30:52.376(1)? (STDERR) at meteorInstall.server.main.js (server/main.js:1:1)
Recently change the way you load the lang file:
node-i18n-iso-countries/index.js
Lines 7 to 10 in 7ede69e
Now you use a loop on all the defined code. Theses dynamics require
call cannot be interpreted by code bundler (browserify or webpack). It's break the library: the ./langs/*.json
are not bundled with the application so loading of the lib fail.
Could you rollback to a more predictive loading (like before)? I know this make the maintenance harder for you but it's the only way to keep the compatibilities with these tools.
Hello Michael,
Please, take note that "Libya" has become "State of Libya".
Would it be possible to update it?
Thank you,
Mino
Can you please handle the non-specific case of when using South Korea vs North Korea, instead of having to use the much more non-common standard of "Korea, Peoples Republic Of".
Hello,
i noticed that my code using your package was returning an empty Dict:
require('i18n-iso-countries').getNames('de') // {}
And so i was wondering what changed, it may be that i updated your package and something broke,
so looked into your code and saw that registeredLocales
is by default empty and you have to "import" languages by require.
Would you be interested for me to create a pull request which adds a method like following:
const c = require('i18n-iso-countries').import('de') // or 'en ' or 'fr'
// instead of
const c = require('i18n-iso-countries')
c.registerLocale(require('i18n-iso-countries/langs/de.json'))
... that would be much better to use than having to require a json by path.
Otherwise i really appreciate your work here 👍
This module looks awesome and I would like to replace our self curated languages lists with it.
As I would like to use it in our web apps, my question: Is it possible to load only specified languages instead of the entire list? We would currently only require two languages and I would like to save transfer bandwidth.
Thanks for this module.
The documentation could be improved by documenting that this module deals with 2 letter "ISO 3166-1 alpha 2" country codes.
This distinguishes them from "ISO 3166-1 alpha 3" codes which have 3 characters:
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
(In my case, I have a 3 letter code that I want to convert to a 2 letter code. I'm not sure if that's something that would be interest to include in the scope of this module or not).
version 3.7.0 seems to be missing ES5 libs, and is causing minify issues as described here:
Hi! You have a great library!
Is it possible to add language names translations to your library or you only work with country names?
It would be great to add this data with similar api https://www.loc.gov/standards/iso639-2/php/code_list.php
not have vi.json,
pls.
ths.
I am using node.js (Angular 6) but I need to register the locale data if I want to use it.
Is it correct?
I need to translate it into multiple languages, I can't register only 2 languages.
This might be a config issue on my side, but thought I'd mention it.
webpack:///./node_modules/i18n-iso-countries/index.js?:43
codes.forEach(function(codeInformation) {
^
TypeError: codes.forEach is not a function
at eval (webpack:///./node_modules/i18n-iso-countries/index.js?:43:7)
at Object.<anonymous> ([stdin]:9414:1)
at __webpack_require__ ([stdin]:20:30)
at eval (webpack:///./src/components/forms/ProfileBillingForm/ProfileBillingForm.jsx?:12:77)
at Object.<anonymous> ([stdin]:4459:1)
at __webpack_require__ ([stdin]:20:30)
at eval (webpack:///./src/components/forms/ProfileBillingForm/index.js?:1:82)
at Object.<anonymous> ([stdin]:9101:1)
at __webpack_require__ ([stdin]:20:30)
at eval (webpack:///./src/components/ProfileBilling/ProfileBilling.jsx?:6:84)
Any ideas how to resolve?
var countries = require("i18n-iso-countries");
console.log(countries.getNames("en"));
This is not working. It is returning empty object. You need to configure a locale in order for it to work:
var countries = require("i18n-iso-countries");
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.getNames("en"));
Documentation is wrong.
So I need something very similar to this library, but instead of ISO country codes to countries, I need to map ISO language codes to languages. I love the approach this library takes and I was wondering if...
a. You'd be open to a fork (renamed node-i18n-iso-languages
).
b. Anyone would be interested in helping on the new package.
I'm happy to take the lead on it, but I figure there might be various native speakers who contributed here that could help me bootstrap the different lang.json
files containing locale-specific language names.
I did find some similar projects for languages, but none as robust as this one...
Or, if someone does know of a robust alternative for languages instead of countries, please share.
Getting this warning since updated to 3.7.0:
Compiled with warnings.
./node_modules/i18n-iso-countries/index.js
46:18-63 Critical dependency: the request of a dependency is an expression
Is there a way to ignore this warning? Thanks!
As of the most recent ISO update, the name of SZ has changed.
I need a method to validate a country code. Currently I'm doing toAlpha3(code) !== undefined
. But I'd prefer that logic to be encapsulated into the node-i18n-iso-countries
package and to call a simple isValid
method.
Add function to fetch list of country codes, something similar to
const countryCodes = require('i18n-iso-countries/codes.json').map(info => info[0])
Hi,
It would be nice to have a function to return an array of country names, with their codes, sorted alphabetically by the locale. Useful for drop down lists.
On the referenced Wikipedia page there is a section with withdrawn codes: https://en.wikipedia.org/wiki/ISO_3166-1_numeric#Withdrawn_codes
I found myself getting data from a customer containing some of these withdrawn codes.
Is there any reason not to include these as well? There should be no conflicts between current and withdrawn codes so I don't see any harm in returning data for a code that is officially no longer part of the standard but may still be in use?
Would you accept a PR adding these codes?
If needed, it could of course be made configurable, such as:
const countries = require('i18n-iso-countries')({includeWithdrawn: true});
const alpha2 = countries.numericToAlpha2('230');
or
const countries = require('i18n-iso-countries');
countries.configure({includeWithdrawn: true});
const alpha2 = countries.numericToAlpha2('230');
or, in each individual call:
const countries = require('i18n-iso-countries');
const alpha2 = countries.numericToAlpha2('230', {includeWithdrawn: true});
Hi,
My question is can I use this library on angular project?
I have this error on compilation:
You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (2:6)
on i18n-iso-countries/langs/ar.json Unexpected token (2:6)
Thanks
The country code for the Canary Islands is currently not supported by the library.
WARNING in ./node_modules/i18n-iso-countries/index.js 46:18-63
Critical dependency: the request of a dependency is an expression
What's the status or the explanation for this string?
node-i18n-iso-countries/langs/en.json
Line 129 in dfdb000
I propose to set change english name from
"US": "United States"
to
"US": "United States of America"
When using this library for search-as-you-type in 'select country' form input in english, entering america
only shows American Samoa
which looks weird.
I offer to create a branch and PR here if you permit me, or see attached diff.
The correct Russian translation for Belarus is Беларусь and not Белоруссия .
could this be fixed?
Thank you
Hi,
I can see that countries data are missing
Why isValid return true for the string '...' ?
i18nIsoCountries.isValid('...') // return true
Hello,
since a few days greek is supported as language, using the getName(country, language) function. But I wonder why the language code was renamed from el to gr in this commit:
I thought that ISO_3166-1 define countries and e.g. iso 639 define language codes? Since you can also pass "ar" as language, which in this context is the language code from "arabic" and not the country "argentina". So it is only consequent to use "el" instead of "gr" as language code, when you mean greek (the language).
I think I am also confused, because the documentation of the getName function does not clearly state, what iso-definition the language-parameter should conform to.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.