Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 6

## main

* Allow I18n calls in `render?`.

*23tux*

* ViewComponent now works without `rails` and `railties` gems loaded, enabling compatibility with Bridgetown 2.0.

*Tom Lord*
Expand Down
3 changes: 1 addition & 2 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,14 @@ def render_in(view_context, &block)

@__vc_content_evaluated = false
@__vc_render_in_block = block
@view_context.instance_variable_set(:@virtual_path, virtual_path)

before_render

if render?
value = nil

@output_buffer.with_buffer do
@view_context.instance_variable_set(:@virtual_path, virtual_path)

rendered_template =
around_render do
render_template_for(@__vc_requested_details).to_s
Expand Down
4 changes: 4 additions & 0 deletions test/sandbox/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ en:

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

rendering_test:
i18n_test_component:
message: "I can be called in render?"
21 changes: 21 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1344,4 +1344,25 @@ def test_render_partial_with_yield_and_method_call
render_inline(PartialWithYieldAndMethodCallComponent.new)
assert_text "hello world", exact: true, normalize_ws: true
end

class I18nTestComponent < ViewComponent::Base
def message
t(".message")
end

def render?
message
end

def call
content_tag :div, t(".message")
end
end

def test_i18n_in_render_hook
vc_test_request.params[:hello] = "world"
render_inline(I18nTestComponent.new)

assert_selector("div", text: I18n.t("rendering_test.i18n_test_component.message"))
end
end
Loading