Skip to main content

Translations

cm-admin uses i18n-active_record so admins can override labels in any language at runtime, without editing locale files.

Setup​

1. Install the i18n-active_record backend​

rails generate i18n:active_record:install

2. Add a locale column to your users table​

class AddLocaleToUser < ActiveRecord::Migration[7.2]
def change
add_column :users, :locale, :string, default: "en"
end
end

3. Chain the database backend in front of the default one​

config/initializers/i18n_active_record.rb
require "i18n/backend/active_record"

I18n.backend = I18n::Backend::Chain.new(
I18n::Backend::ActiveRecord.new, # DB-backed translations (editable at runtime)
I18n.backend # fallback to the default Simple backend
)

I18n::Backend::ActiveRecord.configure do |config|
config.cache_translations = true # defaults to false
config.scope = "cm_admin" # defaults to nil
end

4. Switch the locale per request​

In your cm-admin application controller:

around_action :switch_locale

def switch_locale(&action)
locale = params[:locale] || Current.user.locale || I18n.default_locale
I18n.with_locale(locale, &action)
end

How it works​

The I18n::Backend::Chain checks the database first β€” anything an admin saved on the Translations page wins β€” then falls back to the bundled locale files. config.scope = "cm_admin" keeps these keys separate from your app's own translations.

Recipes​

Add a translation​

Open /cm_admin/translations and create a translation with:

  • key β€” the source text, e.g. Edit
  • value β€” the translated text, e.g. يحرر
  • locale β€” the language code, e.g. ar
note

Success messages are not translated yet.