Configure Rails Admin with i18n

1 Min. Read
Aug 8, 2017

By default, its not that easy to configure your application with i18n. If you wish to support your local language like esp, np, hi, mx, etc. you need to follow the steps i tell you.

Configure your routes.rb

Need to configure your routes.rb to encapsulate the controller with locale info

1
2
3
4
5
6
7
8
9
10
11
12
  # routes.rb
  scope "(:locale)", locale: /en|np/ do
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin'

    devise_for :users
    root 'home#index'
    resource :dashboard do
      collection do
        get :report
      end
    end
  end

Configure locale in each request

Set the locale for each request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  # initialize/rails_admin.rb
  RailsAdmin.config do |config|

  ### Popular gems integration

  ## == Devise ==
  config.authenticate_with do
    default_locale =  :np

    # Set locale per request
    I18n.locale = params[:locale] || default_locale

    if current_user.blank? || (current_user.has_role?('super_admin') == false)
        flash[:error] = 'You are not authorized to access this resource.'
      redirect_to '/'
    end
  end

  config.current_user_method(&:current_user)

Copy yml file to your project

copy .rvm/gems/ruby-2.6.3/gems/rails_admin-1.4.2/config/locales/rails_admin.en.yml or https://raw.githubusercontent.com/sferik/railsadmin/master/config/locales/railsadmin.en.yml

to /config/locale/rails_admin.np.yml or rails_admin.[:locale].yml

Convert the file to your locale.

Common Error

You might encounter a msg in view translation missing for np.datetime.distance_in_words.x_days

add the following to the rails_admin.np.yml file

1
2
3
4
5
np:
  datetime:
    distance_in_words:
      x_days: "%{count} दिन"
      about_x_hours: "%{count} घण्टा"

or you can copy this file to config/locale of nepali translations.

https://gist.github.com/shivabhusal/84b4d9365bc88843c5249aa41ff57688