From 2f545cefbe66dd674fa06f4d44aaa97ad5a312bd Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Thu, 24 Jul 2014 11:11:27 -0700 Subject: [PATCH] Attempt to allow i18n to override partial lookup. #1333 --- middleman-core/features/i18n_partials.feature | 23 ++++++ middleman-core/features/partials.feature | 6 ++ .../i18n-test-app/source/_country.en.erb | 1 + .../i18n-test-app/source/_country.es.erb | 1 + .../fixtures/i18n-test-app/source/_site.erb | 1 + .../source/images/president.en.svg | 1 + .../source/images/president.es.svg | 1 + .../source/localizable/_state.en.erb | 1 + .../source/localizable/_state.es.erb | 1 + .../source/localizable/images/flag.en.svg | 1 + .../source/localizable/images/flag.es.svg | 1 + .../localizable/partials/_greeting.en.erb | 1 + .../localizable/partials/_greeting.es.erb | 1 + .../localizable/partials/index.html.erb | 6 ++ .../partials-app/source/_code_snippet.html | 1 + .../source/static_underscore.html.erb | 1 + .../core_extensions/rendering.rb | 80 +++++++++++-------- .../middleman-more/core_extensions/i18n.rb | 27 +++++++ 18 files changed, 120 insertions(+), 35 deletions(-) create mode 100644 middleman-core/features/i18n_partials.feature create mode 100644 middleman-core/fixtures/i18n-test-app/source/_country.en.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/_country.es.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/_site.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/images/president.en.svg create mode 100644 middleman-core/fixtures/i18n-test-app/source/images/president.es.svg create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/_state.en.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/_state.es.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.en.svg create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.es.svg create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.en.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.es.erb create mode 100644 middleman-core/fixtures/i18n-test-app/source/localizable/partials/index.html.erb create mode 100644 middleman-core/fixtures/partials-app/source/_code_snippet.html create mode 100644 middleman-core/fixtures/partials-app/source/static_underscore.html.erb diff --git a/middleman-core/features/i18n_partials.feature b/middleman-core/features/i18n_partials.feature new file mode 100644 index 00000000..f4f947c0 --- /dev/null +++ b/middleman-core/features/i18n_partials.feature @@ -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" \ No newline at end of file diff --git a/middleman-core/features/partials.feature b/middleman-core/features/partials.feature index 76a7e75a..965215a9 100644 --- a/middleman-core/features/partials.feature +++ b/middleman-core/features/partials.feature @@ -49,3 +49,9 @@ Feature: Provide Sane Defaults for Partial Behavior Given the Server is running at "partials-app" When I go to "/svg.html" Then I should see "Hello World

" + 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" diff --git a/middleman-core/fixtures/i18n-test-app/source/_country.en.erb b/middleman-core/fixtures/i18n-test-app/source/_country.en.erb new file mode 100644 index 00000000..e14a77cb --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/_country.en.erb @@ -0,0 +1 @@ +USA \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/_country.es.erb b/middleman-core/fixtures/i18n-test-app/source/_country.es.erb new file mode 100644 index 00000000..1da91630 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/_country.es.erb @@ -0,0 +1 @@ +Mexico \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/_site.erb b/middleman-core/fixtures/i18n-test-app/source/_site.erb new file mode 100644 index 00000000..46964e75 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/_site.erb @@ -0,0 +1 @@ +Locale Site \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/images/president.en.svg b/middleman-core/fixtures/i18n-test-app/source/images/president.en.svg new file mode 100644 index 00000000..9a301b6a --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/images/president.en.svg @@ -0,0 +1 @@ +obama \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/images/president.es.svg b/middleman-core/fixtures/i18n-test-app/source/images/president.es.svg new file mode 100644 index 00000000..443f1666 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/images/president.es.svg @@ -0,0 +1 @@ +nieto \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/_state.en.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/_state.en.erb new file mode 100644 index 00000000..0a766884 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/_state.en.erb @@ -0,0 +1 @@ +District of Columbia \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/_state.es.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/_state.es.erb new file mode 100644 index 00000000..49c5d37e --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/_state.es.erb @@ -0,0 +1 @@ +Distrito Federal \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.en.svg b/middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.en.svg new file mode 100644 index 00000000..118f7d33 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.en.svg @@ -0,0 +1 @@ +stars \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.es.svg b/middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.es.svg new file mode 100644 index 00000000..541204ba --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/images/flag.es.svg @@ -0,0 +1 @@ +bars \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.en.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.en.erb new file mode 100644 index 00000000..5ab2f8a4 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.en.erb @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.es.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.es.erb new file mode 100644 index 00000000..af5a0623 --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/partials/_greeting.es.erb @@ -0,0 +1 @@ +Hola \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/localizable/partials/index.html.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/partials/index.html.erb new file mode 100644 index 00000000..7369572a --- /dev/null +++ b/middleman-core/fixtures/i18n-test-app/source/localizable/partials/index.html.erb @@ -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" %> \ No newline at end of file diff --git a/middleman-core/fixtures/partials-app/source/_code_snippet.html b/middleman-core/fixtures/partials-app/source/_code_snippet.html new file mode 100644 index 00000000..92f9a3de --- /dev/null +++ b/middleman-core/fixtures/partials-app/source/_code_snippet.html @@ -0,0 +1 @@ +

