如何在没有TypeError的情况下执行ng-xi18n:无法读取属性'创建'未定义?

时间:2016-12-21 10:00:19

标签: angular ionic2

我想在Ionic 2项目中使用internationalization (i18n) of Angular 2。不幸的是,我无法使用ng-xi18n工具尝试以下命令创建翻译源文件:

./node_modules/.bin/ng-xi18n

出现以下TypeError:

TypeError: Cannot read property 'create' of undefined
    at Function.Extractor.create (/Users/user/Development/app/node_modules/@angular/compiler-cli/src/extractor.js:69:45)
    at extract (/Users/user/Development/app/node_modules/@angular/compiler-cli/src/extract_i18n.js:7:34)
    at Object.main (/Users/user/Development/app/node_modules/@angular/tsc-wrapped/src/main.js:47:16)
    at Object.<anonymous> (/Users/user/Development/app/node_modules/@angular/compiler-cli/src/extract_i18n.js:14:9)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
Extraction failed

我尝试升级我的软件包但没有成功。这是我在文件package.json

中的依赖项
"dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/compiler-cli": "^2.4.0",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/platform-server": "^2.4.0",
    "es6-shim": "0.35.1",
    "ionic-angular": "2.0.0-beta.11",
    "ionic-native": "1.3.10",
    "ionicons": "3.0.0",
    "reflect-metadata": "0.1.8",
    "rxjs": "5.0.0-beta.6",
    "typescript": "^2.1.4",
    "zone.js": "0.6.12"
},

过去也许有人解决了这个问题。感谢先进的答案。

2 个答案:

答案 0 :(得分:7)

根据Angular.io,您只需指定翻译格式(不要依赖默认值)。

例如:

./node_modules/.bin/ng-xi18n --i18nFormat xlf

你应该得到一个message.xlf文件。

答案 1 :(得分:3)

回答我自己的问题:这是依赖项的问题。重写package.json解决了这个问题。

"dependencies": {
  "@angular/common": "2.2.1",
  "@angular/compiler": "2.2.1",
  "@angular/compiler-cli": "2.2.1",
  "@angular/core": "2.2.1",
  "@angular/forms": "2.2.1",
  "@angular/http": "2.2.1",
  "@angular/platform-browser": "2.2.1",
  "@angular/platform-browser-dynamic": "2.2.1",
  "@angular/platform-server": "2.2.1",
  "ionic-angular": "^2.0.0-rc.4",
  "ionic-native": "^2.2.12",
  "ionicons": "^3.0.0",
  "rxjs": "5.0.0-beta.12",
  "typescript": "^2.1.4",
  "zone.js": "^0.6.21"
},

可能只是短时间的解决方案,直到出现下一个错误。