Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at implementing site locales and translations #131

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bridgetown-core/lib/bridgetown-core/concerns/site/localizable.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions bridgetown-core/lib/bridgetown-core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/drops/site_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions bridgetown-core/lib/bridgetown-core/ruby_template_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bridgetown-core/lib/bridgetown-core/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Site
include Configurable
include Content
include Extensible
include Localizable
include Processable
include Renderable
include Writable
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions bridgetown-core/lib/bridgetown-core/tags/t.rb
Original file line number Diff line number Diff line change
@@ -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)