有没有办法用rails引用yaml中的常量? [英] is there a way to reference a constant in a yaml with rails?
问题描述
有没有办法让我的 en.yml 文件包含一个常量?
Is there a way to have my en.yml file contain a constant?
# en.yml
foo:
bar:
I love BAZ so much!
# initializers/constants.rb
BAZ = "stackoverflow.com"
I18n.t("foo.bar")
-> "I love stackoverflow.com so much!"
?
如果没有,有没有办法自引用yaml文件?
If not, is there a way to self reference the yaml file?
foo:
bar:
I love *baz* so much!
baz:
stackoverflow.com
I18n.t("foo.bar")
-> "I love stackoverflow.com so much!"
推荐答案
I18N 字符串工具支持插值:
The I18N string tools support interpolation:
I18n.t('foo.bar', :baz => 'stackoverflow.com')
然后在en.yml
中:
foo:
bar:
I love %{baz} so much!
只是不要尝试使用 %{default}
或 %{scope}
作为字符串中的变量,I18n.translate
使用那些用于其他事情的:
Just don't try to use %{default}
or %{scope}
as variables in your strings, I18n.translate
uses those for other things:
如果翻译使用 :default
或 :scope
作为插值变量,则会引发 I18n::ReservedInterpolationKey
异常.
If a translation uses
:default
or:scope
as an interpolation variable, anI18n::ReservedInterpolationKey
exception is raised.
这通常不适用于 YAML,但您的问题似乎专门针对翻译文件.
This doesn't apply to YAML in general but your question seems to be specifically about the translation files.
这篇关于有没有办法用rails引用yaml中的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!