Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to reference a constant in a yaml with rails?

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!"
like image 607
patrick Avatar asked Aug 24 '11 00:08

patrick


1 Answers

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, an I18n::ReservedInterpolationKey exception is raised.

This doesn't apply to YAML in general but your question seems to be specifically about the translation files.

like image 119
mu is too short Avatar answered Nov 19 '22 02:11

mu is too short