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

Add support to locales with lowdash in Resolver::PathParser #44174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions actionpack/test/controller/localized_templates_test.rb
Expand Up @@ -45,4 +45,11 @@ def test_localized_template_has_correct_header_with_no_format_in_template_name
assert_equal "Ciao Mondo", @response.body
assert_equal "text/html", @response.media_type
end

def test_use_locale_with_lowdash
I18n.locale = :"de_AT"

get :hello_world
assert_equal "Guten Morgen", @response.body
end
end
1 change: 1 addition & 0 deletions actionpack/test/fixtures/localized/hello_world.de_AT.html
@@ -0,0 +1 @@
Guten Morgen
2 changes: 1 addition & 1 deletion actionview/lib/action_view/template/resolver.rb
Expand Up @@ -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})?"

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.

Copy link
Member

@jhawthorn jhawthorn Jan 14, 2022

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

Copy link

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.

Copy link
Contributor Author

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.

Copy link

@catz catz Jan 17, 2022

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.

Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

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.

Copy link
Member

@jhawthorn jhawthorn Feb 17, 2022

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 🙇‍♂️.

Copy link
Contributor

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.

variants = "[^.]*"

%r{
Expand Down
10 changes: 10 additions & 0 deletions actionview/test/template/resolver_shared_tests.rb
Expand Up @@ -232,4 +232,14 @@ def test_returns_no_results_with_dot

assert_empty context.find_all("hello_world.html", "test", false, [], {})
end

def test_finds_template_with_lowdash_format
with_file "test/hello_world.es_AR.text.erb", "Texto simple!"

es_ar = context.find_all("hello_world", "test", false, [], locale: [:es_AR])

assert_equal 1, es_ar.size

assert_equal "Texto simple!", es_ar[0].source
end
end