【问题标题】:RAILS: How to eliminate `translation missing` from some I18n.t in production (I18n.t fallback enabled)?RAILS:如何从生产中的某些 I18n.t 中消除“翻译缺失”(启用 I18n.t 回退)?
【发布时间】:2017-04-26 01:52:49
【问题描述】:

在我的 Rails 4.2 应用程序中,所有模块都是使用 I18n.t() 开发的。下面是 index.html.erb 的一个例子:

<table class="table table-hover">
  <thead>    
    <tr>
      <th><%= t('Create Date') %></th>
      <th><%= t(@for_which.sub('_', ' ').titleize) %></th>
      <th><%= t('Brief Note') %></th>
      <th><%= t('Ranking Index') %></th>
      <th><%= t('Active') %></th>
      <th><%= t('Entered By') %></th>
      <th></th>
      <th></th>
    </tr>
     </thead>
       <tbody>

initializers下的local.rb中,本地设置为en

I18n.enforce_available_locales = false
I18n.default_locale = 'en' if Rails.env.production? || Rails.env.development?
I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

但在生产和开发中,我仍然从一些I18n.t 中看到translation missing 的错误,例如标签和索引标题如下:

如何摆脱translation missing的错误?

更新:错误translation missing 显示在production 中,即使在environment/production.rb 中使用以下I18n.t 后备:

 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

更新: 添加CSS:

//hide all "translation missing" warnings:
body.production span.translation_missing {
  display: none;
}

但是选项卡、按钮和表头上的文字消失了:

这是显示标签Employee Infos之一的html源代码的一部分:

            <li class="dropdown">
        <a id="dropdownuser" href="#" data-toggle="dropdown" class="dropdown-toggle" data-toggle="tooltip" data-placement="bottom" ><span class="glyphicons glyphicons-yen text-danger"></span> <span class="translation_missing" title="translation missing: en.Employee Info">Employee Info</span> <b class="caret" id="caret_medium"></b></a>
              <ul id="banktransaction" class="dropdown-menu" role="menu">

                <li><a href="/tt/view_handler?index=1&amp;url=/tt/tsheet/employee_infos"><i class='glyphicon glyphicon-list'></i> translation missing: en.List</a></li>
                <li></li>
                <li><a href="/tt/view_handler?index=1&amp;url=/tt/tsheet/employee_infos/search"><i class='glyphicon glyphicon-list-alt'></i> translation missing: en.Search</a></li>                
              </ul>
            </li>

【问题讨论】:

  • 看看这是否能解决您的问题。 stackoverflow.com/questions/12914019/…
  • 啊啊啊在en.yaml下添加Customers键??
  • @user938363 我删除了我的答案。正如@niceman 建议的那样,第一个解决方案当然应该是将翻译添加到en 字典文件。我以某种方式认为您不能这样做,并且您需要隐藏其余的警告,但是在重读您的问题后,我发现您在任何地方都没有说类似的话。如果确实如此,请更新问题,我将重新发布一个彻底更新的答案,如何修改首选的 Rails 行为。很抱歉造成这种误解。

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


【解决方案1】:

在您的 #config/application.rb 文件中,您可以执行类似的操作

Rails.application.configure do |config|
  config.action_view.raise_on_missing_translations = false
end

你可以得到更多here

希望对你有帮助:)

【讨论】:

  • MZaragoza,我只能放在/environment/development.rb or production.rb,在application.rb不行。它仍然显示翻译丢失错误。即使在生产中。奇数。
  • 这不会做任何改变,因为这是 Rails 4 中的 default
【解决方案2】:

我不知道为什么,但我刚刚重新启动了我的服务器并繁荣了!它奏效了。

你也可以尝试重启你的服务器:)

【讨论】:

    【解决方案3】:

    使用“Faker::Job”而不是“Name.title”

    Faker::Job
    Faker::Job.title #=> "Lead Accounting Associate"
    
    Faker::Job.field #=> "Manufacturing"
    
    Faker::Job.seniority #=> "Lead"
    
    Faker::Job.position #=> "Supervisor"
    
    Faker::Job.key_skill #=> "Teamwork"
    
    Faker::Job.employment_type #=> "Full-time"
    
    Faker::Job.education_level #=> "Bachelor"
    

    它对我有用:)

    【讨论】:

    • 这如何回答这个问题?请在您的答案中添加说明。
    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多