diff --git a/features/localizable.feature b/features/localizable.feature
new file mode 100644
index 0000000..b124a93
--- /dev/null
+++ b/features/localizable.feature
@@ -0,0 +1,173 @@
+Feature: Localizable blog
+
+ Scenario: Article list is scoped to a single language
+ Given the Server is running at "localizable-app"
+
+ When I go to "/index.html"
+ Then I should see "January"
+ And I should not see "Январь"
+
+ When I go to "/ru/index.html"
+ Then I should see "Январь"
+ And I should not see "January"
+
+
+ Scenario: Paginated article list is scoped to a single language
+ Given a fixture app "localizable-app"
+ And a file named "config.rb" with:
+ """
+ activate :i18n
+ activate :blog, localizable: true, permalink: '{title}.html', paginate: true
+
+ ignore 'templates/*'
+ """
+
+ Given the Server is running at "localizable-app"
+
+ When I go to "/index.html"
+ Then I should see "January"
+ And I should not see "Январь"
+
+ When I go to "/ru/index.html"
+ Then I should see "Январь"
+ And I should not see "January"
+
+
+ Scenario: Non-default locale article paths are prefixed when mount_at_root is used
+ Given the Server is running at "localizable-app"
+
+ When I go to "/january.html"
+ Then I should see "January"
+ And I should not see "Январь"
+
+ When I go to "/en/january.html"
+ Then the status code should be "404"
+
+ When I go to "/ru/january.html"
+ Then I should see "Январь"
+ And I should not see "January"
+
+
+ Scenario: All article paths are prefixed with locale when mount_at_root is not used
+ Given a fixture app "localizable-app"
+ And a file named "config.rb" with:
+ """
+ activate :i18n, mount_at_root: false
+ activate :blog, localizable: true, permalink: '{title}.html'
+
+ ignore 'templates/*'
+ """
+
+ Given the Server is running at "localizable-app"
+
+ When I go to "/january.html"
+ Then the status code should be "404"
+
+ When I go to "/en/january.html"
+ Then I should see "January"
+ And I should not see "Январь"
+
+ When I go to "/ru/january.html"
+ Then I should see "Январь"
+ And I should not see "January"
+
+
+ Scenario: Paginated tags are separated by locale
+ Given a fixture app "localizable-app"
+ And a file named "config.rb" with:
+ """
+ activate :i18n
+ activate :blog, localizable: true,
+ paginate: true,
+ permalink: '{title}.html',
+ tag_template: 'templates/tag.html'
+
+ ignore 'templates/*'
+ """
+
+ Given the Server is running at "localizable-app"
+
+ When I go to "/tags/month.html"
+ Then I should see "Tag 'month'"
+ Then I should see "January"
+ And I should not see "Январь"
+
+ When I go to "/tags/month/page/2.html"
+ Then I should see "Tag 'month'"
+ Then I should see "July"
+ And I should not see "Июль"
+
+ When I go to "/ru/tags/winter.html"
+ Then I should see "Тег 'winter'"
+ Then I should see "Январь"
+ And I should not see "January"
+
+ When I go to "/ru/tags/month/page/2.html"
+ Then I should see "Тег 'month'"
+ Then I should see "Июль"
+ And I should not see "July"
+
+
+ Scenario: Tags are separated by locale
+ Given a fixture app "localizable-app"
+ And a file named "config.rb" with:
+ """
+ activate :i18n
+ activate :blog, localizable: true, permalink: '{title}.html', tag_template: 'templates/tag.html'
+
+ ignore 'templates/*'
+ """
+
+ Given the Server is running at "localizable-app"
+
+ When I go to "/tags/winter.html"
+ Then I should see "Tag 'winter'"
+ Then I should see "January"
+ And I should not see "Январь"
+
+ When I go to "/ru/tags/winter.html"
+ Then I should see "Тег 'winter'"
+ Then I should see "Январь"
+ And I should not see "January"
+
+ Scenario: Calendar pages are separated by locale
+ Given a fixture app "localizable-app"
+ And a file named "config.rb" with:
+ """
+ activate :i18n
+ activate :blog, localizable: true, permalink: '{title}.html', calendar_template: 'templates/calendar.html'
+
+ ignore 'templates/*'
+ """
+
+ Given a successfully built app at "localizable-app"
+
+ Then the file "build/2017/01.html" should contain "January 2017"
+ And the file "build/ru/2017/01.html" should contain "Январь 2017"
+
+
+ Scenario: Tag and Calendar links are separated by locale
+ Given a fixture app "localizable-app"
+ And a file named "config.rb" with:
+ """
+ activate :i18n
+ activate :blog, localizable: true, permalink: '{title}.html', tag_template: 'templates/tag.html', calendar_template: 'templates/calendar.html', layout: 'article'
+
+ ignore 'templates/*'
+ """
+
+ Given the Server is running at "localizable-app"
+
+ When I go to "/january.html"
+ Then I should see "winter - /tags/winter.html"
+ Then I should see "month - /tags/month.html"
+ Then I should see "2017 - /2017.html"
+ Then I should see "1 - /2017/01.html"
+ Then I should see "1 - /2017/01/01.html"
+
+ When I go to "/ru/january.html"
+ Then I should see "winter - /ru/tags/winter.html"
+ Then I should see "month - /ru/tags/month.html"
+ Then I should see "2017 - /ru/2017.html"
+ Then I should see "1 - /ru/2017/01.html"
+ Then I should see "2 - /ru/2017/01/02.html"
diff --git a/fixtures/localizable-app/config.rb b/fixtures/localizable-app/config.rb
new file mode 100644
index 0000000..7c20533
--- /dev/null
+++ b/fixtures/localizable-app/config.rb
@@ -0,0 +1,4 @@
+activate :i18n
+activate :blog, localizable: true, permalink: '{title}.html'
+
+ignore 'templates/*'
diff --git a/fixtures/localizable-app/locales/en.yml b/fixtures/localizable-app/locales/en.yml
new file mode 100644
index 0000000..cb295e2
--- /dev/null
+++ b/fixtures/localizable-app/locales/en.yml
@@ -0,0 +1,8 @@
+---
+en:
+ title: International Blog
+ overview: Overview
+ tag: Tag
+ page: Page
+ previous: Previous
+ next: Next
diff --git a/fixtures/localizable-app/locales/ru.yml b/fixtures/localizable-app/locales/ru.yml
new file mode 100644
index 0000000..3192257
--- /dev/null
+++ b/fixtures/localizable-app/locales/ru.yml
@@ -0,0 +1,8 @@
+---
+ru:
+ title: Интернациональный Блог
+ overview: Обзор
+ tag: Тег
+ page: Страница
+ previous: Предыдущий
+ next: Следующий
diff --git a/fixtures/localizable-app/source/2016-03-01-march.html.erb b/fixtures/localizable-app/source/2016-03-01-march.html.erb
new file mode 100644
index 0000000..d7a4899
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-03-01-march.html.erb
@@ -0,0 +1,6 @@
+---
+title: March
+tags: spring, month
+locale: en
+---
+
March
diff --git a/fixtures/localizable-app/source/2016-03-02-march.html.erb b/fixtures/localizable-app/source/2016-03-02-march.html.erb
new file mode 100644
index 0000000..85e01f8
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-03-02-march.html.erb
@@ -0,0 +1,6 @@
+---
+title: Март
+tags: spring, month
+locale: ru
+---
+Март
diff --git a/fixtures/localizable-app/source/2016-04-01-april.html.erb b/fixtures/localizable-app/source/2016-04-01-april.html.erb
new file mode 100644
index 0000000..ccc228f
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-04-01-april.html.erb
@@ -0,0 +1,6 @@
+---
+title: April
+tags: spring, month
+locale: en
+---
+April
diff --git a/fixtures/localizable-app/source/2016-04-02-april.html.erb b/fixtures/localizable-app/source/2016-04-02-april.html.erb
new file mode 100644
index 0000000..19d878e
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-04-02-april.html.erb
@@ -0,0 +1,6 @@
+---
+title: Апрель
+tags: spring, month
+locale: ru
+---
+Апрель
diff --git a/fixtures/localizable-app/source/2016-05-01-may.html.erb b/fixtures/localizable-app/source/2016-05-01-may.html.erb
new file mode 100644
index 0000000..fe1afec
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-05-01-may.html.erb
@@ -0,0 +1,6 @@
+---
+title: May
+tags: spring, month
+locale: en
+---
+May
diff --git a/fixtures/localizable-app/source/2016-05-02-may.html.erb b/fixtures/localizable-app/source/2016-05-02-may.html.erb
new file mode 100644
index 0000000..3e2886c
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-05-02-may.html.erb
@@ -0,0 +1,6 @@
+---
+title: Май
+tags: spring, month
+locale: ru
+---
+Май
diff --git a/fixtures/localizable-app/source/2016-06-01-june.html.erb b/fixtures/localizable-app/source/2016-06-01-june.html.erb
new file mode 100644
index 0000000..ded4aad
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-06-01-june.html.erb
@@ -0,0 +1,6 @@
+---
+title: June
+tags: summer, month
+locale: en
+---
+June
diff --git a/fixtures/localizable-app/source/2016-06-02-june.html.erb b/fixtures/localizable-app/source/2016-06-02-june.html.erb
new file mode 100644
index 0000000..8eb2d6d
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-06-02-june.html.erb
@@ -0,0 +1,6 @@
+---
+title: Июнь
+tags: summer, month
+locale: ru
+---
+Июнь
diff --git a/fixtures/localizable-app/source/2016-07-01-july.html.erb b/fixtures/localizable-app/source/2016-07-01-july.html.erb
new file mode 100644
index 0000000..1d7d1b7
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-07-01-july.html.erb
@@ -0,0 +1,6 @@
+---
+title: July
+tags: summer, month
+locale: en
+---
+July
diff --git a/fixtures/localizable-app/source/2016-07-02-july.html.erb b/fixtures/localizable-app/source/2016-07-02-july.html.erb
new file mode 100644
index 0000000..0644e32
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-07-02-july.html.erb
@@ -0,0 +1,6 @@
+---
+title: Июль
+tags: summer, month
+locale: ru
+---
+Июль
diff --git a/fixtures/localizable-app/source/2016-08-01-august.html.erb b/fixtures/localizable-app/source/2016-08-01-august.html.erb
new file mode 100644
index 0000000..97eda99
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-08-01-august.html.erb
@@ -0,0 +1,6 @@
+---
+title: August
+tags: summer, month
+locale: en
+---
+August
diff --git a/fixtures/localizable-app/source/2016-08-02-august.html.erb b/fixtures/localizable-app/source/2016-08-02-august.html.erb
new file mode 100644
index 0000000..5913104
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-08-02-august.html.erb
@@ -0,0 +1,6 @@
+---
+title: Август
+tags: summer, month
+locale: ru
+---
+Август
diff --git a/fixtures/localizable-app/source/2016-09-01-september.html.erb b/fixtures/localizable-app/source/2016-09-01-september.html.erb
new file mode 100644
index 0000000..903f5c4
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-09-01-september.html.erb
@@ -0,0 +1,6 @@
+---
+title: September
+tags: autumn, month
+locale: en
+---
+September
diff --git a/fixtures/localizable-app/source/2016-09-02-september.html.erb b/fixtures/localizable-app/source/2016-09-02-september.html.erb
new file mode 100644
index 0000000..c557134
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-09-02-september.html.erb
@@ -0,0 +1,6 @@
+---
+title: Сентябрь
+tags: autumn, month
+locale: ru
+---
+Сентябрь
diff --git a/fixtures/localizable-app/source/2016-10-01-october.html.erb b/fixtures/localizable-app/source/2016-10-01-october.html.erb
new file mode 100644
index 0000000..b22c682
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-10-01-october.html.erb
@@ -0,0 +1,6 @@
+---
+title: October
+tags: autumn, month
+locale: en
+---
+October
diff --git a/fixtures/localizable-app/source/2016-10-02-october.html.erb b/fixtures/localizable-app/source/2016-10-02-october.html.erb
new file mode 100644
index 0000000..35714f6
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-10-02-october.html.erb
@@ -0,0 +1,6 @@
+---
+title: Октябрь
+tags: autumn, month
+locale: ru
+---
+Октябрь
diff --git a/fixtures/localizable-app/source/2016-11-01-november.html.erb b/fixtures/localizable-app/source/2016-11-01-november.html.erb
new file mode 100644
index 0000000..1bd0f2f
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-11-01-november.html.erb
@@ -0,0 +1,6 @@
+---
+title: November
+tags: autumn, month
+locale: en
+---
+November
diff --git a/fixtures/localizable-app/source/2016-11-02-november.html.erb b/fixtures/localizable-app/source/2016-11-02-november.html.erb
new file mode 100644
index 0000000..9cafbc2
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-11-02-november.html.erb
@@ -0,0 +1,6 @@
+---
+title: Ноябрь
+tags: autumn, month
+locale: ru
+---
+Ноябрь
diff --git a/fixtures/localizable-app/source/2016-12-01-december.html.erb b/fixtures/localizable-app/source/2016-12-01-december.html.erb
new file mode 100644
index 0000000..6bcab3f
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-12-01-december.html.erb
@@ -0,0 +1,6 @@
+---
+title: December
+tags: winter, month
+locale: en
+---
+December
diff --git a/fixtures/localizable-app/source/2016-12-02-december.html.erb b/fixtures/localizable-app/source/2016-12-02-december.html.erb
new file mode 100644
index 0000000..ee7a9e9
--- /dev/null
+++ b/fixtures/localizable-app/source/2016-12-02-december.html.erb
@@ -0,0 +1,6 @@
+---
+title: Декабрь
+tags: winter, month
+locale: ru
+---
+Декабрь
diff --git a/fixtures/localizable-app/source/2017-01-01-january.html.erb b/fixtures/localizable-app/source/2017-01-01-january.html.erb
new file mode 100644
index 0000000..58dc72a
--- /dev/null
+++ b/fixtures/localizable-app/source/2017-01-01-january.html.erb
@@ -0,0 +1,6 @@
+---
+title: January
+tags: winter, month
+locale: en
+---
+January
diff --git a/fixtures/localizable-app/source/2017-01-02-january.html.erb b/fixtures/localizable-app/source/2017-01-02-january.html.erb
new file mode 100644
index 0000000..8fa01ba
--- /dev/null
+++ b/fixtures/localizable-app/source/2017-01-02-january.html.erb
@@ -0,0 +1,6 @@
+---
+title: Январь
+tags: winter, month
+locale: ru
+---
+Январь
diff --git a/fixtures/localizable-app/source/2017-02-01-february.html.erb b/fixtures/localizable-app/source/2017-02-01-february.html.erb
new file mode 100644
index 0000000..a805578
--- /dev/null
+++ b/fixtures/localizable-app/source/2017-02-01-february.html.erb
@@ -0,0 +1,6 @@
+---
+title: February
+tags: winter, month
+locale: en
+---
+February
diff --git a/fixtures/localizable-app/source/2017-02-02-february.html.erb b/fixtures/localizable-app/source/2017-02-02-february.html.erb
new file mode 100644
index 0000000..5d60e60
--- /dev/null
+++ b/fixtures/localizable-app/source/2017-02-02-february.html.erb
@@ -0,0 +1,6 @@
+---
+title: Февраль
+tags: winter, month
+locale: ru
+---
+Февраль
diff --git a/fixtures/localizable-app/source/_article_list.erb b/fixtures/localizable-app/source/_article_list.erb
new file mode 100644
index 0000000..2416aad
--- /dev/null
+++ b/fixtures/localizable-app/source/_article_list.erb
@@ -0,0 +1,17 @@
+<% if paginate && num_pages > 1 %>
+ <%= t(:page) %> <%= page_number %> / <%= num_pages %>
+<% end %>
+
+<% if paginate && prev_page %>
+ <%= link_to t(:previous), prev_page %>
+<% end %>
+
+
+ <% page_articles.each do |article| %>
+ - <%= link_to article.title, article %>
+ <% end %>
+
+
+<% if paginate && next_page %>
+ <%= link_to t(:next), next_page %>
+<% end %>
diff --git a/fixtures/localizable-app/source/layouts/article.html.erb b/fixtures/localizable-app/source/layouts/article.html.erb
new file mode 100644
index 0000000..416d425
--- /dev/null
+++ b/fixtures/localizable-app/source/layouts/article.html.erb
@@ -0,0 +1,13 @@
+<% wrap_layout :layout do %>
+
+ <% current_article.tags.each do |tag| %>
+ - <%= tag %> - <%= tag_path(tag) %>
+ <% end %>
+
+
+ - <%= current_article.date.year %> - <%= blog_year_path(current_article.date.year) %>
+ - <%= current_article.date.month %> - <%= blog_month_path(current_article.date.year, current_article.date.month) %>
+ - <%= current_article.date.day %> - <%= blog_day_path(current_article.date.year, current_article.date.month, current_article.date.day) %>
+
+ <%= yield %>
+<% end %>
diff --git a/fixtures/localizable-app/source/layouts/layout.erb b/fixtures/localizable-app/source/layouts/layout.erb
new file mode 100644
index 0000000..fb74eae
--- /dev/null
+++ b/fixtures/localizable-app/source/layouts/layout.erb
@@ -0,0 +1,10 @@
+
+
+
+
+ <%= t(:title) %>
+
+
+ <%= yield %>
+
+
diff --git a/fixtures/localizable-app/source/localizable/index.html.erb b/fixtures/localizable-app/source/localizable/index.html.erb
new file mode 100644
index 0000000..7b938cd
--- /dev/null
+++ b/fixtures/localizable-app/source/localizable/index.html.erb
@@ -0,0 +1,7 @@
+---
+pageable: true
+per_page: 5
+---
+<%= t(:overview) %>
+
+<%= partial :article_list %>
diff --git a/fixtures/localizable-app/source/templates/calendar.html.erb b/fixtures/localizable-app/source/templates/calendar.html.erb
new file mode 100644
index 0000000..fee98bc
--- /dev/null
+++ b/fixtures/localizable-app/source/templates/calendar.html.erb
@@ -0,0 +1,15 @@
+---
+pageable: true
+---
+
+ <% case page_type
+ when 'day' %>
+ <%= I18n.l(Date.new(year, month, day), format: :long) %>
+ <% when 'month' %>
+ <%= I18n.l(Date.new(year, month, 1), format: '%B %Y') %>
+ <% when 'year' %>
+ <%= year %>
+ <% end %>
+
+
+<%= partial :article_list %>
diff --git a/fixtures/localizable-app/source/templates/tag.html.erb b/fixtures/localizable-app/source/templates/tag.html.erb
new file mode 100644
index 0000000..457adbd
--- /dev/null
+++ b/fixtures/localizable-app/source/templates/tag.html.erb
@@ -0,0 +1,7 @@
+---
+pageable: true
+per_page: 6
+---
+<%= t(:tag) %> '<%= tagname %>'
+
+<%= partial :article_list %>
diff --git a/lib/middleman-blog/blog_data.rb b/lib/middleman-blog/blog_data.rb
index c1728db..f0aad4e 100644
--- a/lib/middleman-blog/blog_data.rb
+++ b/lib/middleman-blog/blog_data.rb
@@ -109,6 +109,25 @@ def tags
tags
end
+ ##
+ # Returns a map from locale to a map from tag name to an array of
+ # BlogArticles associated with that tag.
+ #
+ # @return [Hash>>>]
+ ##
+ def tags_by_locale
+ locales = Hash.new { |ls, l| ls[l] = Hash.new { |ts, t| ts[t] = [] } }
+
+ articles.each do |article|
+ article.tags.each do |tag|
+ locales[article.locale][tag] << article
+ end
+ end
+
+ # Return locales
+ locales
+ end
+
##
#
##
@@ -242,7 +261,13 @@ def convert_to_article( resource )
#
##
def template_path(template, article, extras={})
- apply_uri_template template, permalink_options(article, extras)
+ path = apply_uri_template template, permalink_options(article, extras)
+
+ if controller.options.localizable && i18n = @app.extensions[:i18n]
+ path = i18n.path_root(article.locale)[1..-1] + path
+ end
+
+ path
end
end
diff --git a/lib/middleman-blog/calendar_pages.rb b/lib/middleman-blog/calendar_pages.rb
index f49eff7..54ec932 100644
--- a/lib/middleman-blog/calendar_pages.rb
+++ b/lib/middleman-blog/calendar_pages.rb
@@ -29,7 +29,7 @@ def initialize(app, blog_controller)
# @param [Number] month
# @param [Number] day
# @return [String]
- def link(year, month=nil, day=nil)
+ def link(year, month=nil, day=nil, locale: nil)
template = if day
@day_link_template
elsif month
@@ -38,40 +38,58 @@ def link(year, month=nil, day=nil)
@year_link_template
end
- apply_uri_template template, date_to_params(Date.new(year, month || 1, day || 1))
+ link = apply_uri_template template, date_to_params(Date.new(year, month || 1, day || 1))
+
+ if locale && i18n = @blog_controller.app.extensions[:i18n]
+ link = i18n.path_root(locale)[1..-1] + link
+ end
+
+ link
end
# Update the main sitemap resource list
# @return [Array]
def manipulate_resource_list(resources)
+ resources + calendar_pages(@blog_data.articles)
+ end
+
+ private
+
+ def calendar_pages(articles, locale = :recurse)
+ if locale == :recurse
+ if @blog_controller.options.localizable
+ return articles.group_by(&:locale).map { |l, a| calendar_pages(a, l) }.flatten
+ else
+ return calendar_pages(articles, nil)
+ end
+ end
+
new_resources = []
# Set up date pages if the appropriate templates have been specified
- @blog_data.articles.group_by {|a| a.date.year }.each do |year, year_articles|
+ articles.group_by {|a| a.date.year }.each do |year, year_articles|
if @generate_year_pages && @year_template
- new_resources << year_page_resource(year, year_articles)
+ new_resources << year_page_resource(year, year_articles, locale)
end
year_articles.group_by {|a| a.date.month }.each do |month, month_articles|
if @generate_month_pages && @month_template
- new_resources << month_page_resource(year, month, month_articles)
+ new_resources << month_page_resource(year, month, month_articles, locale)
end
month_articles.group_by {|a| a.date.day }.each do |day, day_articles|
if @generate_day_pages && @day_template
- new_resources << day_page_resource(year, month, day, day_articles)
+ new_resources << day_page_resource(year, month, day, day_articles, locale)
end
end
end
end
- resources + new_resources
+ new_resources
end
- private
-
- def year_page_resource(year, year_articles)
- Sitemap::ProxyResource.new(@sitemap, link(year), @year_template).tap do |p|
+ def year_page_resource(year, year_articles, locale=nil)
+ Sitemap::ProxyResource.new(@sitemap, link(year, locale: locale), @year_template).tap do |p|
# Add metadata in local variables so it's accessible to
# later extensions
p.add_metadata locals: {
@@ -80,11 +98,13 @@ def year_page_resource(year, year_articles)
'articles' => year_articles,
'blog_controller' => @blog_controller
}
+
+ p.add_metadata(options: { locale: locale }) if locale
end
end
- def month_page_resource(year, month, month_articles)
- Sitemap::ProxyResource.new(@sitemap, link(year, month), @month_template).tap do |p|
+ def month_page_resource(year, month, month_articles, locale=nil)
+ Sitemap::ProxyResource.new(@sitemap, link(year, month, locale: locale), @month_template).tap do |p|
p.add_metadata locals: {
'page_type' => 'month',
'year' => year,
@@ -92,11 +112,13 @@ def month_page_resource(year, month, month_articles)
'articles' => month_articles,
'blog_controller' => @blog_controller
}
+
+ p.add_metadata(options: { locale: locale }) if locale
end
end
- def day_page_resource(year, month, day, day_articles)
- Sitemap::ProxyResource.new(@sitemap, link(year, month, day), @day_template).tap do |p|
+ def day_page_resource(year, month, day, day_articles, locale=nil)
+ Sitemap::ProxyResource.new(@sitemap, link(year, month, day, locale: locale), @day_template).tap do |p|
p.add_metadata locals: {
'page_type' => 'day',
'year' => year,
@@ -105,8 +127,10 @@ def day_page_resource(year, month, day, day_articles)
'articles' => day_articles,
'blog_controller' => @blog_controller
}
+
+ p.add_metadata(options: { locale: locale }) if locale
end
end
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/middleman-blog/extension.rb b/lib/middleman-blog/extension.rb
index 0d02d67..715e71d 100644
--- a/lib/middleman-blog/extension.rb
+++ b/lib/middleman-blog/extension.rb
@@ -39,6 +39,7 @@ class BlogExtension < Extension
option :publish_future_dated, false, 'Whether articles with a date in the future should be considered published'
option :custom_collections, {}, 'Hash of custom frontmatter properties to collect articles on and their options (link, template)'
option :preserve_locale, false, 'Use the global Middleman I18n.locale instead of the lang in the article\'s frontmatter'
+ option :localizable, false, 'Separate articles, tags, and calendars by language, similar to the localizable folder in middleman-i18n'
option :new_article_template, File.expand_path('../commands/article.tt', __FILE__), 'Path (relative to project root) to an ERb template that will be used to generate new articles from the "middleman article" command.'
option :default_extension, '.markdown', 'Default template extension for articles (used by "middleman article")'
diff --git a/lib/middleman-blog/helpers.rb b/lib/middleman-blog/helpers.rb
index 4e90661..5793484 100644
--- a/lib/middleman-blog/helpers.rb
+++ b/lib/middleman-blog/helpers.rb
@@ -33,6 +33,9 @@ def blog_controller(blog_name=nil)
if !blog_name
blog_controller = current_resource.blog_controller if current_resource.respond_to?(:blog_controller)
return blog_controller if blog_controller
+
+ blog_controller = current_resource.metadata[:locals]['blog_controller']
+ return blog_controller if blog_controller
end
end
@@ -82,16 +85,16 @@ def current_article
# @param [String] tag
# @param [Symbol, String] blog_name Optional name of the blog to use.
# @return [String]
- def tag_path(tag, blog_name=nil)
- build_url blog_controller(blog_name).tag_pages.link(tag)
+ def tag_path(tag, blog_name=nil, locale=I18n.locale)
+ build_url blog_controller(blog_name).tag_pages.link(tag, locale)
end
# Get a path to the given year-based calendar page, based on the +year_link+ blog setting.
# @param [Number] year
# @param [Symbol, String] blog_name Optional name of the blog to use.
# @return [String]
- def blog_year_path(year, blog_name=nil)
- build_url blog_controller(blog_name).calendar_pages.link(year)
+ def blog_year_path(year, blog_name=nil, locale=I18n.locale)
+ build_url blog_controller(blog_name).calendar_pages.link(year, locale: locale)
end
# Get a path to the given month-based calendar page, based on the +month_link+ blog setting.
@@ -99,8 +102,8 @@ def blog_year_path(year, blog_name=nil)
# @param [Number] month
# @param [Symbol, String] blog_name Optional name of the blog to use.
# @return [String]
- def blog_month_path(year, month, blog_name=nil)
- build_url blog_controller(blog_name).calendar_pages.link(year, month)
+ def blog_month_path(year, month, blog_name=nil, locale=I18n.locale)
+ build_url blog_controller(blog_name).calendar_pages.link(year, month, locale: locale)
end
# Get a path to the given day-based calendar page, based on the +day_link+ blog setting.
@@ -109,8 +112,8 @@ def blog_month_path(year, month, blog_name=nil)
# @param [Number] day
# @param [Symbol, String] blog_name Optional name of the blog to use.
# @return [String]
- def blog_day_path(year, month, day, blog_name=nil)
- build_url blog_controller(blog_name).calendar_pages.link(year, month, day)
+ def blog_day_path(year, month, day, blog_name=nil, locale=I18n.locale)
+ build_url blog_controller(blog_name).calendar_pages.link(year, month, day, locale: locale)
end
# Whether or not pagination is enabled for this template. This can be used
@@ -130,6 +133,7 @@ def page_articles(blog_name=nil)
# "articles" local variable is populated by Calendar and Tag page generators
# If it's not set then use the complete list of articles
articles = meta[:locals]["articles"] || blog(blog_name).articles
+ articles.select!{|article| article.lang == meta[:options][:locale]} if blog_controller(blog_name).options.localizable
limit ? articles.first(limit) : articles
end
@@ -159,4 +163,4 @@ def build_url(path)
end
end
end
-end
\ No newline at end of file
+end
diff --git a/lib/middleman-blog/tag_pages.rb b/lib/middleman-blog/tag_pages.rb
index 5ff24b8..79add08 100644
--- a/lib/middleman-blog/tag_pages.rb
+++ b/lib/middleman-blog/tag_pages.rb
@@ -33,8 +33,14 @@ def initialize( app, blog_controller )
# @param tag [String] Tag name
# @return [String] Safe Tag URL
##
- def link( tag )
- apply_uri_template @tag_link_template, tag: safe_parameterize( tag )
+ def link( tag, locale = nil )
+ link = apply_uri_template @tag_link_template, tag: safe_parameterize( tag )
+
+ if locale && i18n = @blog_controller.app.extensions[:i18n]
+ link = i18n.path_root(locale)[1..-1] + link
+ end
+
+ link
end
##
@@ -47,6 +53,16 @@ def manipulate_resource_list( resources )
return resources unless @generate_tag_pages
+ if @blog_controller.options.localizable
+ @blog_data.tags_by_locale.each do | locale, articles_by_tags |
+ resources += articles_by_tags.map do | tag, articles |
+ tag_page_resource( tag, articles, locale )
+ end
+ end
+
+ return resources
+ end
+
resources + @blog_data.tags.map do | tag, articles |
tag_page_resource( tag, articles )
end
@@ -64,9 +80,9 @@ def manipulate_resource_list( resources )
#
# @todo Can we inject the correct locale into the metadata here
##
- def tag_page_resource( tag, articles )
+ def tag_page_resource( tag, articles, locale = nil )
- Sitemap::ProxyResource.new( @sitemap, link( tag ), @tag_template ).tap do | p |
+ Sitemap::ProxyResource.new( @sitemap, link( tag, locale ), @tag_template ).tap do | p |
# Detect "formatted" tag in first article - trying to guess the correct format to show
# tagname = articles.first.tags.detect { |article_tag| safe_parameterize(article_tag) == tag }
@@ -79,6 +95,7 @@ def tag_page_resource( tag, articles )
'blog_controller' => @blog_controller
}
+ p.add_metadata(options: { locale: locale }) if locale
end
end