Rails I18n,检查翻译是否存在?

时间:2022-12-08 07:41:14

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在未来将“翻译缺失”更改为“翻译未找到”会怎么样?或者,如果由于某种奇怪的原因,文本包含“翻译缺失”。什么好主意吗?

8 个解决方案

#1


86  

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

根据你所描述的,这应该是可行的:

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

See the documentation for details.

详情请参阅文档。

#2


41  

You can also use

您还可以使用

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

#3


32  

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

:默认并不总是一个解决方案。在更高级的情况下使用这个:

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 %>

#4


17  

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.

注意:如果您故意将一个空字符串作为翻译值,那么它将不起作用。

#5


11  

use :default param:

用途:默认参数:

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

#6


3  

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

#7


2  

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.

NB:这只适用于测试除根键以外的任何东西,因为您正在查看父键。

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"}

#8


2  

Rails 4

Rails 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。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 %>

#1


86  

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

根据你所描述的,这应该是可行的:

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

See the documentation for details.

详情请参阅文档。

#2


41  

You can also use

您还可以使用

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

#3


32  

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

:默认并不总是一个解决方案。在更高级的情况下使用这个:

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 %>

#4


17  

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.

注意:如果您故意将一个空字符串作为翻译值,那么它将不起作用。

#5


11  

use :default param:

用途:默认参数:

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

#6


3  

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

#7


2  

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.

NB:这只适用于测试除根键以外的任何东西,因为您正在查看父键。

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"}

#8


2  

Rails 4

Rails 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。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 %>