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?
Rails是否已经有像这样的真/假翻译?
7 个解决方案
#1
36
After reading this, I got inspired and figured out this solution:
读完这篇文章之后,我得到了启发并想出了这个解决方案:
fr.yml
fr:
"true": Oui
"false": Non
erb:
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:
对于英语,如果您想使用yes和no作为值,请务必引用它们:
en.yml
en:
"true": "yes"
"false": "no"
#2
10
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.
正如Zabba说的那样工作正常,但如果你试图将真假翻译成是 - 否,引用双方,否则你将真实地再次转化为真(TrueClass)。
en:
"true": "yes"
"false": "no"
#3
1
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:
您可以尝试重写I18n的默认翻译方法,委托默认方法进行实际翻译。在初始化程序中使用此代码:
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
1
# 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 :)
使用Rails 3.2.2 :)
#5
0
Remember that the translate method had been aliased in I18n.
请记住,翻译方法在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.
当您为方法添加别名时,实际上是在为方法创建新副本,因此只有在调用t方法时才重新定义translate方法。为了使上面的代码能够工作,你也可以使用t方法作为别名。
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
0
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
0
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 ^^.
你甚至不需要报价^^。
#1
36
After reading this, I got inspired and figured out this solution:
读完这篇文章之后,我得到了启发并想出了这个解决方案:
fr.yml
fr:
"true": Oui
"false": Non
erb:
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:
对于英语,如果您想使用yes和no作为值,请务必引用它们:
en.yml
en:
"true": "yes"
"false": "no"
#2
10
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.
正如Zabba说的那样工作正常,但如果你试图将真假翻译成是 - 否,引用双方,否则你将真实地再次转化为真(TrueClass)。
en:
"true": "yes"
"false": "no"
#3
1
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:
您可以尝试重写I18n的默认翻译方法,委托默认方法进行实际翻译。在初始化程序中使用此代码:
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
1
# 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 :)
使用Rails 3.2.2 :)
#5
0
Remember that the translate method had been aliased in I18n.
请记住,翻译方法在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.
当您为方法添加别名时,实际上是在为方法创建新副本,因此只有在调用t方法时才重新定义translate方法。为了使上面的代码能够工作,你也可以使用t方法作为别名。
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
0
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
0
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 ^^.
你甚至不需要报价^^。