anthony anthony - 1 month ago 8
YAML Question

Rails - How to avoid repeating same i18n attributes translations

I am building a Rails application using I18n translations.

I have two models (Blog and Event), sharing same attributes (title, content).

In my I18n yml files, how can I avoid repeating same keys for each attributes models and share them ?

Extract of my actual code:

fr:
activerecord:
attributes:
blog:
title: Titre
content: Contenu
event:
title: Titre
content: Contenu


I also tried to set attributes as default, removing wrapped model key without any luck.

fr:
activerecord:
attributes:
title: Titre
content: Contenu


Thanks for your help !

My project:


  • Rails 4.2.7.1

  • Ruby 2.3.0


Answer

Similar kind of question is answered here

You can achieve it using yaml aliases

fr:
  activerecord:
    attributes:
      blog: &title_content
        title: Titre
        content: Contenu
      event: *title_content

Refer yaml aliases for more info.

Comments