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
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"}
rchampourlier Aug 29 '14 at 16:08 2014-08-29 16:08
source share