From bd0753b3966051788dd5818e055981fa8585bd67 Mon Sep 17 00:00:00 2001 From: Juan Guecaimburu Date: Fri, 14 Jan 2022 15:11:58 -0300 Subject: [PATCH] Add support to locales with lowdash in Resolver::PathParser 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. --- actionpack/test/controller/localized_templates_test.rb | 7 +++++++ .../test/fixtures/localized/hello_world.de_AT.html | 1 + actionview/lib/action_view/template/resolver.rb | 2 +- actionview/test/template/resolver_shared_tests.rb | 10 ++++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/fixtures/localized/hello_world.de_AT.html diff --git a/actionpack/test/controller/localized_templates_test.rb b/actionpack/test/controller/localized_templates_test.rb index 5c5cef66d56..d4cd82368e5 100644 --- a/actionpack/test/controller/localized_templates_test.rb +++ b/actionpack/test/controller/localized_templates_test.rb @@ -45,4 +45,11 @@ class LocalizedTemplatesTest < ActionController::TestCase 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 diff --git a/actionpack/test/fixtures/localized/hello_world.de_AT.html b/actionpack/test/fixtures/localized/hello_world.de_AT.html new file mode 100644 index 00000000000..3b6e1802611 --- /dev/null +++ b/actionpack/test/fixtures/localized/hello_world.de_AT.html @@ -0,0 +1 @@ +Guten Morgen \ No newline at end of file diff --git a/actionview/lib/action_view/template/resolver.rb b/actionview/lib/action_view/template/resolver.rb index b86a365a6b4..60dea1993b2 100644 --- a/actionview/lib/action_view/template/resolver.rb +++ b/actionview/lib/action_view/template/resolver.rb @@ -19,7 +19,7 @@ module ActionView 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})?" variants = "[^.]*" %r{ diff --git a/actionview/test/template/resolver_shared_tests.rb b/actionview/test/template/resolver_shared_tests.rb index 37452defd27..e7f17fbf457 100644 --- a/actionview/test/template/resolver_shared_tests.rb +++ b/actionview/test/template/resolver_shared_tests.rb @@ -232,4 +232,14 @@ module ResolverSharedTests 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