Is there an existing I18N translation for booleans

2020-05-20 03:32发布

问题:

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.yml:

fr:
  "yes": Oui
  "no": Non

a helper method:

def t_boolean(expression)
  (expression) ? t("yes") : t("no")
end

erb:

Valid: <%= t_boolean(something.is_valid?) %>

Is there some better way to do this?

Does Rails already have translations for true/false like this?

回答1:

After reading this, I got inspired and figured out this solution:

fr.yml

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.yml

en:
  "true": "yes"
  "false": "no"


回答2:

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"


回答3:

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


回答4:

# Inside initializer
module I18n
  class << self
    alias :__translate :translate #  move the current self.translate() to self.__translate()
     alias :t :translate
     def translate(key, options = {})
       if key.class == TrueClass || key.class == FalseClass
         return key ? self.__translate("boolean.true", options) : self.__translate("boolean.false", options)
       else
         return self.__translate(key, options)
       end
     end
  end
end

# Inside locale
boolean:
  :true: 'Yes'
  :false: 'No'

# Calling translate
I18n.translate(is_this_my_boolean_column)

Working with Rails 3.2.2 :)



回答5:

Remember that the translate method had been aliased in I18n.

When you alias a method you are actually creating a new copy of it so only redefining the translate method will not work when calls to the t method occurs. In order to make the above code to work you could, for example, alias the t method, too.

module I18n
  class << self
    alias :__translate :translate #  move the current self.translate() to self.__translate()
    alias :t : translate # move the current self.t() 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


回答6:

Other solution I prefer:

# Create a helper
def yes_no(bool_value)
  if bool_value
    t(:yes_word)
  else
    t(:no_word)
  end
end

# Add the translations, important that you use " around yes or no.
yes_word: "No"
no_word: "Yes"

# In your views, in my case in slim:
span= yes_no myvalue
# Or ERB
<%= yes_no(myvalue) %>


回答7:

For any boolean translation

I just love that boolean pluralization hack

# some_view.html.erb
t(:are_you_ok?, count: (user.is_ok? ? 0 : 1)  ).html_safe

Translations

# locales/en.yml
en:
  are_you_ok?:
    zero: "You are <strong>NOT</strong> ok ! Do something !"
    one: "You are doing fine"

You don't even need quotes actually ^^.