class EventNewsCustomTitle include Mongoid::Document include Mongoid::Timestamps field :key field :title, type: String, localize: true KEYS = ['event_date','speaker','place','host'] def self.get_map KEYS.map{|k| self.where(key: k).first || self.create(key: k,title_translations: I18n.available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("event_news.#{k}")}]}.to_h)} end def default_title I18n.t("event_news.#{self.key}") end def self.get_trans(key) TitleMap[key][I18n.locale] rescue I18n.t("event_news.#{key}") end TitleMap = self.get_map.map{|v| [v.key,v.title_translations]}.to_h end