Ruby on rails 是否可以在YAML翻译键中使用插值?

Ruby on rails 是否可以在YAML翻译键中使用插值?,ruby-on-rails,internationalization,yaml,interpolation,Ruby On Rails,Internationalization,Yaml,Interpolation,假设我在I18n文件中有两个yaml密钥: level1: level2: foo: 'Something' bar: 'Something else' 我会这样引用这些键:t('level1.level2.foo')和t('level1.level2.bar')。如果我有一个变量var,其类型返回'foo'或'bar',我可以做如下操作:t('level1.level2.{var.type}')。现在更棘手的是,如果我已经在字符串插值中使用了键,比如: "The type

假设我在I18n文件中有两个yaml密钥:

level1:
  level2:
    foo: 'Something'
    bar: 'Something else'
我会这样引用这些键:
t('level1.level2.foo')
t('level1.level2.bar')
。如果我有一个变量
var
,其类型返回
'foo'
'bar'
,我可以做如下操作:
t('level1.level2.{var.type}')
。现在更棘手的是,如果我已经在字符串插值中使用了键,比如:

"The type of the #{var.to_s} variable is #{t('level1.level2.#{var.type}')}"

是的,它可以工作,除了要应用的插值外,您需要使用双引号,而不是单引号:

t("level1.level2.#{var.type}")
"The type of the #{var.to_s} variable is #{t("level1.level2.#{var.type}")}"
例如:

[1] pry(main)> var = OpenStruct.new
=> #<OpenStruct>
[2] pry(main)> var.type = 'foo'
=> "foo"
[3] pry(main)> "The type of the #{var.to_s} variable is #{I18n.t("level1.level2.#{var.type}")}"
=> "The type of the #<OpenStruct type=\"foo\"> variable is translation missing: en.level1.level2.foo"
pry(main)>var=OpenStruct.new => # [2] 撬(主)>变量类型='foo' =>“foo” [3] pry(main)>“变量的类型是{I18n.t(“level1.level2.{var.type}”)” =>“缺少#变量的类型:en.level1.level2.foo”
这就是问题所在-我想在YAML键中使用插入,而不是YAML值。老实说,我事先没有尝试过,我几乎可以肯定,{}内的{}不会起作用。谢谢:)