Rails:删除缺少的翻译错误 [英] Rails: Remove missing translation errors

查看:165
本文介绍了Rails:删除缺少的翻译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将国际化用于英语(en)法语(fr),我将en.yml用于有限的用途以及我所写的大多数翻译fr.yml.

I am using internationalization for english (en) and french (fr), I have used en.yml for limited use and most of the translations I am writing in fr.yml.

区域设置为fr 的情况下,一切正常,但是在 en的情况下,由于缺少翻译范围,它向我显示了错误.

With locale as fr everything works good, but with en it shows me error as missing translation span.

例如,如果我有类似的东西

For eg if I have got something like

<%= text_field_tag( "search", params[:search], :placeholder=>t("Search"), :class=>"search_input") %>

我得到的en的输出是:

and i get output for en is:

<input class="search_input" id="search" name="search" placeholder="<span class=" translation_missing"="" title="translation missing: en.Search">

我想要的是,应该关闭英语的翻译错误,因为英语是我的默认语言,但是在某些情况下,我使用了en.yml.

What I want is that it should turn off translation errors for english, since english is my default language, but for some cases I've used en.yml.

或者,如果这不可能,则应删除整个错误消息.

Or if this is not possible then whole error message should be removed.

谢谢

推荐答案

在Rails 4.1中更改了HTML缺少翻译错误的实现.现在,它不是I18n库,而是在视图帮助程序层上处理的.即,在翻译"助手方法中(请参见action_view/helpers/translation_helper.rb).

The implementation of HTML missing translation errors has been changed in Rails 4.1. Now instead of I18n library it is handled on the view helper layer. Namely, in "translate" helper method (see action_view/helpers/translation_helper.rb).

现在执行此操作的一种干净方法是重写helper方法并自己处理异常.

A clean way to do this now is to override the helper method and handle the exception yourself.

# app/helpers/i18n_helper.rb

module I18nHelper
  def translate(key, options={})
    super(key, options.merge(raise: true))
  rescue I18n::MissingTranslationData
    key
  end
  alias :t :translate
end

这篇关于Rails:删除缺少的翻译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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