-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
The question is in the title.
I'm trying to modify mongoid to force a locale for a certain document.
The goal is to have a localized document which doesn't necessarily rely on the current locale.
For example, this would allow to be on a backend with the english locale, but works on a document with a french locale.
This would work like that :
# I18n.locale is :en def create @article= Article.new(params[:article], locale: params[:article_locale]) @article.save # document is created as if I18n.locale == :fr end def update @article= Article.find(params[:id]) @article.force_locale(:fr) @article.update_attributes(params[:article]) # document is updated as if I18n.locale == :fr end
And I think that the most reliable way to handle that, is to get the Document locale from the Localized field :
# ~DocumentInstance~ would be the instance of the Document this field belongs to def mongoize(object) locale = ~DocumentInstance~.forced_locale || ::I18n.locale { locale.to_s => type.mongoize(object) } end def lookup(object) locale = ~DocumentInstance~.forced_locale || ::I18n.locale if ::I18n.respond_to?(:fallbacks) object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object[loc] }] else object[locale.to_s] end end
Thank you.