What is the recommended I18n path? #1865
-
Consider the following extremely contrived component: class HelloComponent < ViewComponent::Base
def initialize(fancy: false)
@fancy = fancy
end
def greeting
@fancy ? t('.fancy_greeting') : t('.regular_greeting')
end
end
##### hello_component.html.erb
<%= greeting %> This raises a Is something like the following where I set the translated values into variables in a class HelloComponent < ViewComponent::Base
attr_accessor :greeting
def initialize(fancy: false)
@fancy = fancy
end
def before_render
self.greeting = @fancy ? t('.fancy_greeting') : t('.regular_greeting')
end
end Even with a very small number of translated strings this feels like I'm really fighting against ViewComponent 😞 The only other option I can think of is to push the logic down into the template which feels even worse. Is there a better way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @jaredmoody, apologies for the delayed reply. I just tried copy/pasting the component in your first code block into a Rails 7.0 app, and it worked just fine. What you're doing here is 100% the "right" way to do translations in components. Is it possible a helper is being called in some other part of your code? To help further diagnose the issue, would it be possible for you to share a minimal reproduction case in the form of an app or test case? A stack trace could also be helpful. |
Beta Was this translation helpful? Give feedback.
Well now I'm the delayed reply - good to know I was doing it right, I'll try to track down why I was seeing that error with calls to
t
!