Hello World

\ No newline at end of file diff --git a/middleman-core/fixtures/partials-app/source/static_underscore.html.erb b/middleman-core/fixtures/partials-app/source/static_underscore.html.erb new file mode 100644 index 00000000..911d816d --- /dev/null +++ b/middleman-core/fixtures/partials-app/source/static_underscore.html.erb @@ -0,0 +1 @@ +<%= partial "code_snippet.html" %> \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 074fa017..04000c34 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -183,12 +183,33 @@ module Middleman # @param [Hash] options # @return [String] 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] - found_partial = false - resolve_opts = { try_without_underscore: true, try_static: true } + if ::Tilt[found_partial] + # 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 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 # 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) - 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) + resolve_template(relative_dir, resolve_opts) || resolve_template(partial_name, resolve_opts) else - # Render the partial if found, otherwide throw exception - render_individual_file(found_partial, locals, options, self, &block) + resolve_template(partial_name, resolve_opts) end end @@ -259,7 +265,7 @@ module Middleman # Overwrite with frontmatter 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 self.class.callbacks_for_hook(:before_render).each do |callback| # Uber::Options::Value doesn't respond to call @@ -443,23 +449,27 @@ module Middleman relative_path = Util.strip_leading_slash(request_path) on_disk_path = File.expand_path(relative_path, source_dir) - # By default, any engine will do - preferred_engines = ['*'] - preferred_engines << nil if options[:try_static] + preferred_engines = if options[:try_static] + [nil] + else + possible_engines = ['*'] # By default, any engine will do - # If we're specifically looking for a preferred engine - if options.key?(:preferred_engine) - extension_class = ::Tilt[options[:preferred_engine]] + # If we're specifically looking for a preferred engine + if options.key?(:preferred_engine) + extension_class = ::Tilt[options[:preferred_engine]] - # Get a list of extensions for a preferred engine - matched_exts = ::Tilt.mappings.select do |_, engines| - engines.include? extension_class - end.keys + # Get a list of extensions for a preferred engine + matched_exts = ::Tilt.mappings.select do |_, engines| + engines.include? extension_class + end.keys - # Prefer to look for the matched extensions - unless matched_exts.empty? - preferred_engines.unshift('{' + matched_exts.join(',') + '}') + # Prefer to look for the matched extensions + unless matched_exts.empty? + possible_engines.unshift('{' + matched_exts.join(',') + '}') + end end + + possible_engines end search_paths = preferred_engines.flat_map do |preferred_engine| diff --git a/middleman-core/lib/middleman-more/core_extensions/i18n.rb b/middleman-core/lib/middleman-more/core_extensions/i18n.rb index 0148f944..450edddd 100644 --- a/middleman-core/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-more/core_extensions/i18n.rb @@ -207,5 +207,32 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension def langs extensions[:i18n].langs 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