Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found <a href="http://groups.google.com/group/mongomapper/browse_thread/thread/70f0c47549c7defc" rel="nofollow">this post</a>: which was very helpful. He extended HashWithIndifferentAccess to work as a LocalizedString. The only thing I didn't like about it was having to explicly specify the locale when setting it each time -- I wanted it to work more like a string. of course, you can't overload the = operator (at least I don't think you can) so I used &lt;&lt;, and added a to_s method that would output the string of the current locale....</p> <pre><code>class LocalizedString &lt; HashWithIndifferentAccess def self.from_mongo(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 &lt;&lt; (value) self[I18n.locale] = value end </code></pre> <p>end</p> <p>and then I have a class like:</p> <pre><code>class SimpleModel include MongoMapper::Document key :test, LocalizedString end </code></pre> <p>and can do things like</p> <pre><code> I18n.locale = :en a = SimpleModel.new a.test &lt;&lt; "English" I18n.locale = :de a.test &lt;&lt; "German" puts a.test # access the translation for the current locale I18n.locale = :en puts a.test # access the translation for the current locale puts a.test[:de] # access a translation explicitly puts a.test[:en] puts a.test.inspect </code></pre> <p>and get </p> <pre><code>German English German English {"en"=&gt;"English", "de"=&gt;"German"} </code></pre> <p>so there we go -- this one actually seems to work for me. Comments welcome, and hope this helps someone!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload