Skip to content

Commit

Permalink
condense to TOKENIZER
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Jun 13, 2023
1 parent 835a7d7 commit 8940781
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/i18n/backend/interpolation_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ module InterpolationCompiler
module Compiler
extend self

TOKENIZER = /(%%?\{[^}]+\})/
INTERPOLATION_SYNTAX_PATTERN = /(%%?)\{([^}]+)\}/
TOKENIZER = /(%%?\{[^}]+\})/

def compile_if_an_interpolation(string)
if interpolated_str?(string)
Expand All @@ -37,7 +36,7 @@ def i18n_interpolate(v = {})
end

def interpolated_str?(str)
str.kind_of?(::String) && str =~ INTERPOLATION_SYNTAX_PATTERN
str.kind_of?(::String) && str =~ TOKENIZER
end

protected
Expand All @@ -48,13 +47,12 @@ def tokenize(str)

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)
token.match(TOKENIZER) ? handle_interpolation_token(token) : escape_plain_str(token)
end.join
end

def handle_interpolation_token(interpolation, matchdata)
escaped, key = matchdata.values_at(1, 2)
escaped == '%%' ? "%{#{key}}" : compile_interpolation_token(key.to_sym)
def handle_interpolation_token(token)
token.start_with?('%%') ? token[1..] : compile_interpolation_token(token[2..-2])
end

def compile_interpolation_token(key)
Expand Down

0 comments on commit 8940781

Please sign in to comment.