Skip to content

Commit

Permalink
Use regional fallbacks from i18n (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
lux-shaun committed Sep 10, 2022
1 parent a84f664 commit 49ee345
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions bridgetown-core/test/test_locales.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 49ee345

Please sign in to comment.