Rails I18n, check if translation exists?

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:

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?

42155 次浏览

use :default param:

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

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

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

See the documentation for details.

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.

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

helpers/application.rb:

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

any ERB template:

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

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

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

Assuming you have this in your i18n file:

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

You can also use

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

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

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.

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

Some versions ago there is a easier way i18next documentation > 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'

Also you can use Contexts. t if not found a key into a context returns the default value.