Skip to content

Instantly share code, notes, and snippets.

@mattash
Created April 21, 2011 06:14
Show Gist options
  • Save mattash/933834 to your computer and use it in GitHub Desktop.
Save mattash/933834 to your computer and use it in GitHub Desktop.
class Example
include MongoMapper::Document
key :title, LocalizedString
end
I18n.locale = :en
e = Example.new
e.title << "English Title"
I18n.locale = :hy
e.title << "Armenian Title" # data added after changing the locale is saved for that locale
puts e.title # returns data for the current locale (i.e. "Armenian Title")
I18n.locale = :en
puts e.title # returns "English Title"
puts e.title[:hy] # translations can also be explicitly called
class LocalizedString < HashWithIndifferentAccess
def self.from_mongo(value)
value.is_a?(String) ? value : LocalizedString.new(value || {})
end
def available_locales
symbolize_keys.keys
end
def to_s
self[I18n.locale]
end
def in_current_locale=(value)
self[I18n.locale] = value
end
def << (value)
self[I18n.locale] = value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment