使用可选参数在.yml中进行翻译

时间:2022-05-22 11:13:21

I want to make a translation my_translation with an optional parameter. For example:

我想用可选参数进行翻译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"

Is this possible?

这可能吗?

3 个解决方案

#1


30  

Yes, definitely. You just write the translations like this:

当然是。你只需写下这样的翻译:

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

Is the parameter really optional? In above translation, you have to provide all parameters.

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

UPDATE: Sorry, I answered too soon. I don't think it's easy to do. Maybe the easiest way is like this:

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

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

#2


16  

I would say it is possible, though not recommended. You have two completely separate strings, based on your comments in @Yanhao's answer, and I would say they should be two separate entries in your yaml file:

我会说这是可能的,虽然不推荐。你有两个完全独立的字符串,根据你在@Yanhao的答案中的评论,我会说它们应该是你的yaml文件中的两个单独的条目:

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

Since the existence of the date determines which string to display, you could perhaps test for its existence in in the params hash in a controller method, assign the title to a variable, and then use it in a view. Perhaps something like:

由于日期的存在决定了要显示的字符串,您可以在控制器方法中的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

If you want behaviour exactly as you have described, you'd need two yaml entries anyway, and you'd have extra convolution, and you'd be doing a I18n no-no by creating a string by concatenating two strings together, which assumes a fixed grammatical sentence structure (not to mention this drives translators up the wall):

如果你想要完全按照你所描述的行为,你需要两个yaml条目,并且你有额外的卷积,并且你通过将两个字符串连接在一起创建一个字符串来做一个I18n no-no,它假设一个固定的语法句子结构(更不用说这会驱动翻译人员):

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

with code something like this:

用这样的代码:

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

So, in summary, I'd say go with two separate whole strings, like in the first example.

因此,总而言之,我会说两个单独的整个字符串,就像在第一个例子中一样。

#3


3  

This is the way i did it!

这就是我做的方式!

  1. First set my translation

    首先设置我的翻译

    I18n.t('my_translation', parameter: optional_parameter)
    
  2. Check if value is nil

    检查值是否为零

    optional_parameter = value.nil? "" : "with an optional parameter which value is #{value}"
    I18n.t('my_translation', parameter: optional_parameter)
    
    • if value is nil =>"This is my translation"
    • 如果值为nil =>“这是我的翻译”
    • if value is 1 => "This is my translation with an optional parameter which value is 1"
    • 如果值为1 =>“这是我的翻译,带有可选参数,其值为1”

#1


30  

Yes, definitely. You just write the translations like this:

当然是。你只需写下这样的翻译:

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

Is the parameter really optional? In above translation, you have to provide all parameters.

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

UPDATE: Sorry, I answered too soon. I don't think it's easy to do. Maybe the easiest way is like this:

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

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

#2


16  

I would say it is possible, though not recommended. You have two completely separate strings, based on your comments in @Yanhao's answer, and I would say they should be two separate entries in your yaml file:

我会说这是可能的,虽然不推荐。你有两个完全独立的字符串,根据你在@Yanhao的答案中的评论,我会说它们应该是你的yaml文件中的两个单独的条目:

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

Since the existence of the date determines which string to display, you could perhaps test for its existence in in the params hash in a controller method, assign the title to a variable, and then use it in a view. Perhaps something like:

由于日期的存在决定了要显示的字符串,您可以在控制器方法中的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

If you want behaviour exactly as you have described, you'd need two yaml entries anyway, and you'd have extra convolution, and you'd be doing a I18n no-no by creating a string by concatenating two strings together, which assumes a fixed grammatical sentence structure (not to mention this drives translators up the wall):

如果你想要完全按照你所描述的行为,你需要两个yaml条目,并且你有额外的卷积,并且你通过将两个字符串连接在一起创建一个字符串来做一个I18n no-no,它假设一个固定的语法句子结构(更不用说这会驱动翻译人员):

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

with code something like this:

用这样的代码:

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

So, in summary, I'd say go with two separate whole strings, like in the first example.

因此,总而言之,我会说两个单独的整个字符串,就像在第一个例子中一样。

#3


3  

This is the way i did it!

这就是我做的方式!

  1. First set my translation

    首先设置我的翻译

    I18n.t('my_translation', parameter: optional_parameter)
    
  2. Check if value is nil

    检查值是否为零

    optional_parameter = value.nil? "" : "with an optional parameter which value is #{value}"
    I18n.t('my_translation', parameter: optional_parameter)
    
    • if value is nil =>"This is my translation"
    • 如果值为nil =>“这是我的翻译”
    • if value is 1 => "This is my translation with an optional parameter which value is 1"
    • 如果值为1 =>“这是我的翻译,带有可选参数,其值为1”