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!"
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With