【问题标题】:Rails 4 I18n - missing translation after adding new locale fileRails 4 I18n - 添加新的语言环境文件后缺少翻译
【发布时间】:2015-09-09 10:13:06
【问题描述】:

目前我有一些语言环境子目录,在配置中设置正确

        config/application.rb
        config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]

我已经有 2 个语言环境文件,我可以轻松获得翻译:

        config/locales/admin/dashboard.en.yml
        en:
          dashboard:
            title: Dashboard

        config/locales/admin/dashboard.fr.yml
        fr:
          dashboard:
            title: Tableau de bord

        irb(main):014:0> I18n.locale = :en
        => :en
        irb(main):015:0> I18n.t("dashboard.title")
        => "Dashboard"
        irb(main):016:0> I18n.locale = :fr
        => :fr
        irb(main):017:0> I18n.t("dashboard.title")
        => "Tableau de bord"

现在我在 SAME 子目录“locales/admin”中添加了 2 个其他语言环境文件(与之前的非常相似...)

        # =============

        config/locales/admin/sheet.en.yml
        en:
          sheet:
            title: Sheet

        config/locales/admin/sheet.fr.yml
        fr:
          sheet:
            title: Table

我重新启动了服务器...并尝试成功获取新添加的翻译

        irb(main):010:0> I18n.locale = :en
        => :en
        irb(main):011:0> I18n.t("sheet.title")
        => "translation missing: en.sheet.title"
        irb(main):012:0> I18n.locale = :fr
        => :fr
        irb(main):013:0> I18n.t("sheet.title")
        => "translation missing: fr.sheet.title"

两种语言都缺少翻译?所以我猜 application.rb 配置文件中的子目录定义有问题,因为我检查了 Rails.application.config.i18n.load_path ,并且新添加的文件不在路径中..

"..../config/locales/admin/dashboard.en.yml",   
"..../config/locales/admin/dashboard.fr.yml",

但不是

"..../config/locales/admin/sheet.en.yml", 
"..../config/locales/admin/sheet.fr.yml", 

感谢您的建议

【问题讨论】:

    标签: ruby-on-rails-4 rails-i18n


    【解决方案1】:

    看来这个问题已经被检测到了,并且和'spring'进程有关..

    #  https://github.com/rails/spring/issues/408
    #   rafaelfranca May 28
    I think you got this error because you added the locale file after you
    started the spring process for your environment, so when you add the 
    file it is not loaded inside the spring process. When you change the 
    application.rb the spring process is reloaded and your locale is now 
    working.
    

    我在 application.rb 中更改了一行(添加了一个空行...),然后重新启动服务器,找到了新的翻译

    rafael 说 rails 控制台应该重启 spring

    Any rails command starts springs. rails console, rails generate, 
    rails server. You can stop the spring process with spring stop.
    

    所以,在添加新的语言环境文件后,最好在重新启动服务器之前停止 spring

    spring stop
    rails server ( or rails console )
    

    【讨论】:

      【解决方案2】:

      更改 application.rb => 重启 Rails 服务器

      我想补充一点,当您对 application.rb 进行任何更改时,您必须重新启动服务器才能使更改生效,不仅仅是 i18n 组件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        • 2012-03-25
        • 1970-01-01
        • 2015-03-25
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多