Skip to content

Commit d2d0a25

Browse files
authored
Merge pull request rails#40981 from mkasztelnik/40900-fix-lazy-translation-in-partial-with-block
Fix lazy translation in partial with block
2 parents 35ecdf0 + 8e53f91 commit d2d0a25

File tree

10 files changed

+28
-9
lines changed

10 files changed

+28
-9
lines changed

actionview/lib/action_view/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ def initialize(lookup_context, assigns, controller) #:nodoc:
241241
end
242242

243243
def _run(method, template, locals, buffer, add_to_stack: true, &block)
244-
_old_output_buffer, _old_template = @output_buffer, @current_template
244+
_old_output_buffer, _old_virtual_path, _old_template = @output_buffer, @virtual_path, @current_template
245245
@current_template = template if add_to_stack
246246
@output_buffer = buffer
247247
public_send(method, locals, buffer, &block)
248248
ensure
249-
@output_buffer, @current_template = _old_output_buffer, _old_template
249+
@output_buffer, @virtual_path, @current_template = _old_output_buffer, _old_virtual_path, _old_template
250250
end
251251

252252
def compiled_method_container

actionview/lib/action_view/context.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Context
1818
def _prepare_context
1919
@view_flow = OutputFlow.new
2020
@output_buffer = nil
21+
@virtual_path = nil
2122
end
2223

2324
# Encapsulates the interaction with the view flow so it

actionview/lib/action_view/helpers/translation_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ def self.i18n_option?(name)
131131

132132
def scope_key_by_partial(key)
133133
if key&.start_with?(".")
134-
if @current_template&.virtual_path
134+
if @virtual_path
135135
@_scope_key_by_partial_cache ||= {}
136-
@_scope_key_by_partial_cache[@current_template.virtual_path] ||= @current_template.virtual_path.gsub(%r{/_?}, ".")
137-
"#{@_scope_key_by_partial_cache[@current_template.virtual_path]}#{key}"
136+
@_scope_key_by_partial_cache[@virtual_path] ||= @virtual_path.gsub(%r{/_?}, ".")
137+
"#{@_scope_key_by_partial_cache[@virtual_path]}#{key}"
138138
else
139139
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
140140
end

actionview/test/abstract_unit.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def view
5959
end
6060

6161
def render_erb(string)
62+
@virtual_path = nil
63+
6264
template = ActionView::Template.new(
6365
string.strip,
6466
"test template",

actionview/test/activerecord/relation_cache_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setup
1010
view_paths = ActionController::Base.view_paths
1111
lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
1212
@view_renderer = ActionView::Renderer.new(lookup_context)
13+
@virtual_path = "path"
1314
@current_template = lookup_context.find "test/hello_world"
1415

1516
controller.cache_store = ActiveSupport::Cache::MemoryStore.new
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<%= t('.foo') %>
2+
<%= yield %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render("translations/templates/partial") %>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<%= render("translations/templates/partial") do %>
2+
<% end %>

actionview/test/template/template_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Context < ActionView::Base
2222
def initialize(*)
2323
super
2424
@output_buffer = "original"
25+
@virtual_path = nil
2526
end
2627

2728
def hello
@@ -34,7 +35,7 @@ def apostrophe
3435

3536
def partial
3637
ActionView::Template.new(
37-
"<%= @current_template.virtual_path %>",
38+
"<%= @virtual_path %>",
3839
"partial",
3940
ERBHandler,
4041
virtual_path: "partial",
@@ -114,9 +115,9 @@ def test_restores_buffer
114115
end
115116

116117
def test_virtual_path
117-
@template = new_template("<%= @current_template.virtual_path %>" \
118+
@template = new_template("<%= @virtual_path %>" \
118119
"<%= partial.render(self, {}) %>" \
119-
"<%= @current_template.virtual_path %>")
120+
"<%= @virtual_path %>")
120121
assert_equal "hellopartialhello", render
121122
end
122123

actionview/test/template/translation_helper_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class TranslationHelperTest < ActiveSupport::TestCase
2323
found_yield_single_argument: { foo: "Foo" },
2424
found_yield_block: { foo: "Foo" },
2525
array: { foo: { bar: "Foo Bar" } },
26-
default: { foo: "Foo" }
26+
default: { foo: "Foo" },
27+
partial: { foo: "Partial foo" }
2728
},
2829
foo: "Foo",
2930
hello: "<a>Hello World</a>",
@@ -176,6 +177,14 @@ def test_finds_translation_scoped_by_partial_yielding_single_argument_block
176177
assert_equal "Foo", view.render(template: "translations/templates/found_yield_single_argument").strip
177178
end
178179

180+
def test_finds_lazy_translation_scoped_by_partial
181+
assert_equal "Partial foo", view.render(template: "translations/templates/partial_lazy_translation").strip
182+
end
183+
184+
def test_finds_lazy_translation_scoped_by_partial_with_block
185+
assert_equal "Partial foo", view.render(template: "translations/templates/partial_lazy_translation_block").strip
186+
end
187+
179188
def test_finds_translation_scoped_by_partial_yielding_translation_and_key
180189
assert_equal "translations.templates.found_yield_block.foo: Foo", view.render(template: "translations/templates/found_yield_block").strip
181190
end

0 commit comments

Comments
 (0)