Rails 国际化帮助 [英] Rails internationalisation help

查看:65
本文介绍了Rails 国际化帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了丹麦语语言环境 (da.yml) 并将其放入 config/locales

I have downloaded the danish locale (da.yml) and put it in config/locales

我收到此错误:无法在第 20 行第 14 列解析 YAML

I get this error: couldn't parse YAML at line 20 column 14

这与我在 的简单形式中得到的错误相同Rails 3 简单表单错误:无法解析 YAML

我的 application.rb:

My application.rb:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

module App
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :da

    # JavaScript files you want as :defaults (application.js is always included).
    # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]
  end
end

我的视图文件:

<% @konkurrancer.each do |vind| %>
   <tr onclick="window.open('<%= vind.tracking %>')" 
      <td><%= vind.udtraekkes.strftime("%d %B") %></td>
   </tr>
 <% end %>

出现以下错误:

C:\Rails\app>rails console
Loading development environment (Rails 3.0.3)
irb(main):001:0> I18n.l Time.now, :format => :short
Psych::SyntaxError: couldn't parse YAML at line 0 column 0
        from C:/Ruby192/lib/ruby/1.9.1/psych.rb:148:in `parse'
        from C:/Ruby192/lib/ruby/1.9.1/psych.rb:148:in `parse_stream'
        from C:/Ruby192/lib/ruby/1.9.1/psych.rb:119:in `parse'
        from C:/Ruby192/lib/ruby/1.9.1/psych.rb:106:in `load'
        from C:/Ruby192/lib/ruby/1.9.1/psych.rb:205:in `load_file'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:170:in `load_yml'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:156:in `load_file'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:15:in `block in load_translations'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:15:in `each'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:15:in `load_translations'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/sim
ple.rb:55:in `init_translations'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/sim
ple.rb:69:in `lookup'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:26:in `translate'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n.rb:155:in `
translate'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n/backend/bas
e.rb:55:in `localize'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/i18n-0.5.0/lib/i18n.rb:235:in `
localize'
        from (irb):1
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/comman
ds/console.rb:44:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/comman
ds/console.rb:8:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/comman
ds.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'irb(main):002:0>

但我在视图中的日期名称没有改变.我的丹麦语言环境文件 http://pastie.org/1651134

But my dates names in view does not change. My danish locale file http://pastie.org/1651134

推荐答案

您需要对日期使用 l 翻译助手,而不是使用 strftime(请参阅 此处).

You need to use the l translation helper for dates and not use strftime (see here).

所以如果你在你的语言环境中有这个:

So if you had this in your locale:

da:
  time:
    formats:
      short: "arrrround %H"

那么在您看来,您将使用:

Then in your view, you'd use:

<%= l vind.udtraekkes, :format => :short %>

这篇关于Rails 国际化帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