Is it possible to set a default locale of controller in Rails. Suppose the application supports different language like, fr, nl, en, cn etc and I want to use 'en' as a default language for the Admin Dashboard Controller. Please suggest
Asked
Active
Viewed 3,341 times
2 Answers
7
Just use a before_action
callback to set the default locale.
class Admin::DashboardController
before_action :set_default_locale
# ...
private
def set_default_locale
I18n.default_locale = :en
end
end

max
- 96,212
- 14
- 104
- 165
-
When i sign in to Admin Dashboard default locale doesn't set to :en – Santosh Aryal May 16 '16 at 15:01
-
Well did you add this code to the correct controller? – max May 16 '16 at 15:02
-
Have you tried using a log statement or any other debugging methods to check that set_default_locale is actually being run? – max May 16 '16 at 15:06
3
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
more details in link and http://guides.rubyonrails.org/i18n.html

Community
- 1
- 1

gabrieltong
- 46
- 4
-
I have set locale locale rule in application.rb but I want to set :en as default locale for a DashboardController – Santosh Aryal May 16 '16 at 14:43