0

I'm new in Angular, Node and Javascript. But I need to work on some project in this technologies. My question is like this. I would like to use library i18n-iso-countries in my project. So I executed npm install i18n-iso-countries --save. So far so good.

In my component file home.component.ts I imported it like this:

import * as countries from 'i18n-iso-countries';

and use like this to make sure library is loaded and is working:

console.log(countries.alpha2ToNumeric("PL"));
console.log(countries.numericToAlpha2(840));
console.log(countries.getName(840, "en"));
console.log(countries.getName("US", "en"));

Two first lines works as expected but last two not. I noticed that I need to register locale according to the docs. So the question is how to import locale? I need to execute

countries.registerLocale(require("i18n-iso-countries/langs/en.json"));

But the error is:

Cannot find name 'require'.

I tried also different import forms like:

import locale = require('i18n-iso-countries/lang/en.json'); import * as locale from 'i18n-iso-countries/langs/en';

But without success. Maybe I'm missing something trivial but I'm not aware of it. I read Modules chapter in typescript Handbook but it doesn't answer to my questions. Maybe You guys will help.

My package.json:

{
  "name": "my-app",
  "version": "0.1.1",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.4.5",
    "@angular/cdk": "^2.0.0-beta.12",
    "@angular/common": "^4.4.5",
    "@angular/compiler": "^4.4.5",
    "@angular/core": "^4.4.5",
    "@angular/flex-layout": "2.0.0-beta.8",
    "@angular/forms": "^4.4.5",
    "@angular/http": "^4.4.5",
    "@angular/material": "^2.0.0-beta.12",
    "@angular/platform-browser": "^4.4.5",
    "@angular/platform-browser-dynamic": "^4.4.5",
    "@angular/platform-server": "^4.4.5",
    "@angular/router": "^4.4.5",
    "ag-grid": "^13.3.1",
    "ag-grid-angular": "^13.3.0",
    "core-js": "2.4.1",
    "hammerjs": "2.0.8",
    "i18n-iso-countries": "^3.0.0",
    "rxjs": "^5.4.1",
    "zone.js": "0.8.12"
  },
  "devDependencies": {
    "@angular/cli": "1.1.3",
    "@angular/compiler-cli": "^4.4.5",
    "@types/hammerjs": "2.0.34",
    "@types/jasmine": "2.5.45",
    "@types/node": "6.0.78",
    "codelyzer": "3.0.1",
    "jasmine-core": "2.6.4",
    "jasmine-spec-reporter": "4.1.1",
    "karma": "1.7.0",
    "karma-chrome-launcher": "2.1.1",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "1.3.0",
    "karma-jasmine": "1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "protractor": "5.1.2",
    "ts-node": "3.0.6",
    "tslint": "5.3.2",
    "typescript": "^2.3.4"
  }
}
Marcin Kapusta
  • 5,076
  • 3
  • 38
  • 55

2 Answers2

1
var countries = require("i18n-iso-countries");

// Support french & english languages.
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
countries.registerLocale(require("i18n-iso-countries/langs/fr.json"));

console.log("US (Alpha-2) => " + countries.getName("US", "en")); // United States of America
console.log("US (Alpha-2) => " + countries.getName("US", "de")); // Vereinigte Staaten von Amerika
console.log("USA (Alpha-3) => " + countries.getName("USA", "en")); // United States of America
console.log("USA (Numeric) => " + countries.getName("840", "en")); // United States of America
0
declare var System: any;
declare var require: any;

after declaring this in typing.d.ts, error for "require" and "system" will not come in the application..