Sha256: 50b39e7fbcaf6e174737dda538406812447fd7652d823ae05af4bda1e75a43d8

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

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)
    '<span>' + main + '</span><br/><span class="font-italic font-grey">' + trans + '</span>'
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tmatt_cms-0.1.9 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.8 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.7 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.6 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.5 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.4 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.3 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.2 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.1 lib/generators/tmatt_cms/templates/concerns/translatable.rb
tmatt_cms-0.1.0 lib/generators/tmatt_cms/templates/concerns/translatable.rb