The following is written on the Rails Guides:

For example, your config / locales directory could look like this:

|-defaults |---es.rb |---en.rb |-models |---book |-----es.rb |-----en.rb |-views |---defaults |-----es.rb |-----en.rb |---books |-----es.rb |-----en.rb |---users |-----es.rb |-----en.rb |---navigation |-----es.rb |-----en.rb 

It can be used for example. I couldn’t find out what I’m doing.

But nothing is said here about how to store flash message translation files (they are in controllers, and there is not even a folder like this).

Hence the question: how to organize the storage of the translation of flash messages? There is no sense to store in translations for presentations, because the same flash can appear on different pages. Maybe you should keep them in the defaults folder? But I do not think this is the most rational decision, because flash'y themselves are not something "default". Maybe it is worth making a separate controllers folder and storing in it? But then what should be the structure of the file itself?

Something like this?

 en: activecontroller: users: ... 

And anyway, will the rails then find these files, if you contact the controller as t('.myflash) ?

    1 answer 1

    You can do anything in the config/locales as a whole. No folders? Add it! You can arrange translations for individual files as you prefer.

    Flash messages stored in separate files make little sense. The fact that they are set in the controller is rather a technical detail, in the end they are still displayed in the views. But if you think that it will be more convenient, do it separately.

    You can easily check which key will be searched for rails by making binding.pry (with the pry , of course) in an action binding.pry and asking for a deliberately non-existent translation:

     Started GET "/" for 127.0.0.1 at 2016-05-18 17:34:22 +0300 Processing by HomeController#index as HTML From: %project%/app/controllers/home_controller.rb @ line 4 HomeController#index: 2: def index 3: binding.pry => 4: render json: {success: true, arr: [*1..10]} 5: end [1] pry(#<HomeController>)> t '.wheehaa' => "translation missing: en.home.index.wheehaa" 

    This means that in this case it is necessary to place the string like this:

     en: home: index: wheehaa: "Whe-e-eha-aa!" 

    In which file - it does not matter.

    • Thank. Another passing question ask. As you know, validation errors are translated as follows: github.com/svenfuchs/rails-i18n/blob/master/rails/locale/be.yml (line 108). What to do if you add an error yourself? For example: @user.errors.add :current_password, "is incorrect" . - smellyshovel
    • @ Matvey Mamonov in the source code Rails throws a symbol. - D-side
    • Sorry for my ignorance, but I did not quite understand what you mean. You need to add errors like this: @user.errors.add :current_password, :is_invalid ? - smellyshovel
    • @ Matvey Mamonov If you believe the source for the link, then yes. - D-side
    • Well, the source, I think, is always worth believing. Well, thanks for helping me figure it out, otherwise I would not have thought of it myself. - smellyshovel