From 49ee345597bbbfb2a6075ccf42ec46387a38b294 Mon Sep 17 00:00:00 2001 From: lux-shaun <92108811+lux-shaun@users.noreply.github.com> Date: Sat, 10 Sep 2022 23:34:23 +0100 Subject: [PATCH] Use regional fallbacks from i18n (#607) --- .../concerns/site/localizable.rb | 4 +-- bridgetown-core/test/test_locales.rb | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb b/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb index 95e3731ec..f42cd24a8 100644 --- a/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb +++ b/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb @@ -10,9 +10,7 @@ def locale I18n.load_path += Dir["#{in_source_dir("_locales")}/**/*.{json,rb,yml}"] I18n.available_locales = config[:available_locales] I18n.default_locale = locale - I18n.fallbacks = (config[:available_locales] + [:en]).uniq.to_h do |available_locale| - [available_locale, [available_locale, locale, :en].uniq] - end + I18n.fallbacks = [locale, :en].uniq locale end end diff --git a/bridgetown-core/test/test_locales.rb b/bridgetown-core/test/test_locales.rb index 652611b73..5867b278a 100644 --- a/bridgetown-core/test/test_locales.rb +++ b/bridgetown-core/test/test_locales.rb @@ -165,4 +165,40 @@ def reset_i18n_config end end end + + context "fallback chain" do + setup do + reset_i18n_config + @site = resources_site + @site.process + end + + should "include English for base language" do + assert_equal %i[de en], I18n.fallbacks[:de] + assert_equal %i[fr en], I18n.fallbacks[:fr] + end + + should "include English and base language for regional locale" do + assert_equal %i[de-NL de en], I18n.fallbacks[:"de-NL"] + assert_equal %i[fr-CA fr en], I18n.fallbacks[:"fr-CA"] + end + end + + context "fallback chain with different default locale" do + setup do + reset_i18n_config + @site = resources_site("default_locale" => :es, "available_locales" => %w[en es]) + @site.process + end + + should "include both the default language and English in the fallback chain" do + assert_equal %i[de es en], I18n.fallbacks[:de] + assert_equal %i[es en], I18n.fallbacks[:es] + end + + should "include base language, default, and English for regional language" do + assert_equal %i[de-NL de es en], I18n.fallbacks[:"de-NL"] + assert_equal %i[es-MX es en], I18n.fallbacks[:"es-MX"] + end + end end