Skip to content

Commit

Permalink
Add t Liquid filter, allow I18n fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed May 26, 2022
1 parent 318af36 commit 0263688
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ def require_all(path)
require "kramdown"
require "colorator"
require "i18n"
require "i18n/backend/fallbacks"
require "faraday"
require "thor"
require "zeitwerk"

# Ensure we can set up fallbacks so the default locale gets used
I18n::Backend::Simple.include I18n::Backend::Fallbacks

module HashWithDotAccess
class Hash # :nodoc:
def to_liquid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def locale
end
I18n.available_locales = config[:available_locales]
I18n.default_locale = locale
I18n.fallbacks.each do |k, v|
v.push(locale) if k != locale && !v.include?(locale)
end
locale
end
end

Expand Down
3 changes: 3 additions & 0 deletions bridgetown-core/lib/bridgetown-core/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,6 @@ def as_liquid(item)
Liquid::Template.register_filter(
Bridgetown::Filters
)
Liquid::Template.register_filter(
Bridgetown::Filters::TranslationFilters
)
11 changes: 11 additions & 0 deletions bridgetown-core/lib/bridgetown-core/filters/translation_filters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Bridgetown
module Filters
module TranslationFilters
def t(input)
I18n.t(input.to_s)
end
end
end
end
4 changes: 4 additions & 0 deletions bridgetown-core/test/resources/src/_locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
en: English
test:
name: Test Name
2 changes: 2 additions & 0 deletions bridgetown-core/test/resources/src/_locales/fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
en:
fr: Français
2 changes: 2 additions & 0 deletions bridgetown-core/test/resources/src/_pages/multi-page.multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ locale_overrides:
---

{% if site.locale == "en" %}English:{% elsif site.locale == "fr" %}French:{% endif %} {{ resource.data.title }}

{{ site.locale | t }}: {{ "test.name" | t }}
21 changes: 21 additions & 0 deletions bridgetown-core/test/test_locales.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,26 @@ class TestLocales < BridgetownUnitTest
<li>Sur mesure: /basefolder/fr/multi-page/</li>
HTML
end

context "translation filters" do
setup do
@site = resources_site
@site.process
# @type [Bridgetown::Resource::Base]
@resources = @site.collections.pages.resources.select do |page|
page.relative_path.to_s == "_pages/multi-page.multi.md"
end
@english_resource = @resources.find { |page| page.data.locale == :en }
@french_resource = @resources.find { |page| page.data.locale == :fr }
end

should "pull in the right English translation" do
assert_includes @english_resource.output, "<p>English: Test Name</p>"
end

should "fall back to English on missing translation" do
assert_includes @french_resource.output, "<p>Français: Test Name</p>"
end
end
end
end

0 comments on commit 0263688

Please sign in to comment.