17

I have a PetsController in which a flash message is setted. Something like this:

class PetsController

  ...

  def treat_dog
    #do somthing
    flash[:success] = 'Your dog is being treated.'
  end

  ...

end

this controller belongs to Admin, so it is located at: app/controllers/admin/pets_controller.rb. I will use I18n, so I replaced the string in controller with t('controllers.admin.pet.treated'), then,I wrote this yml:

en:
  controllers:
    admin:
      pet:
        treated: "Your dog is being treated."

located at: config/locales/controllers/admin/pet/en.yml and it did not work. I have attempted locating it at config/locales/controllers/admin/pets/en.yml, config/locales/controllers/admin/en.yml config/locales/controllers/en.yml and none of these worked, the translation is not found.

How can I use a translation from this controller?

Iván Cortés
  • 581
  • 1
  • 9
  • 22

4 Answers4

24

In controller you use it like this

I18n.t 'controllers.admin.pet.treated'

Using t() directly enables lazy loading:

t(".treated") #loads from key: controllers.admin.pet.treated
Bijan
  • 25,559
  • 8
  • 79
  • 71
Mahesh
  • 6,378
  • 2
  • 26
  • 35
  • 1
    Yes, I have found that by convention, in controllers I18n translation must be used with I18n.t 'key' instead of t('key') but this last must work too. In my case, exchange it works for me. Thanks Mahesh. – Iván Cortés Oct 30 '15 at 17:55
  • 3
    As explained in the guides http://guides.rubyonrails.org/i18n.html#lazy-lookup you could probably simplify that with lazy lookups using the helper like this `t('.treated')` – dft Mar 15 '17 at 18:00
  • 2
    Bear in mind that both I18n.t 'key' and t('key') will work in controllers, but only the t('key') helper supports lazy loading via the t('.key') shortcut. – Augusto Samamé Barrientos Aug 03 '18 at 22:07
  • ```Using t() directly enables lazy loading:``` Lazing loading (https://en.wikipedia.org/wiki/Lazy_loading) is a completely different concept from what is happening in your second example. Rails calls it "lazy lookup". – user938883 Aug 10 '23 at 20:46
0
def treat_dog
    #do somthing
    flash[:success] = t('controllers.admin.pet.treated')
end
Pardeep Saini
  • 2,084
  • 1
  • 18
  • 29
0

Put it into config/locales/en.yml and it should work (you may need to restart the server).

This guide should help clear the head about I18n. I'm giving link to relevant section, but read it in full: http://guides.rubyonrails.org/i18n.html#adding-translations

If you insist on using nested files, you need to enable it. Documentation says:

The default locale loading mechanism in Rails does not load locale files in nested dictionaries, like we have here. So, for this to work, we must explicitly tell Rails to look further:

# config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • the yml's located at `config/locales/en.yml` are used to set the general or generic tranlations.When you have a lot of tranlations, you can locate them with the same hierarchy order than `view` and `controller` tree. – Iván Cortés Oct 28 '15 at 13:56
  • @IvánCortésRomero: hah, didn't know about it! However, do read that guide. It mentions that nested files are not loaded by default and how to enable it. – Sergio Tulentsev Oct 28 '15 at 14:08
  • I did'n know about that configuration,so I have checked my `config/application.rb` and I found already this line. However, thank youa lot. – Iván Cortés Oct 30 '15 at 16:42
0

In callback it should be:

add_breadcrumb proc{ I18n.t('home_page') }, :root_path
Artem P
  • 5,198
  • 5
  • 40
  • 44