DevTeam Blog

Localized fields in Mongoid

30 Oct 2014

By Sebastian Machuca

Sometimes we need store localized data in our MongoDB database, maybe one way to do that is create a ‘Translations’ table, where we store the localized fields. Another way is create multiple fields like “name_en”, “name_fr”…

But Mongoid has a ‘better & simple’ way to do that…

###”Localized Fields” by Mongoid from v2.4.0

  1. Add ‘localize’ flag to the field
Class Post
      include Mongoid::Document
      field :name, localize: true
    end
  1. Set the locale before assign the field value
post = Post.new
    I18n.default_locale = :en
    post.name = "Little Duck"
    I18n.default_locale = :es
    post.name = "Patito"
    post.save
  1. The result
post.name_translations
    => { "en" => "Little Duck", "es" => "Patito" }
</pre>

4. Create custom get/set method:
%w( en es fr ).each do |lang|
      define\_method("name\_#{lang}") do
        I18n.locale = lang.to\_sym
        name = self.name
        I18n.locale = :en 
        name
      end
      define\_method("name\_#{lang}=") do |argument|
        I18n.locale = lang.to\_sym
        self.name= argument
        name = self.name
        I18n.locale = :en 
        name
      end
    end
</pre>

###Some 'dirty' things
- You **must** change the application locale every time.
- To search across the languages, you have to create a index in the model.

### [Official Mongoid Doc](http://mongoid.org/en/mongoid/v3/documents.html#localized_fields)
comments powered by Disqus