Skip to content

Commit

Permalink
Merge branch 'master' into update-workflows
Browse files Browse the repository at this point in the history
* master:
  Revert "Bump to 1.14.2"
  Raise when translated entry contains interpolations for reserved keywords and no substitutions provided
  Update `mocha` gem
  condense to TOKENIZER
  Speed up INTERPOLATION_SYNTAX
  Improve TOKENIZER by 23%
  • Loading branch information
radar committed Mar 5, 2024
2 parents 442d3c0 + 8cc95fc commit b736751
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'

gemspec

gem 'mocha', '~> 1.7.0'
gem 'mocha', '~> 2.1.0'
gem 'test_declarative', '0.0.6'
gem 'rake', '~> 13'
gem 'minitest', '~> 5.14'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails-6.0.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
gemspec :path => '..'

gem 'activesupport', '~> 6.0.0'
gem 'mocha', '~> 2'
gem 'mocha', '~> 2.1.0'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.14'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails-6.1.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
gemspec :path => '..'

gem 'activesupport', '~> 6.1'
gem 'mocha', '~> 2'
gem 'mocha', '~> 2.1.0'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.14'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails-7.0.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
gemspec :path => '..'

gem 'activesupport', '~> 7.0'
gem 'mocha', '~> 2'
gem 'mocha', '~> 2.1.0'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.14'
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.rails-7.1.x
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ source 'https://rubygems.org'

gemspec :path => '..'

gem 'activesupport', '~> 7.1'
gem 'mocha', '~> 2'
gem 'activesupport', '~> 7.1.0'
gem 'mocha', '~> 2.1.0'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.14'
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.rails-main
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source 'https://rubygems.org'
gemspec :path => '..'

gem 'activesupport', github: 'rails/rails', branch: 'main'
gem 'mocha', '~> 1.7.0'
gem 'mocha', '~> 2.1.0'
gem 'test_declarative', '0.0.6'
gem 'rake'
gem 'minitest', '~> 5.1'
Expand Down
4 changes: 3 additions & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ def translate(locale, key, options = EMPTY_HASH)

deep_interpolation = options[:deep_interpolation]
values = Utils.except(options, *RESERVED_KEYS) unless options.empty?
if values
if values && !values.empty?
entry = if deep_interpolation
deep_interpolate(locale, entry, values)
else
interpolate(locale, entry, values)
end
elsif entry.is_a?(String) && entry =~ I18n.reserved_keys_pattern
raise ReservedInterpolationKey.new($1.to_sym, entry)
end
entry
end
Expand Down
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, pattern, key = matchdata.values_at(1, 2, 3)
escaped ? pattern : 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
4 changes: 4 additions & 0 deletions lib/i18n/tests/interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ module Interpolation
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{default}') }
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{separator}') }
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{scope}') }
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '%{scope}') }

I18n.backend.store_translations(:en, :interpolate => 'Hi %{scope}!')
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:interpolate) }
end

test "interpolation: deep interpolation for default string" do
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module I18n
VERSION = "1.14.2"
VERSION = "1.14.1"
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'minitest/autorun'
require 'bundler/setup'
require 'i18n'
require 'mocha/setup'
require 'mocha/minitest'
require 'test_declarative'

class I18n::TestCase < Minitest::Test
Expand Down

0 comments on commit b736751

Please sign in to comment.