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

Allow finding and linking to alternate locale pages #522

Merged
merged 5 commits into from
Apr 22, 2022
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
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Security/YAMLLoad:
Exclude:
- !ruby/regexp /bridgetown-core/features\/.*.rb/
- !ruby/regexp /bridgetown-core/test\/.*.rb$/
Style/FetchEnvVar:
Enabled: false
Style/FrozenStringLiteralComment:
Exclude:
- bridgetown-core/lib/site_template/**/*.rb
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module Bridgetown
autoload :LayoutPlaceable, "bridgetown-core/concerns/layout_placeable"
autoload :LayoutReader, "bridgetown-core/readers/layout_reader"
autoload :LiquidRenderable, "bridgetown-core/concerns/liquid_renderable"
autoload :Localizable, "bridgetown-core/concerns/localizable"
autoload :LiquidRenderer, "bridgetown-core/liquid_renderer"
autoload :LogAdapter, "bridgetown-core/log_adapter"
autoload :PluginContentReader, "bridgetown-core/readers/plugin_content_reader"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def apply_from_url(url)

def determine_remote_filename(arg)
if arg.end_with?(".rb")
arg.split("/").yield_self do |segments|
arg.split("/").then do |segments|
arg.sub!(%r!/#{segments.last}$!, "")
segments.last
end
Expand Down
20 changes: 20 additions & 0 deletions bridgetown-core/lib/bridgetown-core/concerns/localizable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Bridgetown
module Localizable
def all_locales
result_set = case self
when Bridgetown::Resource::Base
collection.resources
when Bridgetown::GeneratedPage
site.generated_pages
else
[]
end

result_set.select { |item| item.data.slug == data.slug }.sort_by do |item|
site.config.available_locales.index item.data.locale
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def uses_resource?
# @param strip_slash_only [Boolean] set to true if you wish "/" to be returned as ""
# @return [String]
def base_path(strip_slash_only: false)
(config[:base_path] || config[:baseurl]).yield_self do |path|
(config[:base_path] || config[:baseurl]).then do |path|
strip_slash_only ? path.to_s.sub(%r{^/$}, "") : path
end
end
Expand Down Expand Up @@ -166,7 +166,7 @@ def configure_component_paths # rubocop:todo Metrics/AbcSize
plugin_components_load_paths = Bridgetown::PluginManager.source_manifests
.filter_map(&:components)

local_components_load_paths = config["components_dir"].yield_self do |dir|
local_components_load_paths = config["components_dir"].then do |dir|
dir.is_a?(Array) ? dir : [dir]
end
local_components_load_paths.map! do |dir|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class GeneratedPageDrop < Drop
:path,
:url,
:relative_url,
:relative_path
:relative_path,
:all_locales

private def_delegator :@obj, :data, :fallback_data
end
Expand Down
3 changes: 2 additions & 1 deletion bridgetown-core/lib/bridgetown-core/drops/resource_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class ResourceDrop < Drop
:relative_url,
:date,
:taxonomies,
:relations
:relations,
:all_locales

private def_delegator :@obj, :data, :fallback_data

Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/filters/url_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def strip_index(input)
# @param input [Object] value which responds to `to_s`
# @return [String]
def strip_extname(input)
Pathname.new(input.to_s).yield_self do |path|
Pathname.new(input.to_s).then do |path|
path.dirname + path.basename(".*")
end.to_s
end
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/generated_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Bridgetown
class GeneratedPage
include LayoutPlaceable
include LiquidRenderable
include Localizable
include Publishable
include Transformable

Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def <=>(other)
# config - The Hash of configuration options.
#
# Returns a new instance.
def initialize(config = {})
def initialize(config = {}) # rubocop:disable Style/RedundantInitialize
# no-op for default
end
end
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/lib/bridgetown-core/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Base
include Bridgetown::Publishable
include Bridgetown::LayoutPlaceable
include Bridgetown::LiquidRenderable
include Bridgetown::Localizable

# @return [HashWithDotAccess::Hash]
attr_reader :data
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/resource/relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def to_liquid
# @return [String]
def kind_of_relation_for_type(type)
relation_schema&.each do |relation_type, collections|
collections = Array(collections).yield_self do |collections_arr|
collections = Array(collections).then do |collections_arr|
collections_arr +
collections_arr.map { |item| ActiveSupport::Inflector.pluralize(item) }
end.flatten.uniq
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def reindent_for_markdown(input)
end

if continue_processing
line_indentation = line.match(%r!^ +!).yield_self do |indent|
line_indentation = line.match(%r!^ +!).then do |indent|
indent.nil? ? "" : indent[0]
end
new_indentation = line_indentation.rjust(starting_indent_length, " ")
Expand Down
18 changes: 18 additions & 0 deletions bridgetown-core/test/resources/src/_layouts/localization.serb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="{%= site.locale %}">
<head>
<title>{{ resource.data.title }}</title>
</head>
<body>
{%= yield %}

Other Locales:

<ul>
{% resource.all_locales.each do |other_resource| %}
<li>{{ other_resource.data.title }}: {{ other_resource.relative_url }}</li>
{% end %}
</ul>

</body>
</html>
1 change: 0 additions & 1 deletion bridgetown-core/test/resources/src/_noodles/ramen.liquid
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
title: "Noodles!"
category: Low Cost
slug: ramen # temp bugfix…delete this as soon as the new Front Matter PR gets merged!
---

Mmm, {{ "yum!" }}
1 change: 1 addition & 0 deletions bridgetown-core/test/resources/src/_pages/multi-page.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
layout: localization
title: "Multi-locale page"
locale_overrides:
fr:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@

That's **nice**.

Locale: {{ resource.data.locale }}
Locale: {{ resource.data.locale }}

Other Locales:

<ul>
{%- for other_resource in resource.all_locales %}
<li>{{ other_resource.data.title }}: {{ other_resource.relative_url }}</li>
{%- endfor %}
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@

C'est **bien**.

Locale: {{ resource.data.locale }}
Locale: {{ resource.data.locale }}

Other Locales:

<ul>
{%- for other_resource in resource.all_locales %}
<li>{{ other_resource.data.title }}: {{ other_resource.relative_url }}</li>
{%- endfor %}
</ul>
2 changes: 2 additions & 0 deletions bridgetown-core/test/test_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ def select; end
assert prev.is_a?(Hash), "doc.next should be an object"
relations = actual.delete("relations")
refute_nil relations
all_locales = actual.delete("all_locales")
assert all_locales.length == 1
assert_equal expected, actual
end

Expand Down
46 changes: 46 additions & 0 deletions bridgetown-core/test/test_locales.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class TestLocales < BridgetownUnitTest
should "have the correct permalink and locale in French" do
assert_equal "/fr/second-level-page/", @french_resource.relative_url
assert_includes @french_resource.output, "<p>C’est <strong>bien</strong>.</p>\n\n<p>Locale: fr</p>"

assert_includes @french_resource.output, <<-HTML
<li>I'm a Second Level Page: /second-level-page/</li>
<li>I'm a Second Level Page in French: /fr/second-level-page/</li>
HTML
end
end

Expand All @@ -41,12 +46,53 @@ class TestLocales < BridgetownUnitTest

should "have the correct permalink and locale in English" do
assert_equal "/multi-page/", @english_resource.relative_url
assert_includes @english_resource.output, 'lang="en"'
assert_includes @english_resource.output, "<title>Multi-locale page</title>"
assert_includes @english_resource.output, "<p>English: Multi-locale page</p>"
end

should "have the correct permalink and locale in French" do
assert_equal "/fr/multi-page/", @french_resource.relative_url
assert_includes @french_resource.output, 'lang="fr"'
assert_includes @french_resource.output, "<title>Sur mesure</title>"
assert_includes @french_resource.output, "<p>French: Sur mesure</p>"

assert_includes @french_resource.output, <<-HTML
<li>Multi-locale page: /multi-page/</li>
<li>Sur mesure: /fr/multi-page/</li>
HTML
end
end

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

should "have the correct permalink and locale in English" do
assert_equal "/basefolder/multi-page/", @english_resource.relative_url
assert_includes @english_resource.output, 'lang="en"'
assert_includes @english_resource.output, "<title>Multi-locale page</title>"
assert_includes @english_resource.output, "<p>English: Multi-locale page</p>"
end

should "have the correct permalink and locale in French" do
assert_equal "/basefolder/fr/multi-page/", @french_resource.relative_url
assert_includes @french_resource.output, 'lang="fr"'
assert_includes @french_resource.output, "<title>Sur mesure</title>"
assert_includes @french_resource.output, "<p>French: Sur mesure</p>"

assert_includes @french_resource.output, <<-HTML
<li>Multi-locale page: /basefolder/multi-page/</li>
<li>Sur mesure: /basefolder/fr/multi-page/</li>
HTML
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def run(default_config, templates, site_title) # rubocop:todo Metrics/AbcSize

next unless template_config["enabled"]

if template.site.config.available_locales.size > 1 && !template_config["locale"]
template_config["locale"] =
template.data["locale"].to_s
end

@logging_lambda.call "found page: #{template.path}", "debug" unless @debug

# Request all documents in all collections that the user has requested
Expand Down