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?
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.
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.
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.
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”.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With