Module: I18n::Backend::InterpolationCompiler::Compiler

### Home Classes Methods

### In Files

- lib/i18n/backend/interpolation_compiler.rb

### Methods

- #compile_if_an_interpolation
- #compile_interpolation_token
- #compiled_interpolation_body
- #direct_key
- #escape_key_sym
- #escape_plain_str
- #handle_interpolation_token
- #interpolate_key
- #interpolate_or_raise_missing
- #interpolated_str?
- #missing_key
- #nil_key
- #reserved_key
- #tokenize

### Class/Module Index

Quicksearch

- I18n
- I18n::ArgumentError
- I18n::Backend
- I18n::Backend::Base
- I18n::Backend::Cache
- I18n::Backend::Cascade
- I18n::Backend::Chain
- I18n::Backend::Chain::Implementation
- I18n::Backend::Fallbacks
- I18n::Backend::Flatten
- I18n::Backend::Gettext
- I18n::Backend::Gettext::PoData
- I18n::Backend::InterpolationCompiler
- I18n::Backend::InterpolationCompiler::Compiler
- I18n::Backend::KeyValue
- I18n::Backend::KeyValue::Implementation
- I18n::Backend::Memoize
- I18n::Backend::Metadata
- I18n::Backend::Pluralization
- I18n::Backend::Simple
- I18n::Backend::Simple::Implementation
- I18n::Config
- I18n::ExceptionHandler
- I18n::Gettext
- I18n::Gettext::Helpers
- I18n::InvalidLocale
- I18n::InvalidLocaleData
- I18n::InvalidPluralizationData
- I18n::Locale
- I18n::Locale::Fallbacks
- I18n::Locale::Tag
- I18n::Locale::Tag::Parents
- I18n::Locale::Tag::Rfc4646
- I18n::Locale::Tag::Rfc4646::Parser
- I18n::Locale::Tag::Simple
- I18n::MissingInterpolationArgument
- I18n::MissingTranslation
- I18n::MissingTranslation::Base
- I18n::MissingTranslationData
- I18n::ReservedInterpolationKey
- I18n::Tests
- I18n::Tests::Basics
- I18n::Tests::Defaults
- I18n::Tests::Link
- I18n::Tests::Localization
- I18n::Tests::Lookup
- I18n::Tests::Pluralization
- I18n::Tests::Procs
- I18n::UnknownFileType
- GetText
- GetText::PoParser
- Hash
- Kernel

No matching classes.

# I18n::Backend::InterpolationCompiler::Compiler

### Constants

INTERPOLATION_SYNTAX_PATTERN
TOKENIZER

### Public Instance Methods

compile_if_an_interpolation(string) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 25
    def compile_if_an_interpolation(string)
      if interpolated_str?(string)
        string.instance_eval               def i18n_interpolate(v = {})                "#{compiled_interpolation_body(string)}"              end, __FILE__, __LINE__
      end

      string
    end

interpolated_str?(str) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 37
    def interpolated_str?(str)
      str.kind_of?(::String) && str =~ INTERPOLATION_SYNTAX_PATTERN
    end

### Protected Instance Methods

compile_interpolation_token(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 58
    def compile_interpolation_token(key)
      "\#{#{interpolate_or_raise_missing(key)}}"
    end

compiled_interpolation_body(str) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 47
    def compiled_interpolation_body(str)
      tokenize(str).map do |token|
        (matchdata = token.match(INTERPOLATION_SYNTAX_PATTERN)) ? handle_interpolation_token(token, matchdata) : escape_plain_str(token)
      end.join
    end

direct_key(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 71
    def direct_key(key)
      "((t = v[#{key}]) && t.respond_to?(:call) ? t.call : t)"
    end

escape_key_sym(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 91
    def escape_key_sym(key)
      # rely on Ruby to do all the hard work :)
      key.to_sym.inspect
    end

escape_plain_str(str) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 87
    def escape_plain_str(str)
      str.gsub(/"|\\|#/) {|x| "\\#{x}"}
    end

handle_interpolation_token(interpolation, matchdata) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 53
    def handle_interpolation_token(interpolation, matchdata)
      escaped, pattern, key = matchdata.values_at(1, 2, 3)
      escaped ? pattern : compile_interpolation_token(key.to_sym)
    end

interpolate_key(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 67
    def interpolate_key(key)
      [direct_key(key), nil_key(key), missing_key(key)].join('||')
    end

interpolate_or_raise_missing(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 62
    def interpolate_or_raise_missing(key)
      escaped_key = escape_key_sym(key)
      RESERVED_KEYS.include?(key) ? reserved_key(escaped_key) : interpolate_key(escaped_key)
    end

missing_key(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 79
    def missing_key(key)
      "I18n.config.missing_interpolation_argument_handler.call(#{key}, v, self)"
    end

nil_key(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 75
    def nil_key(key)
      "(v.has_key?(#{key}) && '')"
    end

reserved_key(key) click to toggle source

    # File lib/i18n/backend/interpolation_compiler.rb, line 83
    def reserved_key(key)
      "raise(ReservedInterpolationKey.new(#{key}, self))"
    end

tokenize(str) click to toggle source

tokenize("foo %{bar} baz %%{buz}") # => ["foo ", "%{bar}", " baz ", "%%{buz}"]

    # File lib/i18n/backend/interpolation_compiler.rb, line 43
    def tokenize(str)
      str.split(TOKENIZER)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.