1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

I18n: Keep fragment and query in url_for (#2062)

* Add Cucumber Feature for url_for with query and fragment

* Keep query and fragment when i18n-ing a path
This commit is contained in:
Julian Schneider 2017-04-28 04:24:04 +02:00 committed by Thomas Reynolds
parent 448e179542
commit fde9e8c04a
2 changed files with 16 additions and 4 deletions

View file

@ -176,6 +176,7 @@ Feature: i18n Paths
es:
paths:
hello: "hola"
form: "formulario"
msg: Hola
"""
And a file named "source/localizable/hello.html.erb" with:
@ -186,6 +187,10 @@ Feature: i18n Paths
Other: <%= url_for "/#{p}", locale: ::I18n.locale == :en ? :es : :en %>
<% end %>
"""
And a file named "source/localizable/form.html.erb" with:
"""
Other: <%= url_for "/form.html", query: { foo: 'bar' }, fragment: "deep", locale: ::I18n.locale == :en ? :es : :en %>
"""
And a file named "source/localizable/article.html.erb" with:
"""
Page Lang: Default
@ -213,6 +218,10 @@ Feature: i18n Paths
Then I should see "Page: Hola"
Then I should see 'Current: /es/hola.html'
Then I should see 'Other: /hello.html'
When I go to "/form.html"
Then I should see 'Other: /es/formulario.html?foo=bar#deep'
When I go to "/es/formulario.html"
Then I should see 'Other: /form.html?foo=bar#deep'
When I go to "/article.html"
Then I should see "Page Lang: Default"
Then I should see 'Current: /article.html'

View file

@ -200,10 +200,13 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
Contract String, Symbol => Maybe[String]
def localized_path(path, locale)
lookup_path = path.dup
lookup_path << app.config[:index_file] if lookup_path.end_with?('/')
@lookup[lookup_path] && @lookup[lookup_path][locale]
begin
lookup = ::Middleman::Util.parse_uri(path)
lookup.path << app.config[:index_file] if lookup.path && lookup.path.end_with?('/')
lookup.to_s if @lookup[lookup.path] && lookup.path = @lookup[lookup.path][locale]
rescue ::Addressable::URI::InvalidURIError
nil
end
end
Contract Symbol => String