module I18n::Backend::InterpolationCompiler::Compiler - i18n-0.6.11 Documentation

# module 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