PhantomJS 不支持 Javascript 国际化 API

标签 javascript angularjs internationalization phantomjs ecmascript-intl

我针对使用 ECMAScript 国际化 API 的 AngularJs 服务运行了一系列 Jasmine 测试。当我通过 Chrome 运行它们时,它们都运行成功。但是,当我使用 PhantomJS 通过 Maven 运行它们时,它们都失败了,因为它似乎 PhantomJs 还不支持国际化 API。

我使用 Intl 对象进行测试时得到的错误消息是:

1: ReferenceError: Can't find variable: Intl in localizationService.js

其余的测试都失败了。

测试很简单,看起来像这样:

it('Format date with en-us locale', (function (){
    var date= "06/13/2013"
    expect(service.date(date,'en-us')).toEqual("6/13/2013");
}))

服务 (localizationService.js) 中的方法是 Intl API 的简单包装器:

function getCurrentTimeZone(){
    return Intl.DateTimeFormat().resolved.timeZone
}

function date(dateInput,locale,options){
        // some other stuff
        // ... 
        if (locale) {
            return _date.toLocaleDateString(locale,options);
        } else {
            return _date.toLocaleDateString();
        }
}

我的问题是:

1- 我的假设是否正确,即 PhantomJS v1.9.2 不支持 ECMAScript 国际化 API?有什么可以确认的吗?

2- 我该如何解决这个问题?我需要通过 Maven 运行我的测试,我将有更多测试以一种或另一种方式访问​​我的 localizationService API。
谢谢

最佳答案

不确定您是否在使用 Karma,但这是我必须做的来解决同样的问题。

  1. npm install karma-intl-shim --save-dev

    这还将安装 polyfill 库 Intl;

  2. 将“intl-shim”添加到 karma.conf.js 中的框架集合:

    ...
    frameworks: ['intl-shim'],
    
  3. 在 karma.conf.js 中添加您希望用于测试的语言环境文件,例如“en-US”:

    ...
    files: [
          './node_modules/Intl/locale-data/jsonp/en-US.js',
    ...
    

关于PhantomJS 不支持 Javascript 国际化 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19228209/

相关文章:

python - 如何在 websetup.py 的 setup_app 中启用 i18n? (格式化重发)

javascript - $(window).scrollTop() 在 safari 中不起作用

javascript - 如何在提交时向表中添加数据

javascript - AngularJS帮助: Connecting a Search Input into an REST Interface

javascript - 使用 AngularJS 将表单数据数组保存到先前的数组

django - {% trans "string"as my_translated_string %} 未在模板中呈现内容

javascript - 编辑帖子 AJAX、PHP、javascript

c# - 使用javascript在asp.net中进行 future 日期验证

javascript - 从数据库中获取 SELECT 的下拉值而不是在 UI 上进行硬编码 (Angularjs)

ruby-on-rails - 为什么 Rails 中的语言环境设置是全局的(使用 Thin 时)?