Skip to content

Mongoid add-on to deal with localizable fields

License

Notifications You must be signed in to change notification settings

milep/mongoid_i18n

 
 

Repository files navigation

mongoid_i18n

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

Usage

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
end

This will allow you to do stuff like this:

I18n.locale = :en
@entry = Entry.new(:title => '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, :title.in => ['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.

About

Mongoid add-on to deal with localizable fields

This gem aims to be a transparent way to deal with localizable fields. Basically use localized_field() instead of field() and that's it.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%