Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Mongoid add-on to deal with localizable fields
branch: master

This branch is even with rahimsiz:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
config/locales
lib/mongoid
spec
.document
.gitignore
Gemfile
Gemfile.lock
LICENSE
README.rdoc
Rakefile
VERSION
mongoid_i18n.gemspec

README.rdoc

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'

Second, add available locales to your application.rb

config.i18n.available_locales = [:en, :es]

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.

The localized_field helper has two options (set to false as default): – credits to @nicolaracco & @ogeidix localized_field :title, :clear_empty_values => true # This will remove all blank values when you do a mass assignment localized_field :title, :use_default_if_empty => true # This will return the value assigned to the default locale, if there isn't a value for the current locale

localized_field :title, :clear_empty_values => true, :use_default_if_empty => true # When you do mass assignment, empty values are removed. If you try to get a 'removed' value, it will return the default value

With the last case: @entry.title_translations = { 'en' => 'New title', 'es' => '' } # It will store only the 'en' value @entry.title_translations[:es] # => New title

There are validation helper to manage locale fields: – credits to @nicolaracco & @ogeidix validates_default_locale :title # The field has to contain at least the default translation validates_one_locale :title # The field has to contain at least one translation validates_all_locales :title # The field has to contain a translation for each available locale

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

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

Something went wrong with that request. Please try again.