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 all available locales for template lookups #45169

Merged

Conversation

oneiros
Copy link
Contributor

@oneiros oneiros commented May 24, 2022

Summary

This is a follow up to #44154 and especially the discussion here: https://github.com/rails/rails/pull/44174/files#r785160819

This PR tries to implement the idea from that discussion, to just use I18n.available_locales to build a list of allowed locales in a template file name.

Other Information

Background: The i18n gem is relatively lax when it comes to naming locales. It does not enforce any standard. Using non-standard locale names also worked fine with translated templates up until (and including) rails 6.1.

Rails 7 changed the template lookup and enforced a naming scheme for locales. This poses a problem for legacy apps that use non-standard locale names.

In my case, I am currently trying to upgrade an app to rails 7 that is big on multitenancy and uses i18n to allow for per-tenant text changes and layout differences. Thus the tenant name is sometimes part of the locale. I worked on another app (not yet on rails 7) that used non-standard locale names to provide an alternative version of the site in "simple language", something that was required by law in this case.

Stemming from the discussion in the ticket mentioned above, the naming rules have already been relaxed a little. But as the discussion in the PR shows, this is not enough for all apps.

This PR is only a suggestion. I would be glad to have any solution that would let me keep the locale names in the aformentioned apps. And of course I am happy to help in any way I can.

@@ -19,7 +19,7 @@ class PathParser # :nodoc:
def build_path_regex
handlers = Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
formats = Template::Types.symbols.map { |x| Regexp.escape(x) }.join("|")
locales = "[a-z]{2}(?:[-_][A-Z]{2})?"
locales = I18n.available_locales.map { |x| Regexp.escape(x) }.join("|")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Regexp.union

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! That is a great tip. I was (half) blindly just copying what was done above, but Regexp.union sure makes it nicer. I changed this now and took the liberty to adjust the two lines above as well for consistency reasons.

@jvillarejo
Copy link
Contributor

jvillarejo commented May 25, 2022

Hey @oneiros thanks for taking time to read the discussion.

When I've researched about the issue, at some point this behavior was decided to be changed for a explicit REGEX ( I don't know why 🤷‍♂️ )

I think that it would be best that if I18n.available_locales is empty to use the regexp "[a-z]{2}(?:[-_][A-Z]{2})?" as default value.

Or maybe to union it too with the available_locales

locales = Regexp.union( I18n.available_locales.map { |x| Regexp.escape(x) } + [/[a-z]{2}(?:[-_][A-Z]{2})?/] ) 

What do you think about it ?

PS: Some of tests related to this didn't pass.

@oneiros
Copy link
Contributor Author

oneiros commented May 27, 2022

Or maybe to union it too with the available_locales

I think that is the best way. This makes it 100% backwards compatible to the behavior introduced in Rails 7.0. And this should work for rare edge cases, where I18n.available_locales is empty. This might be the case e.g. when someone only uses localized templates, but no translation of individual strings.

With this change I can also remove a lot of code I needed to add to existing tests. And this should also fix the failing tests outside of actionview.

Thank you so much for the suggestion!

@oneiros oneiros force-pushed the allow_all_available_locales_for_template_lookup branch from cf6dcca to b5650f2 Compare May 27, 2022 10:33
@jvillarejo
Copy link
Contributor

Greaat work @oneiros !
Hey @jhawthorn what do you think about this change?

@jhawthorn jhawthorn self-assigned this May 27, 2022
@jhawthorn
Copy link
Member

jhawthorn commented May 27, 2022

Thanks! I think this is a great fix to the issue without introducing regressions for folks who have views which they don't have in I18n.avalable_locales.

Would you mind squashing the two commits into one (and remove the @-mentions, otherwise GitHub will ping those users every time this is backported 😅)? After that happy to merge ❤️.

Following the discussion here:
https://github.com/rails/rails/pull/44174/files#r785160819

Background: The `i18n` gem is relatively lax when it comes
to naming locales. It does not enforce any standard. Thus
it is possible to have e.g. per tenant locales (think
`en_tenant1`, `en_tenant2` etc.). This also worked for
translated templates up until rails 6.1.

Rails 7 changed the template lookup and enforced a naming
scheme for locales. This poses a problem for legacy apps
that use non-standard locale names.

This commit changes the way locale names are detected in
template file names. In addition to the previously used
regexp it also allows all known locales from
`I18n.available_locales`.

This makes it backwards compatible to rails 7.0
behavior while also allowing non-standard locale names.
Thanks to jvillarejo for the great idea.

Also introduce the usage of `Regexp.union`, a wonderful
suggestion by casperisfine.
@oneiros oneiros force-pushed the allow_all_available_locales_for_template_lookup branch from b5650f2 to 12c1289 Compare May 30, 2022 07:49
@oneiros
Copy link
Contributor Author

oneiros commented May 30, 2022

Would you mind squashing the two commits into one

Done. Thank you so much for accepting this. It will be a great help.

and remove the @-mentions, otherwise GitHub will ping those users every time this is backported

Oops. I did not know that. Thanks for the heads up.

BTW, does this mean you plan to backport this to 7.0.x? This would of course be absolutely phenomenal. But if it is not possible, we might be able to live with a monkeypatch until 7.1 comes out.

Thanks again to everyone who helped out with this! It is greatly appreciated.

@jhawthorn jhawthorn merged commit a5b2b61 into rails:main May 30, 2022
bendilley pushed a commit to skillstream/rails that referenced this pull request Mar 3, 2023
…es_for_template_lookup

Allow all available locales for template lookups

(cherry picked from commit a5b2b61)
rafaelfranca added a commit that referenced this pull request Mar 3, 2023
…ales-for-template-lookup

Backport (#45169): Allow all available locales for template lookups.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants