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