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
Add support to locales with lowdash in Resolver::PathParser #44174
Add support to locales with lowdash in Resolver::PathParser #44174
Conversation
In previous versions of Rails, a dynamic regex was built to find templates. After that, PathParser started to be used to both match and sort templates. With the dynamic regex, templates with lowdash locales (es_AR) were found properly. But the PathParser regex does not match locales with this format, only allowing dash (es-AR) or no dash (es). Templates with lowdash locales have a wrong virtual path and get filtered. In this commit the PathParser regex is extended to support the lowdash.
@@ -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 = "[a-z]{2}(?:[-_][A-Z]{2})?" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't make this regex configurable? For example, we use a non-standard locale "global", it does not match this regular expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify why global
is desirable over the default of not specifying a locale? I don't think that's a pattern we've seen or have tests for. Do you have :global
configured in I18n.available_locales
? I'd have expected everything to mostly follow ISO 639-1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example we use "global" virtual locale to distinguish US users and all other english speaking users. They use totally different views, but both english language at the same time. It's very easy to use .global.haml views for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that rails-i18n
only supports ISO codes listed here but I haven't tried anything else. There could be a way to get around that. For Ruby i18n
, RFC4646 and RFC4647 compliance doesn't seem to be enforced.
For your specific use case, can you explain the rationale behind using a non-standard locale global
vs the standard which accepts a language and a region code? en-US
for US users, en
for all other english speaking users. That communicates that english is the language in both cases, but you have specific needs for the US region. Not sure what would be the right way to group contries, let's say "south-latam" vs "north-latam", but I can imagine variants in that case.
If non-standard locales need to be supported for backward compatibility reasons, my impression is that using I18n
config to build the regex would be safer than allowing a configurable regex, but there could be a performance tradeoff. Also, a refactor might be needed to avoid hacking the details back to the build regex method. Of course this is just an opinion, I don't have the full picture to make this call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
global name selected exactly for countries grouping and we didn't want to deal with region that time. having only locale seemed very simple that time. also in my opinion different regions like en-us en-gb means different yml files but in our case global=en and only haml templates are different. honestly I wouldn't want to rename all our internals to en-us/en just for one regexp.
not an our case and pretty rare but what if I'd want to have imaginary locales like elf/goblin/ork.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem is that the behavior that anything you config in I18n.available_locales
would work (intended or unintended) was working before.
And in Rails 7 that behavior was removed.
This PR doesn't provide that behavior. It just adds lowdash
support which is POSIX locale format
Also I don't know about your application @catz. But maybe you could make it work if you use request variant
global
I think that making the REGEX configurable has a lot of impact.
Maybe we should make that considers the I18n available_locales
configuration ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhawthorn any thoughts on this discussion? I can modify this PR if accepting any I18n
config is desired. I'll need some direction in that case as some decisions were probably taken when the code was changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the idea, @jvillarejo! request variant is suitable for our needs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect if we use only I18n. available_locales
we'd likely have the same type of issue, with folks who use AV locals which aren't configured as available.
I've merged this PR (thank you!) but if the lack of support is causing hardship would definitely consider a PR to support any available_locale OR the existing format 🙇♂️.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually encountered the same problem when trying to update an app to rails 7 that uses non-standard locale names. In this case it is because of multi-tenancy: The app hosts several tenants and uses i18n for per-tenant text changes and (sometimes) per-tenant templates.
Instead of burying this in an old discussion, I opted to implement the suggestion with I18n.available_locales
here: #45169
Any other solution that let's me keep the locale names would be appreciated just as well.
Thanks! |
…th_parser_locale_regex Add support to locales with lowdash in Resolver::PathParser
In Rails 6.1.4.4, a dynamic regex was built to find templates.
After this commit 9e0c42b
PathParser
started to be used to both match and sort templates. As thePathParser
regex does not match locales with lowdash format (ex::es_AR
), templates including those locales have a wrong virtual path and get filtered.In this commit the regex gets modified to allow lowdash.
Further discussion: #44154
CC: @jhawthorn