Shiva Bhusal
Shiva's Blog

Follow

Shiva's Blog

Follow

Configure Rails Admin with i18n

Shiva Bhusal's photo
Shiva Bhusal
·Aug 8, 2017·

1 min read

Play this article

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

  # 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

  # 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/rails_admin/master/config/locales/rails_admin.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

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

 
Share this