From c0d527ea3ebc72c030f9e76f723c74d69aa5e188 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Thu, 5 Oct 2017 15:27:22 +0200 Subject: [PATCH 1/7] Adding localizable app and basic features Marking feature as @wip since it's not working yet --- features/localizable.feature | 73 +++++++++++++++++++ fixtures/localizable-app/config.rb | 4 + fixtures/localizable-app/locales/en.yml | 8 ++ fixtures/localizable-app/locales/ru.yml | 8 ++ .../source/2016-03-01-march.html.erb | 6 ++ .../source/2016-03-02-march.html.erb | 6 ++ .../source/2016-04-01-april.html.erb | 6 ++ .../source/2016-04-02-april.html.erb | 6 ++ .../source/2016-05-01-may.html.erb | 6 ++ .../source/2016-05-02-may.html.erb | 6 ++ .../source/2016-06-01-june.html.erb | 6 ++ .../source/2016-06-02-june.html.erb | 6 ++ .../source/2016-07-01-july.html.erb | 6 ++ .../source/2016-07-02-july.html.erb | 6 ++ .../source/2016-08-01-august.html.erb | 6 ++ .../source/2016-08-02-august.html.erb | 6 ++ .../source/2016-09-01-september.html.erb | 6 ++ .../source/2016-09-02-september.html.erb | 6 ++ .../source/2016-10-01-october.html.erb | 6 ++ .../source/2016-10-02-october.html.erb | 6 ++ .../source/2016-11-01-november.html.erb | 6 ++ .../source/2016-11-02-november.html.erb | 6 ++ .../source/2016-12-01-december.html.erb | 6 ++ .../source/2016-12-02-december.html.erb | 6 ++ .../source/2017-01-01-january.html.erb | 6 ++ .../source/2017-01-02-january.html.erb | 6 ++ .../source/2017-02-01-february.html.erb | 6 ++ .../source/2017-02-02-february.html.erb | 6 ++ .../localizable-app/source/_article_list.erb | 17 +++++ .../localizable-app/source/layouts/layout.erb | 10 +++ .../source/localizable/index.html.erb | 7 ++ .../source/templates/calendar.html.erb | 15 ++++ .../source/templates/tag.html.erb | 7 ++ 33 files changed, 293 insertions(+) create mode 100644 features/localizable.feature create mode 100644 fixtures/localizable-app/config.rb create mode 100644 fixtures/localizable-app/locales/en.yml create mode 100644 fixtures/localizable-app/locales/ru.yml create mode 100644 fixtures/localizable-app/source/2016-03-01-march.html.erb create mode 100644 fixtures/localizable-app/source/2016-03-02-march.html.erb create mode 100644 fixtures/localizable-app/source/2016-04-01-april.html.erb create mode 100644 fixtures/localizable-app/source/2016-04-02-april.html.erb create mode 100644 fixtures/localizable-app/source/2016-05-01-may.html.erb create mode 100644 fixtures/localizable-app/source/2016-05-02-may.html.erb create mode 100644 fixtures/localizable-app/source/2016-06-01-june.html.erb create mode 100644 fixtures/localizable-app/source/2016-06-02-june.html.erb create mode 100644 fixtures/localizable-app/source/2016-07-01-july.html.erb create mode 100644 fixtures/localizable-app/source/2016-07-02-july.html.erb create mode 100644 fixtures/localizable-app/source/2016-08-01-august.html.erb create mode 100644 fixtures/localizable-app/source/2016-08-02-august.html.erb create mode 100644 fixtures/localizable-app/source/2016-09-01-september.html.erb create mode 100644 fixtures/localizable-app/source/2016-09-02-september.html.erb create mode 100644 fixtures/localizable-app/source/2016-10-01-october.html.erb create mode 100644 fixtures/localizable-app/source/2016-10-02-october.html.erb create mode 100644 fixtures/localizable-app/source/2016-11-01-november.html.erb create mode 100644 fixtures/localizable-app/source/2016-11-02-november.html.erb create mode 100644 fixtures/localizable-app/source/2016-12-01-december.html.erb create mode 100644 fixtures/localizable-app/source/2016-12-02-december.html.erb create mode 100644 fixtures/localizable-app/source/2017-01-01-january.html.erb create mode 100644 fixtures/localizable-app/source/2017-01-02-january.html.erb create mode 100644 fixtures/localizable-app/source/2017-02-01-february.html.erb create mode 100644 fixtures/localizable-app/source/2017-02-02-february.html.erb create mode 100644 fixtures/localizable-app/source/_article_list.erb create mode 100644 fixtures/localizable-app/source/layouts/layout.erb create mode 100644 fixtures/localizable-app/source/localizable/index.html.erb create mode 100644 fixtures/localizable-app/source/templates/calendar.html.erb create mode 100644 fixtures/localizable-app/source/templates/tag.html.erb diff --git a/features/localizable.feature b/features/localizable.feature new file mode 100644 index 0000000..041e169 --- /dev/null +++ b/features/localizable.feature @@ -0,0 +1,73 @@ +@wip +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" 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 %> + + + +<% if paginate && next_page %> +

