Reputation: 3618
I have a config file:
# config/meta.yml
base_meta:
title: 'Top 10 Cats'
It has an corresponding initializer:
# config/initializers/meta.rb
META = YAML.load_file("#{Rails.root.to_s}/config/meta.yml")
I can access the title like so:
META['base_meta']['title'] #=> "Top 10 Cats"
However, I want to internationalize my meta data. I believe this should be handled by the existing locales/
yaml files.
How do I reference the existing translation?
# config/locales/en.yml
en:
title: 'Top 10 Cats'
I've tried using erb, but it doesn't work:
# config/meta.yml
base_meta:
title: t(:title)
Renaming the file to config/meta.yml.erb
has no effect either.
Is there a way to reference the I18n keys from my config file?
Upvotes: 3
Views: 2359
Reputation: 114138
Instead of its value, you could add the key for the existing translation in your YAML file:
# config/locales/en.yml
en:
cats:
title: 'Top 10 Cats'
# config/locales/de.yml
de:
cats:
title: 'Top 10 Katzen'
# config/meta.yml
base_meta:
title: 'cats.title'
So it just returns that key:
META['base_meta']['title'] #=> "cats.title"
Which can then be passed to I18n.t
:
I18n.locale = :en
t(META['base_meta']['title']) #=> "Top 10 Cats"
I18n.locale = :de
t(META['base_meta']['title']) #=> "Top 10 Katzen"
Upvotes: 5
Reputation: 382
Try replace in application.rb default value of config.i18n.load_path parameter with that:
config.i18n.load_path += Dir[Rails.root.join('config/locales/**/*.yml').to_s]
It works for me.
Upvotes: -1