Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use I18n from controller in Rails

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?

like image 794
Iván Cortés Romero Avatar asked Oct 28 '15 13:10

Iván Cortés Romero


People also ask

What is i18n in Rails?

The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.

What is i18n t?

Internationalization (i18n) is the process of preparing software so that it can support local languages and cultural settings. An internationalized product supports the requirements of local markets around the world, functioning more appropriately based on local norms and better meeting in-country user expectations.

What is i18n in angular?

Angular Internationalizationlink Internationalization, sometimes referenced as i18n, is the process of designing and preparing your project for use in different locales around the world. Localization is the process of building versions of your project for different locales.

What is locale in Ruby?

Locale gem is the pure ruby library which provides basic and general purpose APIs for localization. This library was called as “Ruby-Locale”. Since 2.0. 6, this library is called just “locale”.


1 Answers

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
like image 168
Mahesh Avatar answered Oct 05 '22 10:10

Mahesh