<%= link_to t(:next), next_page %>

+<% 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 %> From 26ea5697dbf8cb90692d95bba625ae99a7038aa1 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Thu, 5 Oct 2017 15:37:56 +0200 Subject: [PATCH 2/7] page_articles is limited to scoped language So that overview pages don't list articles from other languages --- features/localizable.feature | 3 ++- lib/middleman-blog/extension.rb | 1 + lib/middleman-blog/helpers.rb | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/features/localizable.feature b/features/localizable.feature index 041e169..8c6dd28 100644 --- a/features/localizable.feature +++ b/features/localizable.feature @@ -1,4 +1,3 @@ -@wip Feature: Localizable blog Scenario: Article list is scoped to a single language @@ -34,6 +33,7 @@ Feature: Localizable blog And I should not see "January" + @wip Scenario: Non-default locale article paths are prefixed when mount_at_root is used Given the Server is running at "localizable-app" @@ -49,6 +49,7 @@ Feature: Localizable blog And I should not see "January" + @wip 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: 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..bcaf3e2 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 @@ -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 From 0b0960c03df888776c4aacc798b7cd1704dc172f Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Fri, 6 Oct 2017 10:51:22 +0200 Subject: [PATCH 3/7] Properly prefixes localizable article paths Following the configuration of middleman-i18n, blog articles are now prefixes with the proper locale. Articles in the default locale, when mount_at_root is used, remain unprefixed. This way we're mimicking the schema used for pages in the localizable folder in middleman-i18n enabled apps. --- features/localizable.feature | 2 -- lib/middleman-blog/blog_data.rb | 8 +++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/features/localizable.feature b/features/localizable.feature index 8c6dd28..6e047f9 100644 --- a/features/localizable.feature +++ b/features/localizable.feature @@ -33,7 +33,6 @@ Feature: Localizable blog And I should not see "January" - @wip Scenario: Non-default locale article paths are prefixed when mount_at_root is used Given the Server is running at "localizable-app" @@ -49,7 +48,6 @@ Feature: Localizable blog And I should not see "January" - @wip 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: diff --git a/lib/middleman-blog/blog_data.rb b/lib/middleman-blog/blog_data.rb index c1728db..9efd832 100644 --- a/lib/middleman-blog/blog_data.rb +++ b/lib/middleman-blog/blog_data.rb @@ -242,7 +242,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 From 75630d66a998e419322323cf8b0d9a2e9985c14b Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Fri, 6 Oct 2017 14:58:04 +0200 Subject: [PATCH 4/7] tag and calendar pages scoped to a single language So that they don't list articles from other languages. Also prefixing the paths according to middleman-i18n configuration with proper locale prefix. --- features/localizable.feature | 79 ++++++++++++++++++++++++++++ lib/middleman-blog/blog_data.rb | 19 +++++++ lib/middleman-blog/calendar_pages.rb | 56 ++++++++++++++------ lib/middleman-blog/tag_pages.rb | 25 +++++++-- 4 files changed, 159 insertions(+), 20 deletions(-) diff --git a/features/localizable.feature b/features/localizable.feature index 6e047f9..a9a2ad1 100644 --- a/features/localizable.feature +++ b/features/localizable.feature @@ -70,3 +70,82 @@ Feature: Localizable blog 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 the Server is running at "localizable-app" + + When I go to "/2017/01.html" + Then I should see "January 2017" + And I should not see "Январь" + + When I go to "/ru/2017/01.html" + Then I should see "Январь 2017" + And I should not see "January" diff --git a/lib/middleman-blog/blog_data.rb b/lib/middleman-blog/blog_data.rb index 9efd832..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 + ## # ## 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/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 From 4763eb60a7883a1202c35e94f58c874a2738e00a Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Mon, 13 Nov 2017 12:10:17 +0100 Subject: [PATCH 5/7] tag_path should point to the current locale by default --- features/localizable.feature | 21 +++++++++++++++++++ .../source/layouts/article.html.erb | 8 +++++++ lib/middleman-blog/helpers.rb | 4 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 fixtures/localizable-app/source/layouts/article.html.erb diff --git a/features/localizable.feature b/features/localizable.feature index a9a2ad1..773b3e7 100644 --- a/features/localizable.feature +++ b/features/localizable.feature @@ -149,3 +149,24 @@ Feature: Localizable blog When I go to "/ru/2017/01.html" Then I should see "Январь 2017" And I should not see "January" + + + Scenario: Tags 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', 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" + + 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" 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..90462c5 --- /dev/null +++ b/fixtures/localizable-app/source/layouts/article.html.erb @@ -0,0 +1,8 @@ +<% wrap_layout :layout do %> +
    + <% current_article.tags.each do |tag| %> +
  • <%= tag %> - <%= tag_path(tag) %>
  • + <% end %> +
