【问题标题】:Translation in .yml with optional parameter带有可选参数的 .yml 翻译
【发布时间】:2012-10-22 02:39:35
【问题描述】:

我想使用可选参数进行翻译my_translation。例如:

> I18n.t('my_translation')
=> "This is my translation"
> I18n.t('my_translation', parameter: 1)
=> "This is my translation with an optional parameter which value is 1"

这可能吗?

【问题讨论】:

    标签: ruby-on-rails internationalization yaml


    【解决方案1】:

    是的,当然。您只需像这样编写翻译:

    my_translation: This is my translation with an optional parameter which value is %{parameter}
    

    参数真的是可选的吗?在上面的翻译中,您必须提供所有参数。

    更新:对不起,我回答得太早了。我不认为这很容易做到。也许最简单的方法是这样的:

    > I18n.t('my_translation1')
    => "This is my translation"
    > I18n.t('my_translation2', parameter: 1)
    => "This is my translation with an optional parameter which value is 1"
    

    【讨论】:

    • 是的,它是可选的。我希望它根据参数是否存在而改变。就像我上面的例子一样。
    • 参数是否总是被传递而不是存在与否?这样就可以通过多元化来做到这一点。顺便说一句,这些值是数字吗?
    • 不,这是报告的标题。我想传递一个日期,如果传递了参数,它可能是“我在 %{date} 的报告”,如果没有传递,它可能是“我的报告”。
    【解决方案2】:

    我会说这是可能的,但不推荐。根据@Yanhao 回答中的 cmets,您有两个完全独立的字符串,我想说它们应该是您的 yaml 文件中的两个单独条目:

    report_name: My report
    report_name_with_date: My report on %{date}
    

    由于date 的存在决定了要显示的字符串,您也许可以在控制器方法的params 哈希中测试它的存在,将标题分配给变量,然后在视图中使用它.也许是这样的:

    report_date = params[:report_date]
    if report_date && report_date.is_a?(Date)
      @report_name = I18n.t('report_name_with_date', date: report_date.to_s)
    else
      @report_name = I18n.t('report_name')
    end
    

    如果您想要完全按照您所描述的行为,则无论如何您都需要两个 yaml 条目,并且您将有额外的卷积,并且您将这样做一个 I18n no-no 通过将两个字符串连接在一起创建一个字符串,它假设一个固定的语法句子结构(更不用说这让翻译者陷入困境):

    report_name_with_date: My report%{on_date}
    on_date: on %{date}
    

    代码如下:

    report_date = params[:report_date]
    if report_date && report_date.is_a?(Date)
      on_date = I18n.t('on_date', date: report_date.to_s)
      @report_name = I18n.t('report_name_with_date', on_date: " #{on_date}")
    else
      @report_name = I18n.t('report_name_with_date', on_date: nil)
    end
    

    所以,总而言之,我会说使用两个单独的完整字符串,就像在第一个示例中一样。

    【讨论】:

      【解决方案3】:

      我就是这样做的!

      1. 先设置我的翻译

        I18n.t('my_translation', parameter: optional_parameter)
        
      2. 检查值是否为零

        optional_parameter = value.nil? "" : "with an optional parameter which value is #{value}"
        I18n.t('my_translation', parameter: optional_parameter)
        
        • 如果值为 nil =>"This is my translation"
        • 如果值为 1 => "This is my translation with an optional parameter which value is 1"

      【讨论】:

      • "with an optional parameter which value is "的翻译在哪里?......
      【解决方案4】:

      如果您使用 number 作为可选参数,Rails 提供了更好的处理方式。

      例如

        invoice:
          zero: "Great! You have no pending invoices."
          one: "You have only 1 pending invoice."
          other: "You have %{count} pending invoices."
        
        >> t("invoice", count: 0) 
        => Great! You have no pending invoices.
      
        >> t("invoice", count: 1)
        => You have only 1 pending invoice.
      
        >> t("invoice", count: 5) 
        => You have 5 pending invoices.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-30
        相关资源
        最近更新 更多