Sha256: ba6a5659bb592bcb3242d6369a2e8fc2a6f46cf16404b1e65b4ffeb42fe0070c

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

module Skyline::TranslationHelper

  # Extended translate function
  # ==== Parameters 
  # key<Symbol,String>:: the key (same as parameter to translate function)
  # options<Hash>:: options(same as parameter to translate function)
  #   this function supports a Class in the options[:scope], ie:
  #        tx(:ueber, :scope => [Skyline::Page, :flashes])
  #     will try to translate this for Skyline::Page and all its parent classes (all descendants from AR)
  #     first try:                  translate(:ueber, :scope => [:page, :flashes])
  #     if that fails, it'll try:   translate(:ueber, :scope => [:article, :flashes])
  #     (note that the Skyline module is stripped)
  #   if no Class is found, we'll fallback on the super function
  #
  # ==== Returns
  # <String>:: the translation
  def t(key, options = {})
    klass = options[:scope] && options[:scope].kind_of?(Array) && options[:scope].detect{|s| s.kind_of?(Class)}
    return super unless klass

    klass_index = options[:scope].index(klass)
    options[:raise] = true
    
    klass.self_and_descendants_from_active_record.map do |superklass|
      begin
        o = options.dup
        o[:scope][klass_index] = superklass.name.underscore.sub(/^skyline\//, "")
        return I18n.translate(key, o)
      rescue I18n::MissingTranslationData => e
      end
    end
    
    super    
  end
  
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
skylinecms-3.1.0 app/helpers/skyline/translation_helper.rb
westarete-skylinecms-3.0.8.20100329 app/helpers/skyline/translation_helper.rb
westarete-skylinecms-3.0.8.20100330 app/helpers/skyline/translation_helper.rb
skylinecms-3.0.8 app/helpers/skyline/translation_helper.rb
skylinecms-3.0.7 app/helpers/skyline/translation_helper.rb