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 26
        def compile_if_an_interpolation(string)
          if interpolated_str?(string)
            string.instance_eval <<-RUBY_EVAL, __FILE__, __LINE__
              def i18n_interpolate(v = {})
                "#{compiled_interpolation_body(string)}"
              end
            RUBY_EVAL
          end

          string
        end
interpolated_str?(str) click to toggle source
# File lib/i18n/backend/interpolation_compiler.rb, line 38
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 59
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 48
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 72
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 92
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 88
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 54
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 68
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 63
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 80
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 76
def nil_key(key)
  "(v.has_key?(#{key}) && '')"
end
reserved_key(key) click to toggle source
# File lib/i18n/backend/interpolation_compiler.rb, line 84
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 44
def tokenize(str)
  str.split(TOKENIZER)
end