Skip to content

Commit 132f86a

Browse files
authored
Merge pull request #2389 from ViewComponent/2386-translate-in-block
Use original virtual path when capturing blocks
2 parents 2061126 + 8f3a570 commit 132f86a

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Fix bug where relative paths in `translate` didn't work in blocks passed to ViewComponents.
14+
15+
*Joel Hawksley*
16+
1317
* Add SerpApi to "Who uses ViewComponent" list
1418

1519
*Andy from SerpApi*

lib/view_component/base.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def render_in(view_context, &block)
108108
self.class.__vc_compile(raise_errors: true)
109109

110110
@view_context = view_context
111-
old_virtual_path = view_context.instance_variable_get(:@virtual_path)
111+
@old_virtual_path = view_context.instance_variable_get(:@virtual_path)
112112
self.__vc_original_view_context ||= view_context
113113

114114
@output_buffer = view_context.output_buffer
@@ -166,7 +166,7 @@ def render_in(view_context, &block)
166166
""
167167
end
168168
ensure
169-
view_context.instance_variable_set(:@virtual_path, old_virtual_path)
169+
view_context.instance_variable_set(:@virtual_path, @old_virtual_path)
170170
@current_template = old_current_template
171171
end
172172

@@ -341,7 +341,9 @@ def content
341341

342342
@__vc_content =
343343
if __vc_render_in_block_provided?
344-
view_context.capture(self, &@__vc_render_in_block)
344+
with_original_virtual_path do
345+
view_context.capture(self, &@__vc_render_in_block)
346+
end
345347
elsif __vc_content_set_by_with_content_defined?
346348
@__vc_content_set_by_with_content
347349
end
@@ -358,6 +360,14 @@ def format
358360
self.class.__vc_response_format
359361
end
360362

363+
# @private
364+
def with_original_virtual_path
365+
@view_context.instance_variable_set(:@virtual_path, @old_virtual_path)
366+
yield
367+
ensure
368+
@view_context.instance_variable_set(:@virtual_path, virtual_path)
369+
end
370+
361371
private
362372

363373
attr_reader :view_context

lib/view_component/slot.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ def to_s
5858
if defined?(@__vc_content_block)
5959
# render_in is faster than `parent.render`
6060
@__vc_component_instance.render_in(view_context) do |*args|
61-
@__vc_content_block.call(*args)
61+
@parent.with_original_virtual_path do
62+
@__vc_content_block.call(*args)
63+
end
6264
end
6365
else
6466
@__vc_component_instance.render_in(view_context)
6567
end
6668
elsif defined?(@__vc_content)
6769
@__vc_content
6870
elsif defined?(@__vc_content_block)
69-
view_context.capture(&@__vc_content_block)
71+
@parent.with_original_virtual_path do
72+
view_context.capture(&@__vc_content_block)
73+
end
7074
elsif defined?(@__vc_content_set_by_with_content)
7175
@__vc_content_set_by_with_content
7276
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div id="outside"><%= t(".local_relative_key") %></div>
2+
3+
<div id="inside">
4+
<%= render(MyComponent.new) do %>
5+
<%= t(".local_relative_key") %>
6+
<% end %>
7+
</div>
8+
9+
<div id="slot">
10+
<%= render(SlotsComponent.new) do |component| %>
11+
<% component.with_title do %>
12+
<%= t(".local_relative_key") %>
13+
<% end %>
14+
<% component.with_item do %>
15+
<%= t(".local_relative_key") %>
16+
<% end %>
17+
<% component.with_footer do %>
18+
<%= t(".local_relative_key") %>
19+
<% end %>
20+
<% end %>
21+
</div>

test/sandbox/config/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ en:
1919
integration_examples:
2020
virtual_path_reset:
2121
message: "Hello world!"
22+
translations_in_block:
23+
local_relative_key: "Local translation"
2224

2325
my_component:
2426
message: "OH NO! (you shouldn't see me)"

test/sandbox/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
get :virtual_path_reset, to: "integration_examples#virtual_path_reset"
3737
post :create, to: "integration_examples#create"
3838
get :turbo_content_type, to: "integration_examples#turbo_content_type"
39+
get :translations_in_block, to: "integration_examples#translations_in_block"
3940
post :submit, to: "integration_examples#submit"
4041

4142
resources :posts

test/sandbox/test/integration_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,4 +774,14 @@ def test_virtual_path_reset
774774
assert_select "#before", text: "Hello world!"
775775
assert_select "#after", text: "Hello world!"
776776
end
777+
778+
def test_works_with_translations_in_block
779+
get "/translations_in_block"
780+
781+
assert_select "#outside", text: "Local translation"
782+
assert_select "#inside", text: "hello,world! Local translation"
783+
assert_select "#slot .title", text: "Local translation"
784+
assert_select "#slot .item", text: "Local translation"
785+
assert_select "#slot .footer", text: "Local translation"
786+
end
777787
end

0 commit comments

Comments
 (0)