Ruby on Rails + Devise + I18n: how to install the language?

I use before_filter in ApplicationController to set the language for my application:

class ApplicationController < ActionController::Base protect_from_forgery before_filter :set_locale def set_locale I18n.locale = request.compatible_language_from ["uk", "ru", "de", "en"] end end 

It works for controllers that are written by me. But all the finished messages are still English.

Setting config.i18n.default_locale = "uk" (or another) in config/application.rb works, so I think the problem is that the developer is not using my before_filter file (maybe it does not inherit ApplicationController (?) At all) .

How to solve this problem? How to make an application using my language?

+6
source share
3 answers

Take a look at the Devise Wiki https://github.com/plataformatec/devise/wiki/I18n They have many sample YML files.

If you still want to write your own, try using something like this in your I18n files.

 en: devise: sessions: signed_in: 'Signed in successfully.' 

More info on GitHub https://github.com/plataformatec/devise#i18n

+3
source

You need to use prepend_before_action (or prepend_before_filter , but it's an alias prepend_before_action and will be deprecated soon), so you should have something like:

 class ApplicationController < ActionController::Base protect_from_forgery prepend_before_action :set_locale private def set_locale I18n.locale = request.compatible_language_from [:uk, :ru, :de, :en] end end 

Note that this may break I18n.locale in your views, so you may need to set it in an optional before_action .

0
source

I had this problem when my French developer locales were loading for everyone, and the problem was that my local developments were originally built in my own file - devise.en.yml . I moved them to the en.yml file and everything was fixed.

Hope this helps someone in the future!

0
source

Source: https://habr.com/ru/post/919692/


All Articles