Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 779
agmcleod Avatar asked Sep 10 '12 14:09

agmcleod


2 Answers

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

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

See the documentation for details.

like image 54
Chris Salzberg Avatar answered Sep 18 '22 09:09

Chris Salzberg


You can also use

I18n.exists?(key, locale) I18n.exists?('do_i_exist', :en) 
like image 31
albandiguer Avatar answered Sep 18 '22 09:09

albandiguer