From 5e30ef98a1c14f80c59bd84bb7bd9ec7e8b14437 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Mon, 1 Jun 2015 11:51:27 -0700 Subject: [PATCH] Fix new link_to i18n w.r.t. index pages. For #850 --- middleman-core/features/i18n_link_to.feature | 25 +++++++++++++++++-- .../middleman-more/core_extensions/i18n.rb | 19 ++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/middleman-core/features/i18n_link_to.feature b/middleman-core/features/i18n_link_to.feature index ceccec5c..19e06ae5 100644 --- a/middleman-core/features/i18n_link_to.feature +++ b/middleman-core/features/i18n_link_to.feature @@ -11,6 +11,7 @@ Feature: i18n Paths --- en: msg: Hello + home: Home """ And a file named "locales/es.yml" with: """ @@ -19,10 +20,21 @@ Feature: i18n Paths paths: hello: "hola" msg: Hola + home: Casa + """ + And a file named "source/localizable/index.html.erb" with: + """ + Page: <%= t(:hom) %> """ And a file named "source/localizable/hello.html.erb" with: """ Page: <%= t(:msg) %> + + <%= link_to "Current Home", "/index.html", class: 'current' %> + <%= link_to "Other Home", "/index.html", title: "Other Home", locale: ::I18n.locale == :en ? :es : :en %> + <% link_to "/index.html", class: 'current' do %>Home: Current Block<% end %> + <% link_to "/index.html", title: "Other Home", locale: ::I18n.locale == :en ? :es : :en do %>Home: Other Block<% end %> + <% data.pages.each_with_index do |p, i| %> <%= link_to "Current #{p}", "/#{p}", class: 'current' %> <%= link_to "Other #{p}", "/#{p}", title: "Other #{p}", locale: ::I18n.locale == :en ? :es : :en %> @@ -32,17 +44,26 @@ Feature: i18n Paths """ And a file named "config.rb" with: """ - activate :i18n + activate :i18n, mount_at_root: :en """ Given the Server is running at "empty-app" When I go to "/hello.html" Then I should see "Page: Hello" + Then I should see 'Current Home' + Then I should see 'Other Home' + Then I should see 'Home: Current Block' + Then I should see 'Home: Other Block' Then I should see 'Current hello.html' Then I should see 'Other hello.html' Then I should see 'Current Block' Then I should see 'Other Block' When I go to "/es/hola.html" Then I should see "Page: Hola" + Then I should see 'Current Home' + Then I should see 'Other Home' + Then I should see 'Home: Current Block' + Then I should see 'Home: Other Block' + Then I should see 'Current hello.html' Then I should see 'Other hello.html' Then I should see 'Current Block' @@ -93,7 +114,7 @@ Feature: i18n Paths """ And a file named "config.rb" with: """ - activate :i18n + activate :i18n, mount_at_root: :en """ Given the Server is running at "empty-app" When I go to "/hello.html" diff --git a/middleman-core/lib/middleman-more/core_extensions/i18n.rb b/middleman-core/lib/middleman-more/core_extensions/i18n.rb index 0bf6b792..0cd87ff0 100644 --- a/middleman-core/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-more/core_extensions/i18n.rb @@ -64,7 +64,13 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension locale = options.delete(:locale) || ::I18n.locale href = super(path_or_resource, options) - extensions[:i18n].localized_path(href, locale) || href + + if result = extensions[:i18n].localized_path(href, locale) + result + else + # Should we log the missing file? + href + end end end @@ -116,7 +122,10 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end def localized_path(path, lang) - @lookup[path] && @lookup[path][lang] + lookup_path = path.dup + lookup_path << app.config[:index_file] if lookup_path.end_with?('/') + + @lookup[lookup_path] && @lookup[lookup_path][lang] end private @@ -222,14 +231,14 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension ::I18n.locale = lang localized_page_id = ::I18n.t("paths.#{page_id}", default: page_id, fallback: []) - localized_path = "" + partially_localized_path = "" File.dirname(path).split('/').each do |path_sub| next if path_sub == "" - localized_path = "#{localized_path}/#{(::I18n.t("paths.#{path_sub}", default: path_sub).to_s)}" + partially_localized_path = "#{partially_localized_path}/#{(::I18n.t("paths.#{path_sub}", default: path_sub).to_s)}" end - path = "#{localized_path}/#{File.basename(path)}" + path = "#{partially_localized_path}/#{File.basename(path)}" prefix = if (options[:mount_at_root] == lang) || (options[:mount_at_root].nil? && langs[0] == lang) '/'