4

我正在尝试构建动态 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"
4

0 回答 0