Noosfero/noosfero

View on GitHub
plugins/orders/lib/i18n_auto_scope.rb

Summary

Maintainability
A
1 hr
Test Coverage
module I18nAutoScope
  extend ActiveSupport::Concern

  included do
    define_method :translate, I18n.method(:translate).to_proc unless self.respond_to? :translate

    alias_method :translate_without_auto_scope, :translate
    alias_method :translate, :translate_with_auto_scope
    alias_method :t, :translate
  end

  DefaultScope = "suppliers_plugin"

  # should be replaced on controller (e.g. controller)
  def i18n_scope
    DefaultScope
  end

  protected

    def translate_with_auto_scope(key, options = {})
      # raise option is removed from hash, so reinsert each time
      options[:raise] = true
      translation = self.translate_without_auto_scope key, options rescue nil

      unless translation
        Array(i18n_scope).each do |scope|
          options[:scope] = scope
          options[:raise] = true
          return translation if (translation = self.translate_without_auto_scope key, options rescue nil)
        end
      end

      translation
    end
end