Skip to content

Commit

Permalink
Merge pull request #651 from fatkodima/optimizations
Browse files Browse the repository at this point in the history
Optimize `I18n.t`
  • Loading branch information
radar authored Feb 28, 2023
2 parents ac2592f + 048af5c commit f75520a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
9 changes: 4 additions & 5 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,11 @@ def with_locale(tmp_locale = nil)
# keys are Symbols.
def normalize_keys(locale, key, scope, separator = nil)
separator ||= I18n.default_separator
locale = locale.to_sym if locale

[
*normalize_key(locale, separator),
*normalize_key(scope, separator),
*normalize_key(key, separator)
]
result = [locale]
result.concat(normalize_key(scope, separator)) if scope
result.concat(normalize_key(key, separator))
end

# Returns true when the passed locale, which can be either a String or a
Expand Down
9 changes: 7 additions & 2 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def translate(locale, key, options = EMPTY_HASH)
end

deep_interpolation = options[:deep_interpolation]
values = Utils.except(options, *RESERVED_KEYS)
values = Utils.except(options, *RESERVED_KEYS) unless options.empty?
if values
entry = if deep_interpolation
deep_interpolate(locale, entry, values)
Expand Down Expand Up @@ -123,7 +123,12 @@ def subtrees?
# first translation that can be resolved. Otherwise it tries to resolve
# the translation directly.
def default(locale, object, subject, options = EMPTY_HASH)
options = options.reject { |key, value| key == :default }
if options.size == 1 && options.has_key?(:default)
options = {}
else
options = Utils.except(options, :default)
end

case subject
when Array
subject.each do |item|
Expand Down
10 changes: 9 additions & 1 deletion lib/i18n/interpolate/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# heavily based on Masao Mutoh's gettext String interpolation extension
# http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb

Expand All @@ -10,6 +12,11 @@ module I18n
INTERPOLATION_PATTERN = Regexp.union(DEFAULT_INTERPOLATION_PATTERNS)
deprecate_constant :INTERPOLATION_PATTERN

INTERPOLATION_PATTERNS_CACHE = Hash.new do |hash, patterns|
hash[patterns] = Regexp.union(patterns)
end
private_constant :INTERPOLATION_PATTERNS_CACHE

class << self
# Return String or raises MissingInterpolationArgument exception.
# Missing argument's logic is handled by I18n.config.missing_interpolation_argument_handler.
Expand All @@ -20,7 +27,8 @@ def interpolate(string, values)
end

def interpolate_hash(string, values)
string.gsub(Regexp.union(config.interpolation_patterns)) do |match|
pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns]
string.gsub(pattern) do |match|
if match == '%%'
'%'
else
Expand Down

0 comments on commit f75520a

Please sign in to comment.