Changing Locale - activeadmin/activeadmin GitHub Wiki
Specifying Locale
You just need to set I18n.locale in a before_action
.
Add the following line to config/initializers/active_admin.rb
:
#config/initializers/active_admin.rb
config.before_action :set_admin_locale
or if you are using Rails 5.1+:
#config/initializers/active_admin.rb
config.before_action :set_admin_locale
and define the following method in your application_controller.rb
:
def set_admin_locale
I18n.locale = :en
end
Of course, put whatever logic you want into the set_admin_locale
method. If your AdminUser
class has a locale
field to store the user's locale, you can do:
def set_admin_locale
I18n.locale = current_admin_user && current_admin_user.locale || I18n.default_locale
end
Switching Locale
If your use case is to be able to switch locale with language links and persist this locale, here is a way.
You set a default locale in a before filter by adding the following lines to application_controller.rb
:
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
or follow the Specifying locale guide.
Let's add a menu for selecting languages in active_admin.rb
:
config.namespace :admin do |admin|
admin.build_menu :utility_navigation do |menu|
menu.add :label => "Languages" do |lang|
lang.add :label => "English",:url => proc { url_for(:locale => 'en') }, id: 'i18n-en', :priority => 1
lang.add :label => "Spanish",:url => proc { url_for(:locale => 'es') }, id: 'i18n-es', :priority => 2
end
menu.add :label => proc { display_name current_active_admin_user },
:url => '#',
:id => 'current_user',
:if => proc { current_active_admin_user? }
admin.add_logout_button_to_menu menu
end
end
Prefix url with the locale in routes.rb
:
scope ":locale", :path_prefix => '/:locale' do
ActiveAdmin.routes(self)
end
Edit: For a rails 4, use this instead:
scope ':locale', defaults: { locale: I18n.locale } do
ActiveAdmin.routes(self)
end
You now have an active admin app that persists locale between queries and permits the user to choose his locale.
You will notice, however, that all links keep the default locale of your app.
You set the default locale in application.rb
:
config.before_configuration do
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
I18n.default_locale = :en
I18n.reload!
end
You can override this default locale by passing the locale to all _path
methods.
The example below reproduces the default actions of active admin including cancan authorization and the locale selected by the user. Let's say we have a AA page customers.rb
:
ActiveAdmin.register Customer do
menu :priority => 2, url: ->{ app_customers_path(locale: I18n.locale) } # Pass the locale to the menu link
action_item do
link_to I18n.t("New Customer"), new_app_customer_path(locale: I18n.locale) # Pass the locale to the new button
end
index do
column :firstname
column :lastname
# Reproduce the default actions of AA, authorization with cancan, pass the locale
column I18n.t("Actions") do |customer|
link_to(I18n.t("Add Coupon"), new_app_coupon_path(:customer_id => customer.id, :locale => I18n.locale )) + " | " + \
link_to(I18n.t("Add Order"), new_app_order_path(:customer_id => customer.id, :locale => I18n.locale))
end
actions defaults: false do |customer|
links = ''.html_safe
if can?(:read, customer)
links += link_to I18n.t('active_admin.view'), app_customer_path(customer, locale: I18n.locale), class: "member_link view_link"
end
if can?(:update, customer)
links += link_to I18n.t('active_admin.edit'), edit_app_customer_path(customer, locale: I18n.locale), class: "member_link edit_link"
end
if can?(:destroy, customer)
links += link_to I18n.t('active_admin.delete'), app_customer_path(customer, locale: I18n.locale), :method => :delete, :confirm => I18n.t('active_admin.delete_confirmation'), class: "member_link delete_link"
end
links
end
end