Ruby-on-rails 带有可选参数的 .yml 中的翻译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13178268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Translation in .yml with optional parameter
提问by Bishma Stornelli
I want to make a translation my_translationwith 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?
这可能吗?
回答by Yanhao
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"
回答by Paul Fioravanti
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 datedetermines which string to display, you could perhaps test for its existence in in the paramshash in a controller method, assign the title to a variable, and then use it in a view. Perhaps something like:
由于 的存在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
If you want behaviour exactly as you have described, you'd need two yaml entries anyway, andyou'd have extra convolution, andyou'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.
所以,总而言之,我会说使用两个单独的完整字符串,就像第一个例子一样。
回答by Sebastian Schuchhardt
This is the way i did it!
我就是这样做的!
First set my translation
I18n.t('my_translation', parameter: optional_parameter)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" - if value is 1
=> "This is my translation with an optional parameter which value is 1"
- if value is nil
首先设置我的翻译
I18n.t('my_translation', parameter: optional_parameter)检查值是否为零
optional_parameter = value.nil? "" : "with an optional parameter which value is #{value}" I18n.t('my_translation', parameter: optional_parameter)- 如果值为零
=>"This is my translation" - 如果值为 1
=> "This is my translation with an optional parameter which value is 1"
- 如果值为零

