From 0263688621b3a8065bba4532ceff57562adb4e13 Mon Sep 17 00:00:00 2001 From: Jared White Date: Wed, 25 May 2022 23:32:20 -0700 Subject: [PATCH] Add `t` Liquid filter, allow I18n fallbacks --- bridgetown-core/lib/bridgetown-core.rb | 4 ++++ .../concerns/site/localizable.rb | 4 ++++ .../lib/bridgetown-core/filters.rb | 3 +++ .../filters/translation_filters.rb | 11 ++++++++++ .../test/resources/src/_locales/en.yml | 4 ++++ .../test/resources/src/_locales/fr.yml | 2 ++ .../resources/src/_pages/multi-page.multi.md | 2 ++ bridgetown-core/test/test_locales.rb | 21 +++++++++++++++++++ 8 files changed, 51 insertions(+) create mode 100644 bridgetown-core/lib/bridgetown-core/filters/translation_filters.rb create mode 100644 bridgetown-core/test/resources/src/_locales/en.yml create mode 100644 bridgetown-core/test/resources/src/_locales/fr.yml diff --git a/bridgetown-core/lib/bridgetown-core.rb b/bridgetown-core/lib/bridgetown-core.rb index 7a6dd804c..ffb3ca6df 100644 --- a/bridgetown-core/lib/bridgetown-core.rb +++ b/bridgetown-core/lib/bridgetown-core.rb @@ -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 diff --git a/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb b/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb index 6750339f0..d4d3acc07 100644 --- a/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb +++ b/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb @@ -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 diff --git a/bridgetown-core/lib/bridgetown-core/filters.rb b/bridgetown-core/lib/bridgetown-core/filters.rb index 22bed7d46..8acf415fc 100644 --- a/bridgetown-core/lib/bridgetown-core/filters.rb +++ b/bridgetown-core/lib/bridgetown-core/filters.rb @@ -435,3 +435,6 @@ def as_liquid(item) Liquid::Template.register_filter( Bridgetown::Filters ) +Liquid::Template.register_filter( + Bridgetown::Filters::TranslationFilters +) diff --git a/bridgetown-core/lib/bridgetown-core/filters/translation_filters.rb b/bridgetown-core/lib/bridgetown-core/filters/translation_filters.rb new file mode 100644 index 000000000..fae6636d8 --- /dev/null +++ b/bridgetown-core/lib/bridgetown-core/filters/translation_filters.rb @@ -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 diff --git a/bridgetown-core/test/resources/src/_locales/en.yml b/bridgetown-core/test/resources/src/_locales/en.yml new file mode 100644 index 000000000..b4c8a907e --- /dev/null +++ b/bridgetown-core/test/resources/src/_locales/en.yml @@ -0,0 +1,4 @@ +en: + en: English + test: + name: Test Name diff --git a/bridgetown-core/test/resources/src/_locales/fr.yml b/bridgetown-core/test/resources/src/_locales/fr.yml new file mode 100644 index 000000000..28df39f8d --- /dev/null +++ b/bridgetown-core/test/resources/src/_locales/fr.yml @@ -0,0 +1,2 @@ +en: + fr: Français diff --git a/bridgetown-core/test/resources/src/_pages/multi-page.multi.md b/bridgetown-core/test/resources/src/_pages/multi-page.multi.md index 9024b2426..7c7b3fa96 100644 --- a/bridgetown-core/test/resources/src/_pages/multi-page.multi.md +++ b/bridgetown-core/test/resources/src/_pages/multi-page.multi.md @@ -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 }} diff --git a/bridgetown-core/test/test_locales.rb b/bridgetown-core/test/test_locales.rb index df083739b..64f39cc61 100644 --- a/bridgetown-core/test/test_locales.rb +++ b/bridgetown-core/test/test_locales.rb @@ -126,5 +126,26 @@ class TestLocales < BridgetownUnitTest
  • Sur mesure: /basefolder/fr/multi-page/
  • 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, "

    English: Test Name

    " + end + + should "fall back to English on missing translation" do + assert_includes @french_resource.output, "

    Français: Test Name

    " + end + end end end