【问题标题】:Is there an existing I18N translation for booleans?布尔值是否存在现有的 I18N 翻译?
【发布时间】:2011-07-10 20:56:45
【问题描述】:

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

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 是否已经有这样的真/假翻译?

【问题讨论】:

    标签: ruby-on-rails-3 internationalization


    【解决方案1】:

    阅读this后,我受到启发并想出了这个解决方案:

    fr.yml

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

    erb:

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

    更新

    对于英语,如果您想使用 yesno 作为值,请务必引用它们:

    en.yml

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

    【讨论】:

    • 这里有一个较短的布尔翻译代码:Valid: &lt;%= t( something.is_valid?.present?.to_s ) %&gt;
    • 对于意大利语,你必须使用双引号 "true":"si" 和 "false":"no"。
    【解决方案2】:

    正如 Zabba 所说的工作正常,但如果你试图将 true-false 转换为 yes-no,请引用双方,否则你会再次将 true 转换为 true (TrueClass)。

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

    【讨论】:

      【解决方案3】:

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

      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)
      

      使用 Rails 3.2.2 :)

      【讨论】:

        【解决方案5】:

        请记住,translate 方法在 I18n 中具有别名。

        当您为方法设置别名时,实际上是在创建它的新副本,因此当调用 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
        

        【讨论】:

        • rails 3.2 对我不起作用,知道吗?我把这段代码放在初始化器中
        • 除了重新定义 t 和 transalate 方法之外,你应该做 "fr: "yes": Oui "no": Non
        • 我仍然得到是/否,我正在与 activeadmin 合作,这可能是问题所在。但我认为它是独立的。
        【解决方案6】:

        我更喜欢的其他解决方案:

        # 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】:

          对于任何布尔翻译

          我只是喜欢布尔复数化技巧

          # 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"
          

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

          【讨论】:

            猜你喜欢
            • 2012-09-03
            • 2023-01-25
            • 1970-01-01
            • 2011-10-13
            • 1970-01-01
            • 2012-11-11
            • 2012-02-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多