mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Attempt to allow i18n to override partial lookup. #1333
This commit is contained in:
parent
15d6210df8
commit
2f545cefbe
18 changed files with 120 additions and 35 deletions
23
middleman-core/features/i18n_partials.feature
Normal file
23
middleman-core/features/i18n_partials.feature
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
Feature: i18n Partials
|
||||||
|
|
||||||
|
Scenario: Running localize with the default config
|
||||||
|
Given a fixture app "i18n-test-app"
|
||||||
|
And a file named "config.rb" with:
|
||||||
|
"""
|
||||||
|
activate :i18n
|
||||||
|
"""
|
||||||
|
Given the Server is running at "i18n-test-app"
|
||||||
|
When I go to "/partials/index.html"
|
||||||
|
Then I should see "Country: USA"
|
||||||
|
Then I should see "State: District of Columbia"
|
||||||
|
Then I should see "Greeting: Hello"
|
||||||
|
Then I should see "Site: Locale Site"
|
||||||
|
Then I should see "Flag: stars"
|
||||||
|
Then I should see "President: obama"
|
||||||
|
When I go to "/es/partials/index.html"
|
||||||
|
Then I should see "Country: Mexico"
|
||||||
|
Then I should see "State: Distrito Federal"
|
||||||
|
Then I should see "Greeting: Hola"
|
||||||
|
Then I should see "Site: Locale Site"
|
||||||
|
Then I should see "Flag: bars"
|
||||||
|
Then I should see "President: nieto"
|
|
@ -49,3 +49,9 @@ Feature: Provide Sane Defaults for Partial Behavior
|
||||||
Given the Server is running at "partials-app"
|
Given the Server is running at "partials-app"
|
||||||
When I go to "/svg.html"
|
When I go to "/svg.html"
|
||||||
Then I should see "<svg"
|
Then I should see "<svg"
|
||||||
|
When I go to "/static_underscore.html"
|
||||||
|
Then I should see "<p>Hello World</p>"
|
||||||
|
When I go to "/code_snippet.html"
|
||||||
|
Then I should see "File Not Found"
|
||||||
|
When I go to "/_code_snippet.html"
|
||||||
|
Then I should see "File Not Found"
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
USA
|
|
@ -0,0 +1 @@
|
||||||
|
Mexico
|
1
middleman-core/fixtures/i18n-test-app/source/_site.erb
Normal file
1
middleman-core/fixtures/i18n-test-app/source/_site.erb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Locale Site
|
|
@ -0,0 +1 @@
|
||||||
|
obama
|
|
@ -0,0 +1 @@
|
||||||
|
nieto
|
|
@ -0,0 +1 @@
|
||||||
|
District of Columbia
|
|
@ -0,0 +1 @@
|
||||||
|
Distrito Federal
|
|
@ -0,0 +1 @@
|
||||||
|
stars
|
|
@ -0,0 +1 @@
|
||||||
|
bars
|
|
@ -0,0 +1 @@
|
||||||
|
Hello
|
|
@ -0,0 +1 @@
|
||||||
|
Hola
|
|
@ -0,0 +1,6 @@
|
||||||
|
Site: <%= partial :site %>
|
||||||
|
Country: <%= partial :country %>
|
||||||
|
Greeting: <%= partial :greeting %>
|
||||||
|
State: <%= partial :state %>
|
||||||
|
Flag: <%= partial "images/flag.svg" %>
|
||||||
|
President: <%= partial "images/president.svg" %>
|
|
@ -0,0 +1 @@
|
||||||
|
<p>Hello World</p>
|
|
@ -0,0 +1 @@
|
||||||
|
<%= partial "code_snippet.html" %>
|
|
@ -183,12 +183,33 @@ module Middleman
|
||||||
# @param [Hash] options
|
# @param [Hash] options
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def render(_, data, options={}, &block)
|
def render(_, data, options={}, &block)
|
||||||
data = data.to_s
|
partial_name = data.to_s
|
||||||
|
found_partial = locate_partial(partial_name, false) || locate_partial(partial_name, true)
|
||||||
|
|
||||||
|
# Look in the partials_dir for the partial with the current engine
|
||||||
|
unless found_partial
|
||||||
|
partials_path = File.join(config[:partials_dir], partial_name)
|
||||||
|
found_partial = locate_partial(partials_path, false) || locate_partial(partials_path, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
raise ::Middleman::CoreExtensions::Rendering::TemplateNotFound, "Could not locate partial: #{data}" unless found_partial
|
||||||
|
|
||||||
locals = options[:locals]
|
locals = options[:locals]
|
||||||
|
|
||||||
found_partial = false
|
if ::Tilt[found_partial]
|
||||||
resolve_opts = { try_without_underscore: true, try_static: true }
|
# Render the partial if found, otherwide throw exception
|
||||||
|
render_individual_file(found_partial, locals, options, self, &block)
|
||||||
|
else
|
||||||
|
File.read(found_partial)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Partial locator.
|
||||||
|
#
|
||||||
|
# @param [String] partial_name
|
||||||
|
# @return [String]
|
||||||
|
def locate_partial(partial_name, try_static=true)
|
||||||
|
resolve_opts = { try_without_underscore: true, try_static: try_static }
|
||||||
|
|
||||||
# If the path is known to the sitemap
|
# If the path is known to the sitemap
|
||||||
if resource = sitemap.find_resource_by_path(current_path)
|
if resource = sitemap.find_resource_by_path(current_path)
|
||||||
|
@ -196,26 +217,11 @@ module Middleman
|
||||||
resolve_opts[:preferred_engine] = File.extname(resource.source_file)[1..-1].to_sym
|
resolve_opts[:preferred_engine] = File.extname(resource.source_file)[1..-1].to_sym
|
||||||
|
|
||||||
# Look for partials relative to the current path
|
# Look for partials relative to the current path
|
||||||
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), data)
|
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(source_dir)}/?}, ''), partial_name)
|
||||||
|
|
||||||
found_partial = resolve_template(relative_dir, resolve_opts)
|
resolve_template(relative_dir, resolve_opts) || resolve_template(partial_name, resolve_opts)
|
||||||
end
|
|
||||||
|
|
||||||
# Look in the partials_dir for the partial with the current engine
|
|
||||||
unless found_partial
|
|
||||||
partials_path = File.join(config[:partials_dir], data)
|
|
||||||
found_partial = resolve_template(partials_path, resolve_opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
raise ::Middleman::CoreExtensions::Rendering::TemplateNotFound, "Could not locate partial: #{data}" unless found_partial
|
|
||||||
|
|
||||||
r = sitemap.find_resource_by_path(sitemap.file_to_path(found_partial))
|
|
||||||
|
|
||||||
if r && !r.template?
|
|
||||||
File.read(r.source_file)
|
|
||||||
else
|
else
|
||||||
# Render the partial if found, otherwide throw exception
|
resolve_template(partial_name, resolve_opts)
|
||||||
render_individual_file(found_partial, locals, options, self, &block)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -259,7 +265,7 @@ module Middleman
|
||||||
# Overwrite with frontmatter options
|
# Overwrite with frontmatter options
|
||||||
options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]
|
options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]
|
||||||
|
|
||||||
template_class = Tilt[path]
|
template_class = ::Tilt[path]
|
||||||
# Allow hooks to manipulate the template before render
|
# Allow hooks to manipulate the template before render
|
||||||
self.class.callbacks_for_hook(:before_render).each do |callback|
|
self.class.callbacks_for_hook(:before_render).each do |callback|
|
||||||
# Uber::Options::Value doesn't respond to call
|
# Uber::Options::Value doesn't respond to call
|
||||||
|
@ -443,23 +449,27 @@ module Middleman
|
||||||
relative_path = Util.strip_leading_slash(request_path)
|
relative_path = Util.strip_leading_slash(request_path)
|
||||||
on_disk_path = File.expand_path(relative_path, source_dir)
|
on_disk_path = File.expand_path(relative_path, source_dir)
|
||||||
|
|
||||||
# By default, any engine will do
|
preferred_engines = if options[:try_static]
|
||||||
preferred_engines = ['*']
|
[nil]
|
||||||
preferred_engines << nil if options[:try_static]
|
else
|
||||||
|
possible_engines = ['*'] # By default, any engine will do
|
||||||
|
|
||||||
# If we're specifically looking for a preferred engine
|
# If we're specifically looking for a preferred engine
|
||||||
if options.key?(:preferred_engine)
|
if options.key?(:preferred_engine)
|
||||||
extension_class = ::Tilt[options[:preferred_engine]]
|
extension_class = ::Tilt[options[:preferred_engine]]
|
||||||
|
|
||||||
# Get a list of extensions for a preferred engine
|
# Get a list of extensions for a preferred engine
|
||||||
matched_exts = ::Tilt.mappings.select do |_, engines|
|
matched_exts = ::Tilt.mappings.select do |_, engines|
|
||||||
engines.include? extension_class
|
engines.include? extension_class
|
||||||
end.keys
|
end.keys
|
||||||
|
|
||||||
# Prefer to look for the matched extensions
|
# Prefer to look for the matched extensions
|
||||||
unless matched_exts.empty?
|
unless matched_exts.empty?
|
||||||
preferred_engines.unshift('{' + matched_exts.join(',') + '}')
|
possible_engines.unshift('{' + matched_exts.join(',') + '}')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
possible_engines
|
||||||
end
|
end
|
||||||
|
|
||||||
search_paths = preferred_engines.flat_map do |preferred_engine|
|
search_paths = preferred_engines.flat_map do |preferred_engine|
|
||||||
|
|
|
@ -207,5 +207,32 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
def langs
|
def langs
|
||||||
extensions[:i18n].langs
|
extensions[:i18n].langs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def locate_partial(partial_name, try_static=false)
|
||||||
|
locals_dir = extensions[:i18n].options[:templates_dir]
|
||||||
|
|
||||||
|
# Try /localizable
|
||||||
|
partials_path = File.join(locals_dir, partial_name)
|
||||||
|
|
||||||
|
lang_suffix = current_resource.metadata[:locals] && current_resource.metadata[:locals][:lang]
|
||||||
|
|
||||||
|
extname = File.extname(partial_name)
|
||||||
|
maybe_static = extname.length > 0
|
||||||
|
suffixed_partial_name = if maybe_static
|
||||||
|
partial_name.sub(extname, ".#{lang_suffix}#{extname}")
|
||||||
|
else
|
||||||
|
"#{partial_name}.#{lang_suffix}"
|
||||||
|
end
|
||||||
|
|
||||||
|
if lang_suffix
|
||||||
|
super(suffixed_partial_name, maybe_static) ||
|
||||||
|
super(File.join(locals_dir, suffixed_partial_name), maybe_static) ||
|
||||||
|
super(partials_path, try_static) ||
|
||||||
|
super
|
||||||
|
else
|
||||||
|
super(partials_path, try_static) ||
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue