Skip to content

Commit

Permalink
Wrapping up t-prefix fix (#89)
Browse files Browse the repository at this point in the history
* Fix t prefixes not being assigned properly

This fixes a change I made to our t-prefixes that broke things: overriding `ActionView::Base#capture` meant that we'd fire on helper calls too — and extract from their block location.

Note: we're leveraging Rails 6.1+'s `@current_template.virtual_path` to avoid needing to muck with block source_locations or view_paths,
Rails has already figured that out for us.

* Freeze two "." string literals

`t` might be called often on a page and in a nested loop these needless String allocations could add up.
Since they're so easy to fix, let's do it.
  • Loading branch information
kaspth authored Aug 31, 2023
1 parent 300136d commit cd6e80e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
5 changes: 0 additions & 5 deletions lib/nice_partials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
require_relative "nice_partials/version"

module NicePartials
def self.locale_prefix_from(lookup_context, block)
partial_location = block.source_location.first.dup
lookup_context.view_paths.each { partial_location.delete_prefix!(_1.path)&.delete_prefix!("/") }
partial_location.split('.').first.gsub('/_', '/').gsub('/', '.')
end
end

ActiveSupport.on_load :action_view do
Expand Down
43 changes: 28 additions & 15 deletions lib/nice_partials/monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# frozen_string_literal: true

# Monkey patch required to make `t` work as expected. Is this evil?
# TODO Do we need to monkey patch other types of renderers as well?
module NicePartials::RenderingWithLocalePrefix
ActionView::Base.prepend self
module BaseIntegration
::ActionView::Base.prepend self

def capture(*, &block)
with_nice_partials_t_prefix(lookup_context, block) { super }
end
def t(key, options = {})
if (template = @_nice_partials_translate_template) && key&.start_with?(".")
key = "#{virtual_path_translate_key_prefix(template.virtual_path)}#{key}"
end

super(key, **options)
end

def t(key, options = {})
if (prefix = @_nice_partials_t_prefix) && key&.start_with?(".")
key = "#{prefix}#{key}"
def with_nice_partials_t_prefix(block)
old_nice_partials_translate_template = @_nice_partials_translate_template
@_nice_partials_translate_template = block ? @current_template : nil
yield
ensure
@_nice_partials_translate_template = old_nice_partials_translate_template
end

super(key, **options)
private

def virtual_path_translate_key_prefix(virtual_path)
@_scope_key_by_partial_cache ||= {} # Reuses Rails' existing `t` cache.
@_scope_key_by_partial_cache[virtual_path] ||= virtual_path.gsub(%r{/_?}, ".")
end
end

private
module PartialRendererIntegration
ActionView::PartialRenderer.prepend self

def with_nice_partials_t_prefix(lookup_context, block)
_nice_partials_t_prefix = @_nice_partials_t_prefix
@_nice_partials_t_prefix = block ? NicePartials.locale_prefix_from(lookup_context, block) : nil
yield
ensure
@_nice_partials_t_prefix = _nice_partials_t_prefix
def render(partial, view, block)
view.with_nice_partials_t_prefix(block) { super }
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/translations/_nice_partials_translated.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render("basic") do |partial| %>
<%= partial.message.t ".message" %>
<%= render "basic" do %>
<% _1.message.t ".message" %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= render "basic" do %>
<% _1.message render("translations/translated") %>
<% _1.message do %>
<%= render "translations/translated" %>
<% end %>
<% end %>
6 changes: 6 additions & 0 deletions test/renderer/translation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class Renderer::TranslationTest < NicePartials::Test
assert_text "nice_partials"
end

test "translations nested" do
render "translations/nice_partials_translated_nested"

assert_text "message\n message\n"
end

test "translations key lookup handles special characters" do
render "translations/special_nice_partials_translated"

Expand Down

0 comments on commit cd6e80e

Please sign in to comment.