Rails I18n, check if translation exists?

Working with the rails 3 application, where I want to check if a translation exists before outputting it, and if it does not exist, return to some static text. I could do something like:

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

But I feel that there must be a better way. What if the rails in the future change "no translation" to "no translation found". Or what if, for some strange reason, the text contains "no translation." Any ideas?

+54
ruby-on-rails internationalization rails-i18n
Sep 10
source share
9 answers

Based on what you described, this should work:

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

See more details.

+94
Sep 10 '12 at 14:11
source share

You can also use

 I18n.exists?(key, locale) I18n.exists?('do_i_exist', :en) 
+48
Nov 06 '15 at 0:49
source share

:default not always a solution. Use this for more complex 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 %> 
+34
Jun 15 '13 at 14:11
source share

How about this?

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

I just think it feels better, more like no translation

Caution: this does not work if you intentionally have an empty string as the translation value.

+18
Sep 26 '12 at 9:50
source share

use: default param:

 I18n.t("some_translation.key", :default => 'some text') 
+11
Sep 10 '12 at 14:11
source share

sometimes you want to do more things when translating

 v = "doesnt_exist" begin puts I18n.t "langs.#{v}", raise: true rescue ... puts "Nooo #{v} has no Translation!" end 
+3
Apr 20 '14 at 15:21
source share

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

Suppose you have this in your i18n file:

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

You can do it:

 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" 

Note. This only works if you are testing anything other than the root key, as you are looking at the parent.

In fact, what is interesting is what you can get when asking for the parent key ...

 I18n.t('key') # => {:special_value=>"Special value", :default_value=>"Default value"} 
+2
Aug 29 '14 at 16:08
source share

Rails 4

I have been repeating some jury urls. The maximum number of URLs was 2, and default_lang was "de". Here is the barley that I used

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

Here's how I checked if the URL was specified, and if it does not exist for another language, it will return to the "de" I18n default_lang parameter. I used @albandiguer's answer, which worked great.

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 %> 
+2
Jan 14 '16 at 14:09
source share

In some versions back there is an easier way i18 the following documentation> API> t :

You can specify either one key as a string, or several keys as an array of strings. The first one that will allow will be returned.

Example:

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

You can also use contexts . t , if the key is not found in the context, returns the default value.

0
Dec 12 '18 at 17:00
source share



All Articles