Rails i18n 规范不起作用

Rails i18n spec doesn't work

在我的 rails 应用程序中,我使用 gem i18n_spec 添加 rspec 测试来检查我的 i18n 配置文件是否正确。

规范文件

require 'spec_helper.rb'
require 'i18n-spec/tasks'

Dir.glob('config/locales/*.yml') do |locale_file|
  describe "#{locale_file}" do
    it { subject.should be_parseable }

导致错误

  ../gems/i18n-spec-0.6.0/lib/i18n-spec/tasks.rb:5:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
    from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
    from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
    from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
    from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
    from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
    from ../spec/selftest/i18n_spec.rb:3:in `<top (required)>'

为什么要测试它们是否可解析?如果您在单元/功能规范中断言 any I18n 键 Rails 将加载您的 I18n 文件并在 yml 文件不可解析时抛出语法错误!

你会得到这样的断言:

  within(".mod-header .login") { click_link I18n.t('public.menu.login') }
  find("#main-container h1").should have_content I18n.t('pages.login.title')

这样 Rails 将加载并解析您的 yml 文件。如果有任何错误,您会在测试输出中找到它们!

错误如下:

<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): mapping values are not allowed in this context at line <num> column <num>>

<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): did not find expected key while parsing a block mapping at <num> column <num>>