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

Allow relative keys in any Ruby object #381

Merged
merged 3 commits into from
Oct 15, 2021

Conversation

javierm
Copy link
Contributor

@javierm javierm commented Jun 16, 2021

When using relative keys in controllers and mailers, it usually makes sense to assume the name of the method (such as "index" or "new") should be part of the absolute key, since they generally follow the "one method == one action" pattern and there's a view file associated with this method/action.

However, in other classes, like presenters, decorators, components, or any other plain old Ruby object, that isn't usually the case. For instance, in components the view file is associated with the whole Ruby class and not just one method.

So now the calling method is only included as part of the key in classes where it's determined the key should be relative to the method.

Closes #375.

P.S. It's the first time I check the code of this project and I basically don't know what I'm doing 😄, so any help/advice is appreciated. Particularly about how to properly use the key_relative_to_method? method (if at all).

@javierm
Copy link
Contributor Author

javierm commented Jun 16, 2021

The approach currently taken in this pull request isn't compatible with existing applications which might assume the method name will be part of the generated translation key. Any advice about how to handle this scenario?

@glebm
Copy link
Owner

glebm commented Jun 16, 2021

Perhaps we could make the regexp for including the method name a setting, e.g. relative.include_method_name_path_regex.

For backwards compatibility, it can default to .*, or we can use the new default and bump major version.

@glebm
Copy link
Owner

glebm commented Jun 16, 2021

Rather than a regex settings, a list of directories would probably be better, as we already have search.relative_roots like that

@glebm
Copy link
Owner

glebm commented Jun 16, 2021

Ah but there we have normalized_path so a filename regexp setting may actually make more sense, I'm not sure

When using relative keys in controllers and mailers, it usually makes
sense to assume the name of the method (such as "index" or "new") should
be part of the absolute key, since they generally follow the "one method
== one action" pattern and there's a view file associated with this
action.

However, in other classes, like presenters, decorators, components, or
any other plain old Ruby object, that isn't usually the case. For
instance, in components the view file is associated with the whole Ruby
class and not just one method.

So now the calling method is only included as part of the key in classes
where it's determined the key should be relative to the method.
@javierm javierm marked this pull request as draft June 24, 2021 16:49
By default the list of files where these keys are excluded is empty in
order to provide backwards compatibility.
@javierm javierm marked this pull request as ready for review June 24, 2021 17:00
@javierm
Copy link
Contributor Author

javierm commented Jun 24, 2021

@glebm I've updated the pull request based on your comments. Again, I'm not familiar with this code, so any suggestions are much appreciated 😉.

@javierm
Copy link
Contributor Author

javierm commented Jun 24, 2021

I forgot to mention I'm using an exclude option instead of an include option because it seemed easier to configure; using an include option in another application would probably always require adding app/mailers and app/controllers there 🤔.

@endverbraucher
Copy link

Hi, this would be a useful feature for us, because we did run in similar problems with interactors and components. Can we provide any support to get this merged and released?

@glebm glebm merged commit 265b80d into glebm:main Oct 15, 2021
@glebm
Copy link
Owner

glebm commented Oct 15, 2021

Ah, this slipped through the cracks.

@javierm javierm deleted the relative_keys_in_poros branch October 15, 2021 19:07
@javierm javierm restored the relative_keys_in_poros branch October 15, 2021 19:09
@javierm javierm deleted the relative_keys_in_poros branch October 15, 2021 19:09
@developius
Copy link

developius commented Jan 26, 2022

I'm interested in using this option with View Components using the sidecar setup, where you end up with this:

app/components/my_component/
│   my_component.html.erb
│   my_component.en.yml
│   my_component.js
│   my_component.scss

Is there a way for the relative_exclude_method_name_paths option to work for this scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for shallow resolution of custom relative i18n keys
4 participants