module Translatable extend ActiveSupport::Concern included do def self.translate_field(field) define_method(field) do self.public_send "#{field}_#{I18n.locale}" end define_method("#{field}_dashboard") do I18n.locale == :zh ? to_dashboard(to_s_zh(field), to_s_en(field)) : to_dashboard(to_s_en(field), to_s_zh(field)) end define_method("#{field}_trans") do I18n.locale == :en ? self.public_send("#{field}_zh").to_s : self.public_send("#{field}_en").to_s end define_method("print_#{field}_trans") do I18n.locale == :en ? '中文: ' + self.public_send("#{field}_zh").to_s : 'English: ' + self.public_send("#{field}_en").to_s end end def self.globalize(*fields) fields.each do |field| translates field globalize_accessors :attributes => [field] translate_field field end end end def to_s_en(field) self.public_send("#{field}_en").to_s end def to_s_zh(field) self.public_send("#{field}_zh").to_s end def to_dashboard(main, trans) '' + main + '
' + trans + '' end end