This repository contains the I18n ActiveRecord backend and support code that has been extracted from the I18n. It is fully compatible with Rails 3, 4 and 5
For Bundler put the following in your Gemfile:
gem 'i18n-active_record', :require => 'i18n/active_record'
Next create a active record model named
Translation with the Rails Generator.
Your migration should look like this:
class CreateTranslations < ActiveRecord::Migration def self.up create_table :translations do |t| t.string :locale t.string :key t.text :value t.text :interpolations t.boolean :is_proc, :default => false t. end end def self.down drop_table :translations end end
With this translation model you will be able to manage your translation, and add new translations or languages through it.
I18n::Backend::ActiveRecord into your Rails application, create a new file in config/initializers named locale.rb.
A simple configuration for your locale.rb could look like this:
require 'i18n/backend/active_record' .backend = ::::.new
A more advanced example (Thanks Moritz), which uses YAML files and ActiveRecord for lookups:
require 'i18n/backend/active_record' Translation = :::::: if Translation.table_exists? .backend = ::::.new ::::.send(:include, ::::Memoize) ::::Simple.send(:include, ::::Memoize) ::::Simple.send(:include, ::::Pluralization) .backend = ::::Chain.new(::::Simple.new, .backend) end
You may also configure whether the ActiveRecord backend should use
delete when cleaning up internally.
::::. do |config| config.cleanup_with_destroy = true # defaults to false end
You can now use
I18n.t('Your String') to lookup translations in the database.
Missing Translations → Interpolations
The interpolations field in the Translations table is used by I18n::Backend::ActiveRecord::Missing to store the interpolations seen the first time this Translation was requested. This will help translators understand what interpolations to expect, and thus to include when providing the translations.
The interpolations field is otherwise unused since the “value” in Translation.value is actually used for interpolation during actual translations.
- Sven Fuchs
- Tim Maslyuchenko