Translation
Overviewâ
We can convert certain keys to the language the application needs.
Installationâ
rails generate 'i18n:active_record:install'
Usageâ
- Create a migration to add a column for locale for the user.
class AddLocaleToUser < ActiveRecord::Migration[7.2]
def change
add_column :users, :locale, :string, default: 'en'
end
end
- Add the following line to the
config/initializers/i18n_active_record.rbfile.
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.cleanup_with_destroy = true # defaults to false
config.scope = 'cm_admin' # defaults to nil, won't be used
end
- We can create a translation by going to
/cm_admin/translations.
- key - The text that needs to be converted. Eg: Edit
- value - The value that needs to be applied. Eg: ÙŰ۱۱
- locale - The language in which the text needs to be converted. Eg: ar
- Add this method to the 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
Important Notesâ
- Success messages are not converted yet.