Sha256: e8c19928ca035540d48889e2761b37325017074c1c0c2a03a6fffa14ad8a5173

Contents?: true

Size: 1.31 KB

Versions: 13

Compression:

Stored size: 1.31 KB

Contents

module GettextI18nRails
  #translates i18n calls to gettext calls
  class Backend
    @@translate_defaults = true
    cattr_accessor :translate_defaults
    attr_accessor :backend

    def initialize(*args)
      self.backend = I18n::Backend::Simple.new(*args)
    end

    def available_locales
      FastGettext.available_locales || []
    end

    def translate(locale, key, options)
      flat_key = flatten_key key, options
      if FastGettext.key_exist?(flat_key)
        raise "no yet build..." if options[:locale]
        _(flat_key)
      else
        if self.class.translate_defaults
          options[:default].to_a.each do |default|
            #try the more specific key first e.g. 'activerecord.errors.my custom message'
            flat_key = flatten_key default, options
            return FastGettext._(flat_key) if FastGettext.key_exist?(flat_key)

            #try the short key thereafter e.g. 'my custom message'
            return FastGettext._(default) if FastGettext.key_exist?(default)
          end
        end
        backend.translate locale, key, options
      end
    end

    def method_missing(method, *args)
      backend.send(method, *args)
    end

    protected

    def flatten_key key, options
      scope = [*(options[:scope] || [])]
      scope.empty? ? key.to_s : "#{scope*'.'}.#{key}"
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
zena-0.16.9 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.8 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.7 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.6 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.5 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.4 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.3 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.2 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.1 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.16.0 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.15.2 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.15.1 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb
zena-0.15.0 vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails/backend.rb