i18n_column

Introduction

This extension provides the capabilities of storing and retrieving translations from a single database column. The translations are stored as a JSON object i.e. “en”:“Home”,“de”:“Zuhause”.

The current and default language are stored within the class I18nColumn::Language. On each request the current language must be set via I18nColumn::Language.current_lang = 'en'. If not set the default language is taken. I18nColumn::Language.current_lang is used as the JSON key to store a translation i.e. “en”:“Home”.

Installation

  • Recently a gem version will be provided on gemcutter.

  • Run rails generate i18n_column:install. It will generate a i18n_column initializer file in order to set the default language.

Example

Migration

class CreateNodes < ActiveRecord::Migration
  def self.up
    create_table(:nodes) do |t|
      t.text(:name)
    end
  end

  def self.down
    drop_table :nodes
  end
end

Model

class Node < ActiveRecord::Base
  i18n_column(:name)
end

Controller

class ApplicationController < ActionController::Base
  before_filter(:set_i18n_column_lang)

  private

  def set_i18n_column_lang
    I18nColumn::Language.current_lang = params[:lang]
  end
end

Console

I18nColumn::Language.current_lang = 'en'
node = Node.create!(:name => 'Home') => {"en":"Home"}
node.name => 'Home'
I18nColumn::Language.current_lang = 'de'
node.name = 'Zuhause'
node.save! => {"en":"Home","de":"Zuhause"}
node.name => 'Zuhause'

Copyright © 2010 Philipp Ullmann. See LICENSE for details.