How do you structure i18n yaml files in Rails?

后端 未结 6 1310
春和景丽
春和景丽 2020-12-07 12:08

I started populating an en yaml file in Rails and I can already tell it will get messy and out of hand before too long. Is there a convention to keeping this file organized?

6条回答
  •  太阳男子
    2020-12-07 13:05

    Coming several years after the battle, but here is a (somewhat totally) different answer.

    To begin with, I do not like the standard t('.xxx') style with the default namespace based on the file structure. I also don't really like the categorisation of translations depending on the DOM structure. While this is a nice approach for very structured translations, it is often repetitive, and not very intuitive.

    I'd rather regroup my translations into more useful categories, so as to make it easier for my translators, because they can work on concrete themes, rather than some weird styles (some translators do not even know what MVC means)

    so my translation file is structured like this

    fr:
      theme1:
        theme11:
          translationxxx: blabla
      theme2:
        translationyyy: blabla
    

    Depending on the needs, the "themes" can be a model, a more abstract context, etc. that is the most intuitive for the translators.

    Because this would be a hassle to write everytime the scope in my views, I have added some convenience methods in my helpers to have a stack based translation context.

    • I push/pop translation scopes on a stack in my views by calling t_scope([new_scope] and pop_t
    • I override the t helper to use the last scope of the stack

    The code for the translation scoping methods is available in that answer

提交回复
热议问题