Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

I18n ActiveRecord backend

tree: d8c55c4f0d

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.textile

I18n::Backend::ActiveRecord

This repository contains the I18n ActiveRecord backend and support code that has been extracted from the I18n.

Installation

For Bundler put the following in your Gemfile:

  gem 'i18n-active_record',
      :git => 'git://github.com/gitman/i18n-active_record.git',
      :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                 # I dont recommend using key as it seems reserve in some database
        t.text   :value               # same in value so now you can set your on custom name here suppose :t_key and :t_value
        t.text   :interpolations
        t.boolean :is_proc, :default => false

        t.timestamps
      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.

To load 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:

  I18n.backend = I18n::Backend::Database.new
  I18n::Backend::ActiveRecord::Translation.set_key_column('t_key') # put your custom column my is t_key and t_value
  I18n::Backend::ActiveRecord::Translation.set_value_column('t_value')

A more adavanced example (Thanks Moritz), which uses YAML files and ActiveRecord for lookups:

  I18n.backend = I18n::Backend::ActiveRecord.new
  I18n::Backend::ActiveRecord::Translation.set_key_column('t_key') # put your custom column my is t_key and t_value
  I18n::Backend::ActiveRecord::Translation.set_value_column('t_value')

  I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
  I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
  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)

Usage

You can now use I18n.t('Your String') to lookup translations in the database.

Maintainers

  • Sven Fuchs
Something went wrong with that request. Please try again.