Empower link_to in a i18n context
This commit is contained in:
parent
72916dec13
commit
9de1f16f3b
43
middleman-core/features/i18n_link_to.feature
Normal file
43
middleman-core/features/i18n_link_to.feature
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
Feature: i18n Links
|
||||||
|
|
||||||
|
Scenario: A template changes i18n during preview
|
||||||
|
Given a fixture app "empty-app"
|
||||||
|
And a file named "data/pages.yml" with:
|
||||||
|
"""
|
||||||
|
- hello.html
|
||||||
|
"""
|
||||||
|
And a file named "locales/en.yml" with:
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
en:
|
||||||
|
msg: Hello
|
||||||
|
"""
|
||||||
|
And a file named "locales/es.yml" with:
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
es:
|
||||||
|
paths:
|
||||||
|
hello: "hola"
|
||||||
|
msg: Hola
|
||||||
|
"""
|
||||||
|
And a file named "source/localizable/hello.html.erb" with:
|
||||||
|
"""
|
||||||
|
Page: <%= t(:msg) %>
|
||||||
|
<% data.pages.each_with_index do |p, i| %>
|
||||||
|
<%= link_to "Current #{p}", "/#{p}" %>
|
||||||
|
<%= link_to "Other #{p}", "/#{p}", ::I18n.locale == :en ? :es : :en %>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
And a file named "config.rb" with:
|
||||||
|
"""
|
||||||
|
activate :i18n
|
||||||
|
"""
|
||||||
|
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 href="/hello.html">Current hello.html</a>'
|
||||||
|
Then I should see '<a href="/es/hola.html">Other hello.html</a>'
|
||||||
|
When I go to "/es/hola.html"
|
||||||
|
Then I should see "Page: Hola"
|
||||||
|
Then I should see '<a href="/es/hola.html">Current hello.html</a>'
|
||||||
|
Then I should see '<a href="/hello.html">Other hello.html</a>'
|
|
@ -7,9 +7,13 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
option :mount_at_root, nil, 'Mount a specific language at the root of the site'
|
option :mount_at_root, nil, 'Mount a specific language at the root of the site'
|
||||||
option :data, 'locales', 'The directory holding your locale configurations'
|
option :data, 'locales', 'The directory holding your locale configurations'
|
||||||
|
|
||||||
|
attr_reader :lookup
|
||||||
|
|
||||||
def initialize(app, options_hash={}, &block)
|
def initialize(app, options_hash={}, &block)
|
||||||
super
|
super
|
||||||
|
|
||||||
|
@lookup = {}
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
# If :directory_indexes is already active,
|
# If :directory_indexes is already active,
|
||||||
# throw a warning explaining the bug and telling the use
|
# throw a warning explaining the bug and telling the use
|
||||||
|
@ -27,9 +31,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_configuration
|
def after_configuration
|
||||||
app.files.reload_path(app.config[:locals_dir] || options[:data])
|
app.files.reload_path(app.config[:locales_dir] || options[:data])
|
||||||
|
|
||||||
@locales_glob = File.join(app.config[:locals_dir] || options[:data], '**', '*.{rb,yml,yaml}')
|
@locales_glob = File.join(app.config[:locales_dir] || options[:data], '**', '*.{rb,yml,yaml}')
|
||||||
@locales_regex = convert_glob_to_regex(@locales_glob)
|
@locales_regex = convert_glob_to_regex(@locales_glob)
|
||||||
|
|
||||||
@maps = {}
|
@maps = {}
|
||||||
|
@ -53,6 +57,11 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
def t(*args)
|
def t(*args)
|
||||||
::I18n.t(*args)
|
::I18n.t(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def link_to(text, target, lang=::I18n.locale)
|
||||||
|
url = extensions[:i18n].localized_path(target, lang)
|
||||||
|
url ? super(text, url) : super(text, target)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
delegate :logger, to: :app
|
delegate :logger, to: :app
|
||||||
|
@ -85,7 +94,17 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
resources + new_resources
|
@lookup = new_resources.each_with_object({}) do |desc, sum|
|
||||||
|
abs_path = desc.source_path.sub(options[:templates_dir], '')
|
||||||
|
sum[abs_path] ||= {}
|
||||||
|
sum[abs_path][desc.lang] = '/' + desc.path
|
||||||
|
end
|
||||||
|
|
||||||
|
resources + new_resources.map { |r| r.to_resource(app) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def localized_path(path, lang)
|
||||||
|
@lookup[path] && @lookup[path][lang]
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -173,6 +192,14 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
[lang, path, basename]
|
[lang, path, basename]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
LocalizedPageDescriptor = Struct.new(:path, :source_path, :lang) do
|
||||||
|
def to_resource(app)
|
||||||
|
p = ::Middleman::Sitemap::Resource.new(app.sitemap, path)
|
||||||
|
p.proxy_to(source_path)
|
||||||
|
p
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def build_resource(path, source_path, page_id, lang)
|
def build_resource(path, source_path, page_id, lang)
|
||||||
old_locale = ::I18n.locale
|
old_locale = ::I18n.locale
|
||||||
::I18n.locale = lang
|
::I18n.locale = lang
|
||||||
|
@ -194,11 +221,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
|
|
||||||
@_localization_data[path] = [lang, path, localized_page_id]
|
@_localization_data[path] = [lang, path, localized_page_id]
|
||||||
|
|
||||||
p = ::Middleman::Sitemap::Resource.new(app.sitemap, path)
|
|
||||||
p.proxy_to(source_path)
|
|
||||||
|
|
||||||
::I18n.locale = old_locale
|
::I18n.locale = old_locale
|
||||||
p
|
|
||||||
|
LocalizedPageDescriptor.new(path, source_path, lang)
|
||||||
end
|
end
|
||||||
|
|
||||||
module LocaleHelpers
|
module LocaleHelpers
|
||||||
|
@ -209,10 +234,10 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
end
|
end
|
||||||
|
|
||||||
def locate_partial(partial_name, try_static=false)
|
def locate_partial(partial_name, try_static=false)
|
||||||
locals_dir = extensions[:i18n].options[:templates_dir]
|
locales_dir = extensions[:i18n].options[:templates_dir]
|
||||||
|
|
||||||
# Try /localizable
|
# Try /localizable
|
||||||
partials_path = File.join(locals_dir, partial_name)
|
partials_path = File.join(locales_dir, partial_name)
|
||||||
|
|
||||||
lang_suffix = current_resource.metadata[:locals] && current_resource.metadata[:locals][:lang]
|
lang_suffix = current_resource.metadata[:locals] && current_resource.metadata[:locals][:lang]
|
||||||
|
|
||||||
|
@ -226,7 +251,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
|
|
||||||
if lang_suffix
|
if lang_suffix
|
||||||
super(suffixed_partial_name, maybe_static) ||
|
super(suffixed_partial_name, maybe_static) ||
|
||||||
super(File.join(locals_dir, suffixed_partial_name), maybe_static) ||
|
super(File.join(locales_dir, suffixed_partial_name), maybe_static) ||
|
||||||
super(partials_path, try_static) ||
|
super(partials_path, try_static) ||
|
||||||
super
|
super
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in a new issue