From f0d5ffd567e4f33163e6c8dc0e6c17230feb8d0e Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 30 May 2022 11:49:40 -0700 Subject: [PATCH] Merge pull request #45169 from oneiros/allow_all_available_locales_for_template_lookup Allow all available locales for template lookups (cherry picked from commit a5b2b6155ceac1c073979272bb1c424fdf35cbc7) --- actionview/lib/action_view/template/resolver.rb | 8 +++++--- actionview/test/template/resolver_shared_tests.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/actionview/lib/action_view/template/resolver.rb b/actionview/lib/action_view/template/resolver.rb index 60dea1993b24d..c4ad350bc4fc9 100644 --- a/actionview/lib/action_view/template/resolver.rb +++ b/actionview/lib/action_view/template/resolver.rb @@ -17,9 +17,11 @@ class PathParser # :nodoc: ParsedPath = Struct.new(:path, :details) 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})?" + handlers = Regexp.union(Template::Handlers.extensions.map(&:to_s)) + formats = Regexp.union(Template::Types.symbols.map(&:to_s)) + available_locales = I18n.available_locales.map(&:to_s) + regular_locales = [/[a-z]{2}(?:[-_][A-Z]{2})?/] + locales = Regexp.union(available_locales + regular_locales) variants = "[^.]*" %r{ diff --git a/actionview/test/template/resolver_shared_tests.rb b/actionview/test/template/resolver_shared_tests.rb index e7f17fbf45737..db9d4b9072244 100644 --- a/actionview/test/template/resolver_shared_tests.rb +++ b/actionview/test/template/resolver_shared_tests.rb @@ -242,4 +242,16 @@ def test_finds_template_with_lowdash_format assert_equal "Texto simple!", es_ar[0].source end + + def test_finds_template_with_arbitrarily_formatted_locale + I18n.backend.store_translations(:en_customer1, { hello: "hello" }) + with_file "test/hello_world.en_customer1.text.erb", "Good day, world." + + templates = context.find_all("hello_world", "test", false, [], locale: [:en_customer1]) + + assert_equal 1, templates.size + assert_equal "Good day, world.", templates[0].source + ensure + I18n.reload! + end end