+ <%= yield %> +<% end %> diff --git a/lib/middleman-blog/helpers.rb b/lib/middleman-blog/helpers.rb index bcaf3e2..339bb43 100644 --- a/lib/middleman-blog/helpers.rb +++ b/lib/middleman-blog/helpers.rb @@ -85,8 +85,8 @@ 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. From 8edc8b43a6e3ff169e1d5c678e26da2e10606dd1 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Mon, 13 Nov 2017 12:11:51 +0100 Subject: [PATCH 6/7] Changing feature to use build instead of server Since it was failing randomly with the server, but working properly when run in isolation. --- features/localizable.feature | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/features/localizable.feature b/features/localizable.feature index 773b3e7..dab2deb 100644 --- a/features/localizable.feature +++ b/features/localizable.feature @@ -140,15 +140,10 @@ Feature: Localizable blog ignore 'templates/*' """ - Given the Server is running at "localizable-app" - - When I go to "/2017/01.html" - Then I should see "January 2017" - And I should not see "Январь" + Given a successfully built app at "localizable-app" - When I go to "/ru/2017/01.html" - Then I should see "Январь 2017" - And I should not see "January" + Then the file "build/2017/01.html" should contain "January 2017" + And the file "build/ru/2017/01.html" should contain "Январь 2017" Scenario: Tags links are separated by locale From f88cb1507c9602a791684d3dc996a43ab5401fcc Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Mon, 13 Nov 2017 12:27:27 +0100 Subject: [PATCH 7/7] calendar path helpers should point to the current locale by default --- features/localizable.feature | 10 ++++++++-- .../source/layouts/article.html.erb | 5 +++++ lib/middleman-blog/helpers.rb | 14 +++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/features/localizable.feature b/features/localizable.feature index dab2deb..b124a93 100644 --- a/features/localizable.feature +++ b/features/localizable.feature @@ -146,12 +146,12 @@ Feature: Localizable blog And the file "build/ru/2017/01.html" should contain "Январь 2017" - Scenario: Tags links are separated by locale + 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', layout: 'article' + activate :blog, localizable: true, permalink: '{title}.html', tag_template: 'templates/tag.html', calendar_template: 'templates/calendar.html', layout: 'article' ignore 'templates/*' """ @@ -161,7 +161,13 @@ Feature: Localizable blog 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/source/layouts/article.html.erb b/fixtures/localizable-app/source/layouts/article.html.erb index 90462c5..416d425 100644 --- a/fixtures/localizable-app/source/layouts/article.html.erb +++ b/fixtures/localizable-app/source/layouts/article.html.erb @@ -4,5 +4,10 @@
  • <%= 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/lib/middleman-blog/helpers.rb b/lib/middleman-blog/helpers.rb index 339bb43..5793484 100644 --- a/lib/middleman-blog/helpers.rb +++ b/lib/middleman-blog/helpers.rb @@ -93,8 +93,8 @@ def tag_path(tag, blog_name=nil, locale=I18n.locale) # @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. @@ -102,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. @@ -112,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 @@ -163,4 +163,4 @@ def build_url(path) end end end -end \ No newline at end of file +end