Ruby-on-rails Rails I18n,检查翻译是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12353416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:43:26  来源:igfitidea点击:

Rails I18n, check if translation exists?

ruby-on-railsinternationalizationrails-i18n

提问by agmcleod

Working on a rails 3 app where I want to check if a translation exists before outputting it, and if it doesn't exist fall back to some static text. I could do something like:

在 rails 3 应用程序上工作,我想在输出之前检查翻译是否存在,如果不存在,则退回到某些静态文本。我可以做这样的事情:

if I18n.t("some_translation.key").to_s.index("translation missing")

But I feel like there should be a better way than that. What if rails in the future changes the "translation missing" to "translation not found". Or what if for some weird reason the text contains "translation missing". Any ideas?

但我觉得应该有比这更好的方法。如果 rails 将来将“缺少翻译”更改为“找不到翻译”怎么办。或者如果由于某种奇怪的原因文本包含“缺少翻译”怎么办。有任何想法吗?

回答by Chris Salzberg

Based on what you've described, this should work:

根据您的描述,这应该有效:

I18n.t("some_translation.key", :default => "fallback text")

See the documentationfor details.

有关详细信息,请参阅文档

回答by albandiguer

You can also use

你也可以使用

I18n.exists?(key, locale)
I18n.exists?('do_i_exist', :en)

回答by Nowaker

:defaultis not always a solution. Use this for more advanced cases:

:default并不总是一个解决方案。将此用于更高级的情况:

helpers/application.rb:

助手/application.rb:

def i18n_set? key
  I18n.t key, :raise => true rescue false
end

any ERB template:

任何 ERB 模板:

<% if i18n_set? "home.#{name}.quote" %>
  <div class="quote">
    <blockquote><%= t "home.#{name}.quote" %></blockquote>
    <cite><%= t "home.#{name}.cite" %></cite>
  </div>
<% end %>

回答by Jeremy F.

What about this ?

那这个呢 ?

I18n.t('some_translation.key', :default => '').empty?

I just think it feels better, more like there is no translation

我只是觉得感觉更好,更像是没有翻译

Caveat: doesn't work if you intentionally have an empty string as translation value.

警告:如果您故意将空字符串作为翻译值,则不起作用。

回答by sumskyi

use :default param:

使用:默认参数:

I18n.t("some_translation.key", :default => 'some text')

回答by Fer Padron

sometimes you want to do more things on translations fails

有时你想在翻译上做更多的事情失败

v = "doesnt_exist"
begin
  puts I18n.t "langs.#{v}", raise: true
rescue
  ...
  puts "Nooo #{v} has no Translation!"
end

回答by rchampourlier

This is a trick but I think it may be useful sometimes...

这是一个技巧,但我认为它有时可能有用......

Assuming you have this in your i18n file:

假设你的 i18n 文件中有这个:

en:
  key:
    special_value: "Special value"
    default_value: "Default value"

You may do this:

你可以这样做:

if I18n.t('key').keys.include?(:special_value)
  I18n.t('key.special_value')
else
  I18n.t('key.default_value')
end
# => "Special value"

if I18n.t('key').keys.include?(:unknown_value)
  I18n.t('key.special_value')
else
  I18n.t('key.default_value')
end
# => "Default value"

NB: This only works if you're testing anything but a root key since you're looking at the parent.

注意:这仅在您测试除根密钥以外的任何东西时才有效,因为您正在查看父密钥。

In fact, what's interesting is what you can get when requesting a parent key...

事实上,有趣的是在请求父密钥时可以得到什么......

I18n.t('key')
# => {:special_value=>"Special value", :default_value=>"Default value"}

回答by mahatmanich

Rails 4

导轨 4

I was iterating over some urls of jury members. The max amount of urls were 2, and default_lang was "de". Here is the yaml that I used

我正在遍历陪审团成员的一些网址。url 的最大数量为 2,default_lang 为“de”。这是我使用的 yaml

de:
 jury:
  urls:
   url0: http://www.example.com
   name0: example.com
   url1:
   name1:

en:
 jury:
  urls:
   url0: 
   name0: 
   url1:
   name1:

Here is how I checked if there was a url given and if it did not exist for another language, it would fallback to the I18n default_lang "de". I used answer of @albandiguer which worked great.

这是我如何检查是否有给定的 url,如果它不存在于另一种语言,它将回退到 I18n default_lang“de”。我使用了@albandiguer 的答案,效果很好。

I Hope this helps someone:

我希望这可以帮助别人:

    <% 2.times do |j| %>
        <% if I18n.exists?("jury.urls.url#{j}", "de") &&
           I18n.exists?("jury.urls.name#{j}", "de") %>
          <%= "<br/>".html_safe  if j == 1%>
          <a href="<%= t("jury.urls.url#{j}") %>" target="_blank">
            <%= t("jury.urls.name#{j}") %>
          </a>
        <% end %>
     <% end %>

回答by Gabrielizalo

Some versions ago there is a easier way i18next documentation > API > t:

某些版本以前有一种更简单的方法i18next 文档 > API > t

You can specify either one key as a String or multiple keys as an Array of String. The first one that resolves will be returned.

您可以将一个键指定为字符串或将多个键指定为字符串数组。将返回第一个解析的。

Example:

例子:

i18next.t ( ['unknown.key', 'my.key' ] ); // It will return value for 'my.key'

i18next.t ( ['unknown.key', 'my.key' ] ); // It will return value for 'my.key'

Also you can use Contexts. tif not found a keyinto a contextreturns the default value.

您也可以使用Contextst如果未找到进入上下文键,则返回默认值。