Class: GettextI18nRails::Backend

### Home Classes Methods

### In Files

- lib/gettext_i18n_rails/backend.rb

### Parent

Object

### Methods

- ::new
- #available_locales
- #discard_pass_through_key
- #flatten_key
- #gettext_key
- #interpolate
- #method_missing
- #plural_translate
- #translate

### Class/Module Index

Quicksearch

- GettextI18nRails
- GettextI18nRails::Backend
- GettextI18nRails::BaseParser
- GettextI18nRails::GettextHooks
- GettextI18nRails::HamlParser
- GettextI18nRails::HtmlSafeTranslations
- GettextI18nRails::ModelAttributesFinder
- GettextI18nRails::Railtie
- GettextI18nRails::SlimParser
- RubyGettextExtractor
- RubyGettextExtractor::Extractor18
- RubyGettextExtractor::Extractor19
- RubyGettextExtractor::ExtractorMethods
- ActionController
- ActionController::Base
- ActiveModel
- ActiveModel::Translation
- ActiveRecord
- ActiveRecord::Base
- I18n
- I18n::Config
- Object
- String

No matching classes.

# GettextI18nRails::Backend

translates i18n calls to gettext calls

### Constants

RUBY19

### Attributes

backend[RW]

### Public Class Methods

new(*args) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 10
    def initialize(*args)
      self.backend = I18n::Backend::Simple.new(*args)
    end

### Public Instance Methods

available_locales() click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 14
    def available_locales
      FastGettext.available_locales || []
    end

method_missing(method, *args) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 29
    def method_missing(method, *args)
      backend.send(method, *args)
    end

translate(locale, key, options) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 18
    def translate(locale, key, options)
      if gettext_key = gettext_key(key, options)
        translation =
          plural_translate(gettext_key, options) || FastGettext._(gettext_key)
        interpolate(translation, options)
      else
        result = backend.translate(locale, key, options)
        (RUBY19 and result.is_a?(String)) ? result.force_encoding("UTF-8") : result
      end
    end

### Protected Instance Methods

discard_pass_through_key(key, translation) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 59
    def discard_pass_through_key(key, translation)
      if translation == key
        nil
      else
        translation
      end
    end

flatten_key(key, options) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 82
    def flatten_key key, options
      scope = [*(options[:scope] || [])]
      scope.empty? ? key.to_s : "#{scope*'.'}.#{key}"
    end

gettext_key(key, options) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 35
    def gettext_key(key, options)
      flat_key = flatten_key key, options
      if FastGettext.key_exist?(flat_key)
        flat_key
      elsif self.class.translate_defaults
        [*options[:default]].each do |default|
          #try the scoped(more specific) key first e.g. 'activerecord.errors.my custom message'
          flat_key = flatten_key default, options
          return flat_key if FastGettext.key_exist?(flat_key)

          #try the short key thereafter e.g. 'my custom message'
          return default if FastGettext.key_exist?(default)
        end
        return nil
      end
    end

interpolate(string, values) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 67
    def interpolate(string, values)
      if string.respond_to?(:%)
        reserved_keys = if defined?(I18n::RESERVED_KEYS) # rails 3+
          I18n::RESERVED_KEYS
        else
          I18n::Backend::Base::RESERVED_KEYS
        end

        options = values.except(*reserved_keys)
        options.any? ? (string % options) : string
      else
        string
      end
    end

plural_translate(gettext_key, options) click to toggle source

    # File lib/gettext_i18n_rails/backend.rb, line 52
    def plural_translate(gettext_key, options)
      if options[:count]
        translation = FastGettext.n_(gettext_key, options[:count])
        discard_pass_through_key gettext_key, translation
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.