gpt4 book ai didi

angular - 如何在 Angular 6 中使用 i18n-iso-countries

转载 作者:行者123 更新时间:2023-12-04 14:41:18 27 4
gpt4 key购买 nike

在我的 Angualr 6 应用程序中,我尝试使用 i18n-iso-countries 库 (JavaScript) 将国家/地区代码转换为国家/地区名称。
我是用 npm 安装的

npm -i i18n-iso-countries



然后在我的组件中,我像这样导入它:

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



我是这样使用它的:
  let valueName = '';
tmpArr = tmpArr.map((e) => {
const tmp = e.split('_')[1];
console.log('tmp: ' + tmp);
// console.log(i18IsoCountries);
valueName = i18IsoCountries.getName(tmp, 'en');
return tmp;
}

但是 valueName 是未定义的。

我也在 Stackblitz here. 中创建了类似的设置
您可以看到 getName 的输出未定义,但 getAlpha2Codes() 的输出返回值。

最佳答案

我刚刚发现语言需要注册。文档有点困惑

If you use i18n-iso-countries with Node.js your are done.



但是你 需要注册您要使用的语言。

所以在 Angular 6 中你需要:

npm install i18n-iso-countries --save



然后在您的组件中:
import * as i18nIsoCountries from 'i18n-iso-countries';
然后在 ngOnInit()
i18nIsoCountries.registerLocale(require("i18n-iso-countries/langs/en.json"));

关于angular - 如何在 Angular 6 中使用 i18n-iso-countries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52600104/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com