Custom data types for localization

40 views
Skip to first unread message

Matt Ash

unread,
Apr 5, 2011, 7:07:55 AM4/5/11
to MongoMapper
I have a site that requires certain fields to be localized. I realize
that the best way to go about this is by creating a custom data type.
After searching around, the approach that is used [here](https://
gist.github.com/828114) seemed to be the best. Unfortunately as [this
StackOverflow page](http://stackoverflow.com/questions/5030862/mongo-
mapper-custom-data-types-for-localization) points out, it doesn't work
when applied to more than one field in a single class (or embedded
document in my case). An answer was submitted on the above
StackOverflow page, but it doesn't seem to work.

Unfortunately, I don't have much experience with creating custom data
types. Does anyone have any ideas how to make this work?

Brandon Keepers

unread,
Apr 5, 2011, 1:21:52 PM4/5/11
to mongo...@googlegroups.com
The StackOverflow answer looks like it should work, in what way is it not working?

=b
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en

Matt Ash

unread,
Apr 6, 2011, 5:56:41 AM4/6/11
to MongoMapper
Brandon, thanks for your response. You can see all the relevant files
and the console log on [this gist](https://gist.github.com/905413).

Basically, the error I get in the console is the following:

>> f = Foo.new({:title => "Title"})
NoMethodError: undefined method `translations' for "Title":String
from /Volumes/MEDIA/projects/oratsouyts/lib/LocalizedString.rb:19:in
`to_mongo'

Brandon Keepers

unread,
Apr 6, 2011, 12:59:13 PM4/6/11
to mongo...@googlegroups.com
It looks like you need the same logic in #to_mongo has you have in #from_mongo, where it does different things based on whether it's a hash or string.  Not sure why value would ever be just a string, but that appears to be the case.

=b

AdamN

unread,
Apr 6, 2011, 11:45:24 AM4/6/11
to MongoMapper
I'm getting the same result -- very interested in figuring this out,
but I'm not making much progress myself.

AdamN

unread,
Apr 6, 2011, 3:58:38 PM4/6/11
to MongoMapper
Please see http://stackoverflow.com/questions/5030862/mongo-mapper-custom-data-types-for-localization/5572226#5572226

I found another post that had a different approach of extending Hash,
and made some changes so it worked more like a string instead of
having to be explicit with the locale all the time. Hope this helps
-- seems to work for my purpose (so far... could be issues! :) )

Emerson Lackey

unread,
Apr 7, 2011, 11:31:37 PM4/7/11
to MongoMapper
The idea of using MongoMapper and MongoDB to handle projects with
multilingual requirements really excites me.

On Apr 6, 3:58 pm, AdamN <adam.nicker...@lxi.ca> wrote:
> Please seehttp://stackoverflow.com/questions/5030862/mongo-mapper-custom-data-t...

Matt Ash

unread,
Apr 8, 2011, 8:05:42 AM4/8/11
to MongoMapper
AdamN, thanks so much for your solution. I've applied it to my app,
and so far everything seems to be working fine.

I just want to share two points that I implemented when using your
solution for the sake of anyone else dealing with this problem.
Firstly, my situation was such that I converting information in a pre-
existing collection to a multi-lingual field. I didn't want to alter
field names, and not every entry had multi-lingual data. In order to
account for this I modified your from_mongo as follows:

def self.from_mongo(value)
value.is_a?(String) ? value : LocalizedString.new(value || {})
end

This way, if I was querying an entry that had legacy data in String
type, I would still be able to access it.

Also, I built my app such that my public API would serve to_json dumps
of my mongomapper objects. Adopting this solution outputted my
LocalizedString field as a hash in my JSON, which wasn't desirable. To
circumvent this, I used the as_json method in my model code. For
example:

def as_json(options={})
{ :title => self.title.to_s }
end

This made sure that my JSON was clean.

On more point I'd like to point out for others that might not realize
it is that its still possible to pass the value of the LocalizedString
using "=" or update_attributes!, but the value has to be passed as a
Hash, and that Hash has to have all of the multilingual values in it,
like:

Foo.update_attributes!(:title => {"en" => "English", "de" =>
"German"})

Thanks again, AdamN! I can finally move this project forward.

On Apr 7, 12:58 am, AdamN <adam.nicker...@lxi.ca> wrote:
> Please seehttp://stackoverflow.com/questions/5030862/mongo-mapper-custom-data-t...
>

TomasC

unread,
Apr 8, 2011, 10:11:38 AM4/8/11
to mongo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages