Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative translations don't work in initializer #1577

Closed
jcoyne opened this issue Nov 11, 2022 · 8 comments · Fixed by #1666
Closed

Relative translations don't work in initializer #1577

jcoyne opened this issue Nov 11, 2022 · 8 comments · Fixed by #1666

Comments

@jcoyne
Copy link
Collaborator

jcoyne commented Nov 11, 2022

This is similar to #74

If I have an initializer and I use the translation helper, then I get the error:

Cannot use t(".link_text") shortcut because path is not available

This happens even after calling super

If I move the translation into an instance method, it works as expected.

Using view_component-2.75.0, rails-7.0.4, ruby 3.1.0

@joelhawksley
Copy link
Member

@jcoyne thanks for filing this issue. Might you be up for writing a PR with a failing test? Perhaps @elia could have a look, then.

@elia
Copy link
Collaborator

elia commented Nov 24, 2022

This is expected, although I agree would be great to have them working in the initializer as well.

The reason it works as an instance method is that the method is most likely called after initialization and compilation of the component. Happy to review any proposal to make it work from the get go tho 🙌

@joelhawksley
Copy link
Member

@jcoyne given @elia's response, are there any documentation changes you think we should make?

@jcoyne
Copy link
Collaborator Author

jcoyne commented Nov 28, 2022

I suppose some warning in the documentation would be nice. I'm not at all clear why this timing concern exists. Maybe an explanation about that?

@joelhawksley
Copy link
Member

@jcoyne perhaps we could just add a warning for now? Like this test case:

assert_includes err.message, "can't be used during initialization"
. We could also add something to the docs.

@jcoyne
Copy link
Collaborator Author

jcoyne commented Nov 28, 2022

@joelhawksley that would be really helpful

@joelhawksley
Copy link
Member

@jcoyne cool. Would you be up for writing a PR? I can help clean up language if you get something started ❤️

@jcoyne
Copy link
Collaborator Author

jcoyne commented Nov 28, 2022

No, because I don't understand the order problem were seeing. I don't understand what the compile step is or what that does.

joelhawksley added a commit that referenced this issue Dec 22, 2022
In order for translations to work in an initializer,
ActionView requires @virtual_path to be set. To set it,
we allocate the component, set @virtual_path, then
call the component's initializer. This is either terrible,
brilliant, or both.

Closes #1577
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants