Acts As Localizable
Acts As Localizable is an alternative to the globalize series of plugins. It enables the ability to localize fields in an ActiveRecord model.
It has the ability to be used with I18n or independently of it and is compatible with Rails 3.
Requirements
ActiveRecord
I18n
Rails( > 3.0.0beta4)
To install Acts As Localizable you can install it as a gem.
gem install acts_as_localizable
Once the gem is installed you need to create the LocalizedField table in your project. To do this run:
rails generate localized_fields
rake db:migrate
Translating Fields
In order for Acts As Localizable to work you must first notify the plugin which models you would like to localize. You can do this by simply adding a line to the top of your model:
class Post < ActiveRecord::Base
acts_as_localizable
end
This allows you to look at fields or set fields by using a localized property:
post.localized.title # => "Love"
post.localized(:es).title # => "Amor"
I18n.locale = :es
post.localized.title # => "Amor"
As can be seen above, you can specify which language you are requesting as an argument to the localized method, or if none is specified, it will use the locale set in the I18n gem.
To set a local title:
post.localized(:es).title = "Amor"
post.save