Skip to content

Commit

Permalink
Memoize the result of gsubbing @virtual_path
Browse files Browse the repository at this point in the history
This gets called many times for each virtual_path, creating a new string
each time that `translate` is called. We can memoize this so that it
only happens once per virtual_path instead.
  • Loading branch information
dillonwelch committed Mar 20, 2018
1 parent 9d9f752 commit 05eaa07
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions actionview/lib/action_view/helpers/translation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ def localize(*args)

private
def scope_key_by_partial(key)
if key.to_s.first == "."
stringified_key = key.to_s
if stringified_key.first == "."
if @virtual_path
@virtual_path.gsub(%r{/_?}, ".") + key.to_s
@_scope_key_by_partial_cache ||= {}
@_scope_key_by_partial_cache[@virtual_path] ||= @virtual_path.gsub(%r{/_?}, ".")
"#{@_scope_key_by_partial_cache[@virtual_path]}#{stringified_key}"
else
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
end
Expand Down

0 comments on commit 05eaa07

Please sign in to comment.