是否存在针对布尔值的I18N翻译?

23

根据表达式的真假,我需要显示各种语言中的“是”或“否”。目前我是这样做的:

fr.yml:

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

一个辅助方法:

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

erb:

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

有没有更好的方法来做这件事?

Rails是否已经有了类似这样的true/false翻译?

7个回答

40

阅读完这篇文章后,我受到启发并想出了以下解决方案:

fr.yml

fr:
  "true": Oui
  "false": Non

erb:

Valid: <%= t something.is_valid?.to_s %>

更新

对于英语,如果您想使用yesno作为值,请确保将它们加上引号:

en.yml

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

这里有一个更短的布尔值转换代码:有效: <%= t(something.is_valid?.present?.to_s)%> - Rubén Norte
同时,在使用意大利语时,您必须使用双引号来表示“true”:“si”和“false”:“no”。 - Simonini

10

正如Zabba所说,这个方法很好用。但是如果你想把true和false翻译成yes和no,请两边都加上引号,否则true会再次被翻译成TrueClass。

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

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)

使用Rails 3.2.2 :)


1

您可以尝试覆盖I18n的默认translate方法,并委托默认方法执行实际的翻译。在初始化程序中使用以下代码:

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

谢谢,我相信那会在某个时候很有用!但现在,我发现了一个简单的解决方案(已添加为答案)。 - Zabba

0

记住,在I18n中,translate方法已被别名。

当你给一个方法起别名时,实际上是创建了一个新的副本,因此仅重新定义translate方法并不能在调用t方法时生效。 为了使上述代码正常工作,你可以例如也给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

我的Rails 3.2版本无法运行,有什么想法吗?我将这段代码放在初始化器中。 - Sebastien
除了重新定义t和transalate方法之外,您还应该执行“fr: “yes”:是 “no”:否 - Pikachu
我仍然得到是/否的答案,我正在使用activeadmin工作,这可能是问题所在。但我认为它是独立的。 - Sebastien

0

我更喜欢的另一种解决方案:

# 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) %>

0
任何布尔翻译:
我只是喜欢那个布尔复数的技巧。
# some_view.html.erb
t(:are_you_ok?, count: (user.is_ok? ? 0 : 1)  ).html_safe

翻译

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

实际上,你甚至不需要引号^^。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接