|
| 1 | +# I18n::Backend::ActiveRecord |
| 2 | + |
| 3 | +This repository contains the I18n ActiveRecord backend and support code that has been extracted from the "I18n": https://github.com/svenfuchs/i18n. |
| 4 | +It is fully compatible with Rails 3, 4, 5 and 6. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +For Bundler put the following in your Gemfile: |
| 9 | + |
| 10 | +```ruby |
| 11 | +gem 'i18n-active_record', require: 'i18n/active_record' |
| 12 | +``` |
| 13 | + |
| 14 | +After updating your bundle, run the installer |
| 15 | + |
| 16 | + $ rails g i18n:active_record:install |
| 17 | + |
| 18 | +It creates a migration: |
| 19 | + |
| 20 | +```ruby |
| 21 | +class CreateTranslations < ActiveRecord::Migration |
| 22 | + def change |
| 23 | + create_table :translations do |t| |
| 24 | + t.string :locale |
| 25 | + t.string :key |
| 26 | + t.text :value |
| 27 | + t.text :interpolations |
| 28 | + t.boolean :is_proc, default: false |
| 29 | + |
| 30 | + t.timestamps |
| 31 | + end |
| 32 | + end |
| 33 | +end |
| 34 | +``` |
| 35 | + |
| 36 | +To specify table name use: |
| 37 | + |
| 38 | + $ rails g i18n:active_record:install MyTranslation |
| 39 | + |
| 40 | +With the translation model you will be able to manage your translation, and add new translations or languages through |
| 41 | +it. |
| 42 | + |
| 43 | +By default the installer creates a new file in `config/initializers` named `i18n_active_record.rb` with the following content. |
| 44 | + |
| 45 | +```ruby |
| 46 | +require 'i18n/backend/active_record' |
| 47 | + |
| 48 | +Translation = I18n::Backend::ActiveRecord::Translation |
| 49 | + |
| 50 | +if Translation.table_exists? |
| 51 | + I18n.backend = I18n::Backend::ActiveRecord.new |
| 52 | + |
| 53 | + I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize) |
| 54 | + I18n::Backend::Simple.send(:include, I18n::Backend::Memoize) |
| 55 | + I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) |
| 56 | + |
| 57 | + I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend) |
| 58 | +end |
| 59 | +``` |
| 60 | + |
| 61 | +To perform a simpler installation use: |
| 62 | + |
| 63 | + $ rails g i18n:active_record:install --simple |
| 64 | + |
| 65 | +It generates: |
| 66 | + |
| 67 | +```ruby |
| 68 | +require 'i18n/backend/active_record' |
| 69 | +I18n.backend = I18n::Backend::ActiveRecord.new |
| 70 | +``` |
| 71 | + |
| 72 | +You may also configure whether the ActiveRecord backend should use `destroy` or `delete` when cleaning up internally. |
| 73 | + |
| 74 | +```ruby |
| 75 | +I18n::Backend::ActiveRecord.configure do |config| |
| 76 | + config.cleanup_with_destroy = true # defaults to false |
| 77 | +end |
| 78 | +``` |
| 79 | + |
| 80 | +## Usage |
| 81 | + |
| 82 | +You can now use `I18n.t('Your String')` to lookup translations in the database. |
| 83 | + |
| 84 | +## Missing Translations -> Interpolations |
| 85 | + |
| 86 | +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. |
| 87 | + |
| 88 | +The `interpolations` field is otherwise unused since the "value" in `Translation#value` is actually used for interpolation during actual translations. |
| 89 | + |
| 90 | +## Examples |
| 91 | + |
| 92 | +* http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/ |
| 93 | + |
| 94 | +## Maintainers |
| 95 | + |
| 96 | +* Sven Fuchs |
| 97 | +* Tim Masliuchenko |
0 commit comments