Fr. Mesrop Ash

Thoughts and miscellany from life in ministry

Localizing Data Models in MongoMapper

published

One of my longterm goals for The Oratsouyts Online was to have the content available in at least both English and Armenian. I had developed the site using insanely cool NoSQL technology called Mongo because of its speed and flexibility. The flexibilty was key because I hadn’t worked out the whole data model when I started the project, and I knew it would grow and change in the future.

I had built the site using the Ruby micro-framework Sinatra at first, but later as the project grew I ported it to Padrino. At that time I moved from the raw Mongo Ruby Driver to the sleek looking MongoMapper. It had certainly made the already easy Mongo syntax significantly easier.

When I finally got around to localizing the site I found that localizing the UI was a breeze with Padrino’s baked in i18n support, but when I started on the data, MongoMapper was presenting a problem. With the ridiculous flexibility that Mongo offers I assumed it would be a piece of cake. Hours and days of searching for solutions and then trying to hack my own proved fruitless. Eventually, I posted on the MongoMapper group, in the hope that someone else had done or could do what I couldn’t. Fortunately within a couple days a solution was found. I wanted to post it here simple to provide another vector by which someone else may find this solution lest their googling be fruitless as mine.

The Solution

I had started with a solution I found on Stackoverflow, but it really didn’t work. Eventually with the help other group members, we ended up with this solution. Its a custom data type that should be applied to the keys that will be localized.

Here is the code.

Here is an example of what the model would look like.

Here is an example of how to interact with the model in the console.

An important note, to add data to the localized field the << operator must be used. Adding data using = will result in the localization being nullified.

For more info, I encourage you to read Stackoverlow solution, as well as my comment on the MongoMapper group.

A second solution

After I implemented the above solution, another user posted his solution which acts as a MongoMapper Plugin and not a custom data type. It actually looks more effective, but I haven’t tested it yet. It can be found in this GitHub Gist

I hope these solutions will be helpful to anyone who finds themselves in the same position I was.

Notes

  1. 1985ok reblogged this from mattash
  2. mattash posted this

Comments

blog comments powered by Disqus