Created
April 21, 2011 06:14
-
-
Save mattash/933834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Example | |
include MongoMapper::Document | |
key :title, LocalizedString | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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