I need to display "Yes" or "No" in various languages based on whether a expression is true or false. Currently I am doing it like this:
fr:
"yes": Oui
"no": Non
def t_boolean(expression)
(expression) ? t("yes") : t("no")
end
Valid: <%= t_boolean(something.is_valid?) %>
Is there some better way to do this?
Does Rails already have translations for true/false like this?
After reading this, I got inspired and figured out this solution:
fr:
"true": Oui
"false": Non
erb:
Valid: <%= t something.is_valid?.to_s %>
Update
For english, if you want to use yes
and no
as values, be sure to quote them:
en:
"true": "yes"
"false": "no"
Just as Zabba says works fine, but if you are trying to translate true-false into yes-no, quote both sides, else you'll get true translated into true (TrueClass) again.
en:
"true": "yes"
"false": "no"
You may try overriding I18n's default translate
method, delegating to the default method to do the actual translation. Use this code in an initializer:
module I18n
class << self
alias :__translate :translate # move the current self.translate() to self.__translate()
def translate(key, options = {})
if key.class == TrueClass || key.class == FalseClass
return key ? self.__translate("yes", options) : self.__translate("no", options)
else
return self.__translate(key, options)
end
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With