I18n :: InvalidLocaleData:I18n gem在js导出中有问题

时间:2018-07-18 11:28:02

标签: ruby string parsing escaping yaml

我正在使用OS X 10.13.4上的Rails 3.2.22.4,Ruby 2.2.7 开发应用程序。有多个翻译文件,例如。 en.yml,基于国家/地区的sq.yml。从下面的rubymine执行命令时,加载网页时会看到错误。

  

rake --trace i18n:js:export

  • en.yml中的yaml内容已通过验证,是正确的。一旦从文件中删除该错误所在的特定行将在其他.yml文件中给出错误。正在使用当前版本的宝石i18n (0.9.5)
  • Rails服务器启动,但是Web应用程序首页的加载因相同的错误而中断。
  • 在代码遇到i18n翻译代码时引发错误,因为错误从一个文件移动到另一个文件,所以yml格式不是问题。
  • 这主要是i18n,Psych等宝石问题,但是更改多个版本并不能解决。

如果尝试加载页面,则会在浏览器中显示错误:

can not load translations from /Users/biju/projecty/config/locales/en.yml: 
#<Psych::SyntaxError: (/Users/biju/projecty/config/locales/en.yml): found unknown escape character while parsing a quoted scalar at line 26 column 27>

尝试执行rake任务时出现错误堆栈

2018-07-18 16:40:51 +0530 Projecty::Application.initialize! took 1.733 sec
---------------------------------------------------------------------------------------------------------------
** Execute i18n:js:export
rake aborted!
I18n::InvalidLocaleData: can not load translations from /Users/biju/projecty/config/locales/en.yml: #<Psych::SyntaxError: (/Users/biju/projecty/config/locales/en.yml): found unknown escape character while parsing a quoted scalar at line 26 column 27>
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/base.rb:232:in `rescue in load_yml'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/base.rb:229:in `load_yml'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/base.rb:213:in `load_file'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/base.rb:15:in `block in load_translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/base.rb:15:in `each'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/base.rb:15:in `load_translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-0.9.5/lib/i18n/backend/simple.rb:63:in `init_translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:164:in `block in translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:163:in `instance_eval'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:163:in `translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:121:in `block in scoped_translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:120:in `each'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:120:in `scoped_translations'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:43:in `segment_for_scope'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:54:in `block in configured_segments'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:48:in `each'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:48:in `inject'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:48:in `configured_segments'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:93:in `translation_segments'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/i18n/js.rb:36:in `export'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/i18n-js-3.0.5/lib/tasks/export.rake:5:in `block (3 levels) in <top (required)>'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:240:in `call'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:240:in `block in execute'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:235:in `each'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:235:in `execute'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/biju/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/bin/rake:23:in `load'
/Users/biju/.rvm/gems/ruby-2.2.7@yb/bin/rake:23:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'

.yml文件的摘要,第26行给出了错误。

    en:
  formtastic:
    labels:
      event_filter:
        source_server: "Server:"
        source_source: "Sevent_filter:"
        source_server: "Server:"
        source_source: "Source:"
        serverOption: "Server:"
        Category: "Category:"
        EventID: "Event ID:"
        Type: "Type:"
        TEA: "Target E-mail Address:"
        All: "All"
        ShWare: "ShWare:"
        Services: "Services:"
        Other: "Other"
        Any: "Any"
        Error: "Error"
        Warning: "Warning"
        Information: "Information"
        None: ""
        can_not_be_blank: "can't be blank or click on \'Any\'"

  list:
    buttons:
      previous: "Previous"
      next: "Next"

编辑 在Rails控制台中重建错误:

    trans2 = YAML.load(File.read("#{D2_DIR}/config/locales/en.yml")).with_indifferent_access
Psych::SyntaxError: (<unknown>): found unknown escape character while parsing a quoted scalar at line 26 column 27
    from /Users/biju/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/psych.rb:370:in `parse'
    from /Users/biju/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
    from /Users/biju/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/psych.rb:318:in `parse'
    from /Users/biju/.rvm/rubies/ruby-2.2.7/lib/ruby/2.2.0/psych.rb:245:in `load'
    from (irb):18
    from /Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/railties-3.2.22.4/lib/rails/commands/console.rb:47:in `start'
    from /Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/railties-3.2.22.4/lib/rails/commands/console.rb:8:in `start'
    from /Users/biju/.rvm/gems/ruby-2.2.7@yb/gems/railties-3.2.22.4/lib/rails/commands.rb:41:in `<top (required)>'
    from /Users/biju/projecty/script/rails:11:in `require'
    from /Users/biju/projecty/script/rails:11:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

经过足够的搜索后,我无法获得指针,但是可能不确定是否是宝石问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

这是特定于操作系统的问题,与YAML库有关。 .yml文件的解析在 can_not_be_blank处中断:“不能为空或单击\'Any \'” 。以下错误仅出现在经过多个OS x版本测试的OSX中。

require 'yaml

2.4.1 :002 > trans1 = YAML.load(File.read("/Users/app.yml"))
Psych::SyntaxError: (<unknown>): found unknown escape character while parsing a quoted scalar at line 23 column 27
    from /Users/biju/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/psych.rb:377:in `parse'
    from /Users/biju/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/psych.rb:377:in `parse_stream'
    from /Users/biju/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/psych.rb:325:in `parse'
    from /Users/biju/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/psych.rb:252:in `load'
    from (irb):2
    from /Users/biju/.rvm/rubies/ruby-2.4.1/bin/irb:11:in `<main>'
2.4.1 :003 > 

相同的代码在Windows irb中有效。但是,在OSX中,将 \'Any \'更改为'Any'有助于执行该操作。不知道可能是什么原因,除了库是安装在UNIX vs Windows中。