fesplugas / simplified_translation

Rails translation plugin for your model contents.

This URL has Read+Write access

pantulis (author)
Wed Jul 15 02:07:03 -0700 2009
fesplugas (committer)
Fri Sep 04 08:58:32 -0700 2009
commit  129c0531a87a6ed140056002001905935d9d6f36
tree    a0db05ecdc8f5bc57cd257e132ececd04dbf292b
parent  8bd69ce0821cbcfb21a093802e8942d6621d766d
name age message
file MIT-LICENSE Thu Apr 23 00:52:44 -0700 2009 Update copyright years. [Francesc Esplugas]
file README.rdoc Thu Apr 23 00:52:44 -0700 2009 Update copyright years. [Francesc Esplugas]
file Rakefile Thu Apr 23 00:48:02 -0700 2009 Change markdown to rdoc. [Francesc Esplugas]
file init.rb Fri May 01 03:17:01 -0700 2009 Don't overwrite a Rails method ... use _() with... [Francesc Esplugas]
directory lib/ Fri Sep 04 08:58:32 -0700 2009 Better dynamic finders. Signed-off-by: Frances... [pantulis]
directory tasks/ Wed Dec 24 06:46:00 -0800 2008 Updated tasks name, fixed problem with encoding... [Francesc Esplugas]
directory test/ Fri Sep 04 08:58:32 -0700 2009 Better dynamic finders. Signed-off-by: Frances... [pantulis]
README.rdoc

Simplified Translation

The main idea of this plugin is to keep all field translations on the same table. I’ve worked with other Rails plugins but all of them have some caveats, and I needed something really simple.

With the release of Rails 2.2.X, with the I18n integrated now it’s easier to translate an Rails application, GetText is no longer needed. So I’ve added a few tasks which allow to extract translatable text from the models, views and controllers.

    fesplugas@sueisfine:test_i18n $ rake -T i18n
    (in /home/fesplugas/projects/simplified_plugins)
    rake locales:autofill  # Autofill missing translations
    rake locales:list      # List current locales
    rake locales:missing   # List missing translation strings
    rake locales:update    # Update locale files

Quick explanation

We have a page model with the following attributes.

    Page: title_en, title_cat, title_es

By default Rails defines the current locale as :en and the default locale to :en. If we add translate :title to our model we can get the content of the attribute without defining the locale, and we’ll get the localized field.

    @page_title = Page.find(:first).title

We can change the locale with I18n.locale.

    I18n.locale = :cat
    @page_title = Page.find(:first).title

And we’ll get the title attribute of the defined locale.

Large example

    ##
    # app/views/welcome/index.html.erb
    #
    <%=t "Today is {{value}}.", :value => Date.today %>

    ##
    # app/controllers/welcome_controller.rb
    #
    class WelcomeController < ApplicationController

      def index
        @text = translate "Today is {{value}}.", :value => Date.today
      end

    end

    ##
    # app/controllers/application.rb
    #
    class ApplicationController < ActionController::Base

      before_filter :set_locale

      def set_locale
        I18n.locale = params[:locale] || 'es'
      end

    end

Sample migration

    class CreatePages < ActiveRecord::Migration

      def self.up
        create_table :pages do |t|
          t.string :title_en, :null => false
          t.string :title_es, :null => false
          t.string :title_cat, :null => false
          t.text :content_en, :null => false
          t.text :content_es, :null => false
          t.text :content_cat, :null => false
          t.boolean :status, :null => false, :default => false
        end
      end

      def self.down
        drop_table :pages
      end

    end

Model definition

    ##
    # app/models/page.rg
    #
    class Page < ActiveRecord::Base
      translate :title, :body
    end

Usage from the console

    $ script/console
    Loading development environment (Rails 2.2.2)
    >> I18n.default_locale
    => :en
    >> I18n.locale
    => :en
    >> Page.first.title
    => "Hello World"
    >> I18n.locale = :es
    => :es
    >> Page.first.title
    => "Hola Mundo"
    >> Page.first.update_attributes :title => "Hola Mundo"
    => true
    >> Page.first.title
    => "Hola Mundo"
    >> I18n.locale = :undefined
    => :undefined
    >> Page.first.title
    => "Hello World"    <== Because is the default locale

Copyright © 2008-2009 Francesc Esplugas Marti, released under the MIT license