扩展YAML别名锚点

时间:2013-03-08 14:55:58

标签: ruby-on-rails-3 yaml rails-i18n string-interpolation

我正在尝试构造动态YAML来返回字符串,我想知道是否有可能用YAML做这样的事情?

en:
  occupation: "Studying #{department} at %{university} %{*speciality}"
  speciality: &speciality "specialising in %{subject}"

预期产出

I18n.t(:occupation, :department => "Computer Science", 
                    :university => "Staffordshire University", 
                    :subject    => "Ruby")
#=> "Studying Computer Science at Staffordshire University specialising in Ruby"

I18n.t(:occupation, :department => "Computer Science", 
                    :university => "Staffordshire University", 
                    :subject    => nil)
#=> "Studying Computer Science at Staffordshire University"

目前,我认为这可能是唯一可行的方式是

subject = "Ruby"
I18n.t(:occupation, :department => "Computer Science", 
                    :university => "Staffordshire University", 
                    :subject    => (
                      I18n.t(:speciality, :subject => subject) if subject
                    ))
#=> "Studying Computer Science at Staffordshire University specialising in Ruby"

0 个答案:

没有答案