From a295dba82d68680c079a8b36e93bade1d667b908 Mon Sep 17 00:00:00 2001 From: Kazato Sugimoto Date: Fri, 26 Apr 2024 20:45:45 +0900 Subject: [PATCH] Optimize I18n::Locale::Fallbacks#compute --- lib/i18n/locale/fallbacks.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/i18n/locale/fallbacks.rb b/lib/i18n/locale/fallbacks.rb index 14744683..79c63dbe 100644 --- a/lib/i18n/locale/fallbacks.rb +++ b/lib/i18n/locale/fallbacks.rb @@ -90,11 +90,16 @@ def inspect protected def compute(tags, include_defaults = true, exclude = []) - result = Array(tags).flat_map do |tag| - tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude - tags.each { |_tag| tags += compute(@map[_tag], false, exclude + tags) if @map[_tag] } - tags + tags = Array(tags).flat_map do |tag| + I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } end + tags -= exclude + + result = tags + tags.each do |tag| + result += compute(@map[tag], false, exclude + result) if @map[tag] + end + result.push(*defaults) if include_defaults result.uniq! result.compact!