Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Last active October 4, 2023 14:58
Show Gist options
  • Save jhjguxin/5779890 to your computer and use it in GitHub Desktop.
Save jhjguxin/5779890 to your computer and use it in GitHub Desktop.
Mongoid localized fields, and how use it
# encoding: UTF-8
class GxApp
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Sequence
store_in session: "gxservice"
field :name, type: String, default: "Guanxi.me"
field :version, type: String
field :release_title, type: String, localize: true
field :change_log, type: String, localize: true
field :platform_os, type: String
field :description, type: String, localize: true
## sequence number
field :gx_app_id, :type => String
sequence :gx_app_id
validates_presence_of :name, :version, :change_log, :platform_os
validates_uniqueness_of :version, :scope => :platform_os
scope :platform_apps, ->(platform_os) { where(platform_os: platform_os) }
def initialize(attrs = nil, options = nil)
super
self.release_title_translations = { "zh-CN"=>"关系网有新版本了", "en"=>"Welcome to new version"}
end
class << self
def lastest_app_info(platform_os = nil)
result = Utility.cache_value({cache_key: "#{self.name.underscore}_lastest_#{platform_os}_app_info", expires_in: 1.hours}) {
GxApp.platform_apps(platform_os).desc(:id).first.as_json
}
result || self.desc(:id).first.as_json
end
def lastest_app_version(platform_os = nil)
lastest_app_info(platform_os)["version"] || self.desc(:id).first.version
end
end
end

From 2.4.0 Mongoid now supports localized fields without the need of any external gem.

class Product
  include Mongoid::Document
  field :description, localize: true
end

By telling the field to localize, Mongoid will under the covers store the field as a hash of locale/value pairs, but normal access to it will behave like a string.

# Using a default locale of en.
product = Product.new
product.description = "Marvelous!"
I18n.locale = :de
product.description = "Fantastisch!"

product.attributes
#=> { "description" => { "en" => "Marvelous!", "de" => "Fantastisch!" }

You can get and set all the translations at once by using the corresponding _translations method.

product.description_translations
#=> { "description" => { "en" => "Marvelous!", "de" => "Fantastisch!" }
product.description_translations =
  { "description" => { "en" => "Marvelous!", "de" => "Wunderbar!" }

How to create multilocale form for localized fields

https://github.com/mongoid/mongoid/issues/1770

Instead of the normal field name, use the field translations name. Ie:

class Product
  include Mongoid::Document
  field :name, localize: true
end

# The getter for all translations:
user.name_translations

# The setter for all translations:
user.name_translations=

You just create an input that will submit to name_translations= in this case with a hash of locale/value pairs.

class Category
  include Mongoid::Document

  field :name, type: String, localize: true
end

<%= f.fields_for :name_translations do |fo| %>
  <%= fo.label :ru %>
  <%= fo.text_field :ru %>
  <%= fo.label :en %>
  <%= fo.text_field :en %>
<% end %>

when you use simple_form

<%= f.input :change_log do %>
  <%= f.fields_for :change_log_translations do |localized_field| %>
    <%= localized_field.input "zh-CN", as: :text, input_html: { value: @gxservice_gx_app.change_log_translations["zh-CN"], :rows =>"5", :cols =>"80" } %>
    <%= localized_field.input "en", as: :text, input_html: { value: @gxservice_gx_app.change_log_translations["en"], :rows =>"5", :cols =>"80" } %>
  <% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment