Skip to content

Latest commit

 

History

History
88 lines (50 loc) · 2.55 KB

README.rdoc

File metadata and controls

88 lines (50 loc) · 2.55 KB

has_foreign_language

has_foreign_language makes it easy to internationalize your database without mucking up your views and controllers or having to write a bunch of YAML. Just create fields for each locale you want to support in addition to your default field and has_foreign_language will automatically call the appropriate one based on the current locale.

Installation

Using Rails 3

Include the gem in your Gemfile:

gem “has_foreign_language”, :git => “git://github.com/dalpo/has_foreign_language.git”

Or as a plugin:

rails plugin install git://github.com/dalpo/has_foreign_language.git

Example

First create your model:

rails g model book title:string description:text

Then specify which fields need to be internationalized in your model:

class Country < ActiveRecord::Base
  has_foreign_language :title, :description
end

Finally generate a migration with internationalized fields:

Sintax:

rails g has_foreign_language <language> [Model]

Example:

rails g has_foreign_language it

Now let’s create a book in the console and see how this works:

I18n.default_locale = "en"
b = Book.new(:title => "Slaughterhouse Five", :title_fr => "Abattoir Cinq", :title_de => "Schlachthof Fünf")
b.title # Slaughterhouse Five

I18n.locale = "fr"
b.title # Abattoir Cinq

I18n.locale = "de"
b.title # Schlachthof Fünf

Calling title on the model returns the appropriate column depending on which locale we’re in. This also works when we change the attribute:

I18n.locale = "de"
b.title = "Etwas Anderes"
b # Book id: nil, title: "Slaughterhouse Five", title_fr: "Abattoir Cinq", title_de: "Etwas Anderes"

If you want to set or return the default language attribute while in a different locale, you can call it the way you would call any other. Assuming the default is “en”, you would run:

b.title_en # Slaughterhouse Five
b.title_en = "Something Else" # Book id: nil, title: "Something Else", title_fr: "Abattoir Cinq", title_de: "Etwas Anderes"

Validations

Validations are easy. Just run the validation on the default field name and it will apply to whatever locale you’re in.

class Country < ActiveRecord::Base
  has_foreign_language :title
  validates_presence_of :title
end

I18n.locale = "de"
b = Book.new(:title => "Etwas Anderes") # Book id: nil, title: nil, title_fr: nil, title_de: "Etwas Anderes"
b.valid? # true

I18n.locale = "en"
b.valid? # false

b.title = "Slaughterhouse Five"
b.valid? # true

Copyright © 2008 factor design initiative, released under the MIT license