From 6b646256e26a28ffd89339b0acab0f86d70f7acc Mon Sep 17 00:00:00 2001 From: Jared White Date: Tue, 1 Sep 2020 09:36:14 -0700 Subject: [PATCH] First pass at implementing site locales and translations --- .../concerns/site/localizable.rb | 20 +++++++++++++++++++ .../lib/bridgetown-core/configuration.rb | 2 ++ .../lib/bridgetown-core/drops/site_drop.rb | 2 +- .../lib/bridgetown-core/ruby_template_view.rb | 4 ++++ bridgetown-core/lib/bridgetown-core/site.rb | 2 ++ bridgetown-core/lib/bridgetown-core/tags/t.rb | 14 +++++++++++++ 6 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb create mode 100644 bridgetown-core/lib/bridgetown-core/tags/t.rb diff --git a/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb b/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb new file mode 100644 index 000000000..672322d63 --- /dev/null +++ b/bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Bridgetown + module Site::Localizable + def locale + if @locale + @locale + else + @locale = ENV.fetch("BRIDGETOWN_LOCALE", config[:default_locale]).to_sym + I18n.load_path << Dir[in_source_dir("_locales") + "/*.yml"] + I18n.available_locales = config[:available_locales] + I18n.default_locale = @locale + end + end + + def locale=(new_locale) + I18n.locale = @locale = new_locale.to_sym + end + end +end diff --git a/bridgetown-core/lib/bridgetown-core/configuration.rb b/bridgetown-core/lib/bridgetown-core/configuration.rb index 957864099..9a59ca4a5 100644 --- a/bridgetown-core/lib/bridgetown-core/configuration.rb +++ b/bridgetown-core/lib/bridgetown-core/configuration.rb @@ -52,6 +52,8 @@ class Configuration < ActiveSupport::HashWithIndifferentAccess "show_dir_listing" => false, # Output Configuration + "available_locales" => ["en"], + "default_locale" => "en", "permalink" => "date", "timezone" => nil, # use the local timezone diff --git a/bridgetown-core/lib/bridgetown-core/drops/site_drop.rb b/bridgetown-core/lib/bridgetown-core/drops/site_drop.rb index 6fb69d1d0..b50bcb061 100644 --- a/bridgetown-core/lib/bridgetown-core/drops/site_drop.rb +++ b/bridgetown-core/lib/bridgetown-core/drops/site_drop.rb @@ -8,7 +8,7 @@ class SiteDrop < Drop mutable false def_delegator :@obj, :data - def_delegators :@obj, :time, :pages, :static_files, :tags, :categories + def_delegators :@obj, :locale, :time, :pages, :static_files, :tags, :categories private def_delegator :@obj, :config, :fallback_data diff --git a/bridgetown-core/lib/bridgetown-core/ruby_template_view.rb b/bridgetown-core/lib/bridgetown-core/ruby_template_view.rb index aa4bbdbd7..94b3bb682 100644 --- a/bridgetown-core/lib/bridgetown-core/ruby_template_view.rb +++ b/bridgetown-core/lib/bridgetown-core/ruby_template_view.rb @@ -20,6 +20,10 @@ def initialize(site) def webpack_path(asset_type) Bridgetown::Utils.parse_webpack_manifest_file(@site, asset_type.to_s) end + + def t(*args) + I18n.send :t, *args + end end attr_reader :layout, :page, :site, :content diff --git a/bridgetown-core/lib/bridgetown-core/site.rb b/bridgetown-core/lib/bridgetown-core/site.rb index 0d1da2afa..ad1d05d97 100644 --- a/bridgetown-core/lib/bridgetown-core/site.rb +++ b/bridgetown-core/lib/bridgetown-core/site.rb @@ -7,6 +7,7 @@ class Site include Configurable include Content include Extensible + include Localizable include Processable include Renderable include Writable @@ -25,6 +26,7 @@ class Site # config - A Hash containing site configuration details. def initialize(config) self.config = config + locale @plugin_manager = PluginManager.new(self) @cleaner = Cleaner.new(self) diff --git a/bridgetown-core/lib/bridgetown-core/tags/t.rb b/bridgetown-core/lib/bridgetown-core/tags/t.rb new file mode 100644 index 000000000..cf31d8c2e --- /dev/null +++ b/bridgetown-core/lib/bridgetown-core/tags/t.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Bridgetown + module Tags + class TranslationTag < Liquid::Tag + def render(_context) + key = @markup.strip + I18n.t(key) + end + end + end +end + +Liquid::Template.register_tag("t", Bridgetown::Tags::TranslationTag)