Fix new link_to i18n w.r.t. index pages. For #850

This commit is contained in:
Thomas Reynolds 2015-06-01 11:51:27 -07:00
parent 68adbfeb2a
commit 5e30ef98a1
2 changed files with 37 additions and 7 deletions

View file

@ -11,6 +11,7 @@ Feature: i18n Paths
--- ---
en: en:
msg: Hello msg: Hello
home: Home
""" """
And a file named "locales/es.yml" with: And a file named "locales/es.yml" with:
""" """
@ -19,10 +20,21 @@ Feature: i18n Paths
paths: paths:
hello: "hola" hello: "hola"
msg: 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: And a file named "source/localizable/hello.html.erb" with:
""" """
Page: <%= t(:msg) %> 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 %><span>Home: Current Block</span><% end %>
<% link_to "/index.html", title: "Other Home", locale: ::I18n.locale == :en ? :es : :en do %><span>Home: Other Block</span><% end %>
<% data.pages.each_with_index do |p, i| %> <% data.pages.each_with_index do |p, i| %>
<%= link_to "Current #{p}", "/#{p}", class: 'current' %> <%= link_to "Current #{p}", "/#{p}", class: 'current' %>
<%= link_to "Other #{p}", "/#{p}", title: "Other #{p}", locale: ::I18n.locale == :en ? :es : :en %> <%= 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: And a file named "config.rb" with:
""" """
activate :i18n activate :i18n, mount_at_root: :en
""" """
Given the Server is running at "empty-app" Given the Server is running at "empty-app"
When I go to "/hello.html" When I go to "/hello.html"
Then I should see "Page: Hello" Then I should see "Page: Hello"
Then I should see '<a class="current" href="/index.html">Current Home</a>'
Then I should see '<a title="Other Home" href="/es/index.html">Other Home</a>'
Then I should see '<a class="current" href="/index.html"><span>Home: Current Block</span></a>'
Then I should see '<a title="Other Home" href="/es/index.html"><span>Home: Other Block</span></a>'
Then I should see '<a class="current" href="/hello.html">Current hello.html</a>' Then I should see '<a class="current" href="/hello.html">Current hello.html</a>'
Then I should see '<a title="Other hello.html" href="/es/hola.html">Other hello.html</a>' Then I should see '<a title="Other hello.html" href="/es/hola.html">Other hello.html</a>'
Then I should see '<a class="current" href="/hello.html"><span>Current Block</span></a>' Then I should see '<a class="current" href="/hello.html"><span>Current Block</span></a>'
Then I should see '<a title="Other hello.html" href="/es/hola.html"><span>Other Block</span></a>' Then I should see '<a title="Other hello.html" href="/es/hola.html"><span>Other Block</span></a>'
When I go to "/es/hola.html" When I go to "/es/hola.html"
Then I should see "Page: Hola" Then I should see "Page: Hola"
Then I should see '<a class="current" href="/es/index.html">Current Home</a>'
Then I should see '<a title="Other Home" href="/index.html">Other Home</a>'
Then I should see '<a class="current" href="/es/index.html"><span>Home: Current Block</span></a>'
Then I should see '<a title="Other Home" href="/index.html"><span>Home: Other Block</span></a>'
Then I should see '<a class="current" href="/es/hola.html">Current hello.html</a>' Then I should see '<a class="current" href="/es/hola.html">Current hello.html</a>'
Then I should see '<a title="Other hello.html" href="/hello.html">Other hello.html</a>' Then I should see '<a title="Other hello.html" href="/hello.html">Other hello.html</a>'
Then I should see '<a class="current" href="/es/hola.html"><span>Current Block</span></a>' Then I should see '<a class="current" href="/es/hola.html"><span>Current Block</span></a>'
@ -93,7 +114,7 @@ Feature: i18n Paths
""" """
And a file named "config.rb" with: And a file named "config.rb" with:
""" """
activate :i18n activate :i18n, mount_at_root: :en
""" """
Given the Server is running at "empty-app" Given the Server is running at "empty-app"
When I go to "/hello.html" When I go to "/hello.html"

View file

@ -64,7 +64,13 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
locale = options.delete(:locale) || ::I18n.locale locale = options.delete(:locale) || ::I18n.locale
href = super(path_or_resource, options) 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
end end
@ -116,7 +122,10 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
def localized_path(path, lang) 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 end
private private
@ -222,14 +231,14 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
::I18n.locale = lang ::I18n.locale = lang
localized_page_id = ::I18n.t("paths.#{page_id}", default: page_id, fallback: []) 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| File.dirname(path).split('/').each do |path_sub|
next if 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 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) prefix = if (options[:mount_at_root] == lang) || (options[:mount_at_root].nil? && langs[0] == lang)
'/' '/'