This repository contains the I18n Sequel 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-sequel', :require => 'i18n/sequel'
Next create a Sequel model named Translation
with the Rails Generator.
Your migration should look like this:
class CreateI18nTranslationsMigration < Sequel::Migration def up create_table :translations do primary_key :id String :locale, null: false String :key, null: false String :value, text: true String :interpolations, text: true TrueClass :is_proc, null: false, default: false index [:locale, :key], :unique => true end end def 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.
To load I18n::Backend::Sequel
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/sequel' I18n.backend = I18n::Backend::Sequel.new
A more advanced example (Thanks Moritz), which uses YAML files and Sequel for lookups:
require 'i18n/backend/sequel' Translation = I18n::Backend::Sequel::Translation if Translation.table_exists? I18n.backend = I18n::Backend::Sequel.new I18n::Backend::Sequel.send(:include, I18n::Backend::Memoize) I18n::Backend::Simple.send(:include, I18n::Backend::Memoize) I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend) end
You may also configure whether the Sequel backend should use destroy
or delete
when cleaning up internally.
I18n::Backend::Sequel.configure 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.
The interpolations field in the Translations table is used by I18n::Backend::Sequel::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.
- Jim Garvin
Sven Fuchs, Tim Masliuchenko, and others produced the majority of this code, and deserve the majority of the credit for it’s existence. Jim Garvin merely ported it from the ActiveRecord ORM to the Sequel ORM.