Find file
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (33 sloc) 1.65 KB


Mongoid::I18n is a gem that makes localization almost transparent for the developer.


First, include it in your Gemfile. Remeber to add the :require parameter:

gem 'mongoid_i18n', :require => 'mongoid/i18n'

Now you can add the module to the documents that need localization:

class Entry
  include Mongoid::Document
  include Mongoid::I18n

  localized_field :title # Just use localized_field instead of field
  field :published, :type => Boolean

This will allow you to do stuff like this:

I18n.locale = :en
@entry = => 'Title') # will be stored in the 'en' translation

I18n.locale :es
@entry.title = 'Título' # will be stored in the 'es' translation

@entry.title_translations # this returns a hash: {'en' => 'Title', 'es' => 'Title'}
@entry.title_translations = {'en' => 'New title', 'es' => 'Nuevo título} # You can always mass-assign.

Criteria is handled for you:

I18n.locale = :en
Entry.where(:title => 'Title') # This will search automatically for the 'en' translation
Entry.find(:first, => ['Stuff', 'More stuff']) # find() and complex criteria also works

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.


Copyright © 2010 Rodrigo Álvarez. See LICENSE for details.