Skip to content

Latest commit

 

History

History
56 lines (32 loc) · 1.64 KB

README.rdoc

File metadata and controls

56 lines (32 loc) · 1.64 KB

acts_as_translatable is a Ruby on Rails plugin for easy translation of models and database tables.

Installation

In your Gemfile:

gem 'acts_as_translatable'

And run bundle install.

Example

Generate translations for a category model with name and description fields, with default locale set to ‘en’ (English):

$ rails generate acts_as_translatable en category name description

This will create the necessary migration. Then run:

$ rake db:migrate

Then add to app/models/category.rb:

class Category < ActiveRecord::Base
  acts_as_translatable_on :name, :description
  ...
end

And play around with I18n:

I18n.locale = "en"
Category.first.update_attribute :name, "English name"

I18n.locale = "de"
Category.first.update_attribute :name, "Deutsche Name"

puts Category.first.name     # => "English name"
puts Category.first.name_en  # => "Deutsche Name"
puts Category.first.name_es  # => nil

Category.find_by_name("Programming")
Category.find_by_name_de("Programmierung")

And it supports I18n fallbacks.

Use it in conjunction with the translate_acts_as_translatable_models (lassebunk/translate_acts_as_translatable_models) to automatically translate your acts_as_translatable models.

Have fun! Please send comments and suggestions to [email protected], and issues here at GitHub, please :)

Todo’s

  • Add finder methods such as find_by_name and find_by_name_en.

  • Cache translations on application level using Rails.cache so they won’t be loaded for each translated record?

Copyright © 2012 Lasse Bunk, released under the MIT license