I personally store the localized attributes as a hash (ex: name : {:en
=> 'english name', :es => 'spanish name'} ).
To help with the manipulation of these kind of attributes, I defined a
custom type for them:
class LocalizedString < HashWithIndifferentAccess
def self.from_mongo(value)
LocalizedString.new(value || {})
end
def available_locales
symbolize_keys.keys
end
def in_current_locale
self[I18n.locale]
end
def in_current_locale=(value)
self[I18n.locale] = value
end
end
I'm using the I18n class from Rails.
This way, on the MongoMapper::Document, I just have to declare each
translatable attribute to use this class as its type, like:
key :name, LocalizedString
The only thing I don't like of this method is that the attribute
doesn't get marked as dirty when I change one of the translations like
in:
object.name[:en] = "new english name"
object.name_changed? # returns false
A different approximation would be to save the localized attributes on
separated documents. Take a look at how it works the dm-is-localizable
gem for DataMapper, for example.
Carlos Paramio