Is there a way to have my en.yml file开发者_StackOverflow中文版 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!"
?
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!"
The I18N string tools support interpolation:
I18n.t('foo.bar', :baz => 'stackoverflow.com')
And then in the en.yml
:
foo:
bar:
I love %{baz} so much!
Just don't try to use %{default}
or %{scope}
as variables in your strings, I18n.translate
uses those for other things:
If a translation uses
:default
or:scope
as an interpolation variable, anI18n::ReservedInterpolationKey
exception is raised.
This doesn't apply to YAML in general but your question seems to be specifically about the translation files.
精彩评论