Support relative urls in i18n links. For #850

v3-stable
Thomas Reynolds 2015-06-01 13:49:14 -07:00
parent 5e30ef98a1
commit ca8655744a
2 changed files with 73 additions and 2 deletions

View File

@ -44,6 +44,7 @@ Feature: i18n Paths
"""
And a file named "config.rb" with:
"""
set :strip_index_file, false
activate :i18n, mount_at_root: :en
"""
Given the Server is running at "empty-app"
@ -63,12 +64,75 @@ Feature: i18n Paths
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 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 title="Other hello.html" href="/hello.html"><span>Other Block</span></a>'
Scenario: link_to is i18n aware and supports relative_links
Given a fixture app "empty-app"
And a file named "locales/en.yml" with:
"""
---
en:
msg: Hello
home: Home
"""
And a file named "locales/es.yml" with:
"""
---
es:
paths:
hello: "hola"
msg: Hola
home: Casa
"""
And a file named "source/localizable/index.html.erb" with:
"""
Page: <%= t(:home) %>
"""
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 %><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 %>
<%= link_to "Current hello.html", "/hello.html", class: 'current' %>
<%= link_to "Other hello.html", "/hello.html", title: "Other hello.html", locale: ::I18n.locale == :en ? :es : :en %>
<% link_to "/hello.html", class: 'current' do %><span>Current Block</span><% end %>
<% link_to "/hello.html", title: "Other hello.html", locale: ::I18n.locale == :en ? :es : :en do %><span>Other Block</span><% end %>
"""
And a file named "config.rb" with:
"""
set :relative_links, true
set :strip_index_file, false
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 '<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 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 title="Other hello.html" href="es/hola.html"><span>Other Block</span></a>'
When I go to "/es/hola.html"
Then I should see "Page: Hola"
Then I should see '<a class="current" href="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="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="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 class="current" href="hola.html"><span>Current Block</span></a>'
Then I should see '<a title="Other hello.html" href="../hello.html"><span>Other Block</span></a>'
Scenario: url_for is i18n aware
Given a fixture app "empty-app"
And a file named "data/pages.yml" with:

View File

@ -63,14 +63,21 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
def url_for(path_or_resource, options={})
locale = options.delete(:locale) || ::I18n.locale
should_relativize = options.key?(:relative) ? options[:relative] : config[:relative_links]
options[:relative] = false
href = super(path_or_resource, options)
if result = extensions[:i18n].localized_path(href, locale)
final_path = if result = extensions[:i18n].localized_path(href, locale)
result
else
# Should we log the missing file?
href
end
options[:relative] = should_relativize
super(final_path, options)
end
end