开发者

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

开发者 https://www.devze.com 2023-03-30 10:57 出处:网络
Is there a way to have my en.yml file开发者_StackOverflow中文版 contain a constant? # en.yml foo:

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, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消