From 56343c84ed366ad8a33e8461585d4ee224f162f4 Mon Sep 17 00:00:00 2001 From: Paul C Pederson Date: Mon, 28 Oct 2013 16:42:08 -0700 Subject: [PATCH 1/2] localization by filename extension --- middleman-core/features/i18n_builder.feature | 36 ++++++++++++------- middleman-core/features/i18n_preview.feature | 18 +++++----- .../source/{ => layouts}/layout.erb | 0 .../{ => localizable}/morning.en.html.erb | 0 .../{ => localizable}/morning.es.html.erb | 0 .../source/{ => localizable}/one.en.md | 0 .../source/{ => localizable}/one.es.md | 0 .../middleman-more/core_extensions/i18n.rb | 22 ++++++------ 8 files changed, 44 insertions(+), 32 deletions(-) rename middleman-core/fixtures/i18n-test-app/source/{ => layouts}/layout.erb (100%) rename middleman-core/fixtures/i18n-test-app/source/{ => localizable}/morning.en.html.erb (100%) rename middleman-core/fixtures/i18n-test-app/source/{ => localizable}/morning.es.html.erb (100%) rename middleman-core/fixtures/i18n-test-app/source/{ => localizable}/one.en.md (100%) rename middleman-core/fixtures/i18n-test-app/source/{ => localizable}/one.es.md (100%) diff --git a/middleman-core/features/i18n_builder.feature b/middleman-core/features/i18n_builder.feature index bf92efce..34a802b1 100644 --- a/middleman-core/features/i18n_builder.feature +++ b/middleman-core/features/i18n_builder.feature @@ -1,6 +1,6 @@ Feature: i18n Builder In order to preview localized html - + Scenario: Running localize with the default config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -22,9 +22,19 @@ Feature: i18n Builder | password.txt | Then the following files should not exist: | en/index.html | + | en/manana.html | + | en/hola.html | + | en/una.html | + | es/morning.html | + | es/one.html | + | es/hello.html | + | en/morning.en.html | + | en/morning.es.html | + | morning.en.html | + | morning.es.html | | defaults_en/index.html | | en_defaults/index.html | - And the file "index.html" should contain "Howdy" + And the file "index.html" should contain "Howdy" And the file "hello.html" should contain "Hello World" And the file "morning.html" should contain "Good morning" And the file "one.html" should contain "Only one" @@ -34,7 +44,7 @@ Feature: i18n Builder And the file "es/una.html" should contain "Solamente una" And the file "CNAME" should contain "test.github.com" And the file "password.txt" should contain "hunter2" - + Scenario: Running localize with the alt path config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -50,11 +60,11 @@ Feature: i18n Builder | lang_es/hola.html | Then the following files should not exist: | lang_en/index.html | - And the file "index.html" should contain "Howdy" + And the file "index.html" should contain "Howdy" And the file "hello.html" should contain "Hello World" And the file "lang_es/index.html" should contain "Como Esta?" And the file "lang_es/hola.html" should contain "Hola World" - + Scenario: Running localize with the alt root config Given a fixture app "i18n-alt-root-app" And a file named "config.rb" with: @@ -70,11 +80,11 @@ Feature: i18n Builder | es/hola.html | Then the following files should not exist: | en/index.html | - And the file "index.html" should contain "Howdy" + And the file "index.html" should contain "Howdy" And the file "hello.html" should contain "Hello World" And the file "es/index.html" should contain "Como Esta?" And the file "es/hola.html" should contain "Hola World" - + Scenario: Running localize with the lang map config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -90,11 +100,11 @@ Feature: i18n Builder | spanish/hola.html | Then the following files should not exist: | english/index.html | - And the file "index.html" should contain "Howdy" + And the file "index.html" should contain "Howdy" And the file "hello.html" should contain "Hello World" And the file "spanish/index.html" should contain "Como Esta?" And the file "spanish/hola.html" should contain "Hola World" - + Scenario: Running localize with the no mount config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -111,11 +121,11 @@ Feature: i18n Builder Then the following files should not exist: | index.html | | hello.html | - And the file "en/index.html" should contain "Howdy" + And the file "en/index.html" should contain "Howdy" And the file "en/hello.html" should contain "Hello World" And the file "es/index.html" should contain "Como Esta?" And the file "es/hola.html" should contain "Hola World" - + Scenario: Running localize with the subset config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -131,9 +141,9 @@ Feature: i18n Builder | en/index.html | | es/index.html | | es/hola.html | - And the file "index.html" should contain "Howdy" + And the file "index.html" should contain "Howdy" And the file "hello.html" should contain "Hello World" - + Scenario: Running localize with relative_assets Given a fixture app "i18n-test-app" And a file named "config.rb" with: diff --git a/middleman-core/features/i18n_preview.feature b/middleman-core/features/i18n_preview.feature index 6f059044..93c60dc1 100644 --- a/middleman-core/features/i18n_preview.feature +++ b/middleman-core/features/i18n_preview.feature @@ -1,6 +1,6 @@ Feature: i18n Preview In order to preview localized html - + Scenario: Running localize with the default config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -80,8 +80,8 @@ Feature: i18n Preview Then I should see "Como Esta?" When I go to "/lang_es/hola.html" Then I should see "Hola World" - - + + Scenario: Running localize with the alt root config Given a fixture app "i18n-alt-root-app" And a file named "config.rb" with: @@ -99,7 +99,7 @@ Feature: i18n Preview Then I should see "Como Esta?" When I go to "/es/hola.html" Then I should see "Hola World" - + Scenario: Running localize with the lang map config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -167,8 +167,8 @@ Feature: i18n Preview Then I should see "File Not Found" When I go to "/es/hola.html" Then I should see "File Not Found" - - + + Scenario: Running localize with the no mount config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -188,7 +188,7 @@ Feature: i18n Preview Then I should see "Como Esta?" When I go to "/es/hola.html" Then I should see "Hola World" - + Scenario: Running localize with the subset config Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -206,7 +206,7 @@ Feature: i18n Preview Then I should see "File Not Found" When I go to "/es/hola.html" Then I should see "File Not Found" - + Scenario: Running localize with relative_assets Given a fixture app "i18n-test-app" And a file named "config.rb" with: @@ -249,4 +249,4 @@ Feature: i18n Preview Then I should see "More" When I go to "/es/" Then I should see "Como Esta?" - Then I should see "Mucho" + Then I should see "Mucho" \ No newline at end of file diff --git a/middleman-core/fixtures/i18n-test-app/source/layout.erb b/middleman-core/fixtures/i18n-test-app/source/layouts/layout.erb similarity index 100% rename from middleman-core/fixtures/i18n-test-app/source/layout.erb rename to middleman-core/fixtures/i18n-test-app/source/layouts/layout.erb diff --git a/middleman-core/fixtures/i18n-test-app/source/morning.en.html.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/morning.en.html.erb similarity index 100% rename from middleman-core/fixtures/i18n-test-app/source/morning.en.html.erb rename to middleman-core/fixtures/i18n-test-app/source/localizable/morning.en.html.erb diff --git a/middleman-core/fixtures/i18n-test-app/source/morning.es.html.erb b/middleman-core/fixtures/i18n-test-app/source/localizable/morning.es.html.erb similarity index 100% rename from middleman-core/fixtures/i18n-test-app/source/morning.es.html.erb rename to middleman-core/fixtures/i18n-test-app/source/localizable/morning.es.html.erb diff --git a/middleman-core/fixtures/i18n-test-app/source/one.en.md b/middleman-core/fixtures/i18n-test-app/source/localizable/one.en.md similarity index 100% rename from middleman-core/fixtures/i18n-test-app/source/one.en.md rename to middleman-core/fixtures/i18n-test-app/source/localizable/one.en.md diff --git a/middleman-core/fixtures/i18n-test-app/source/one.es.md b/middleman-core/fixtures/i18n-test-app/source/localizable/one.es.md similarity index 100% rename from middleman-core/fixtures/i18n-test-app/source/one.es.md rename to middleman-core/fixtures/i18n-test-app/source/localizable/one.es.md diff --git a/middleman-core/lib/middleman-more/core_extensions/i18n.rb b/middleman-core/lib/middleman-more/core_extensions/i18n.rb index b16ed02c..1f213d48 100644 --- a/middleman-core/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-more/core_extensions/i18n.rb @@ -60,7 +60,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension def langs @_langs ||= get_known_languages end - + # Update the main sitemap resource list # @return [void] def manipulate_resource_list(resources) @@ -69,17 +69,19 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension new_resources = [] resources.each do |resource| + # Ff it uses file extension localization + if !parse_locale_extension(resource.path).nil? + result = parse_locale_extension(resource.path) + lang, path, page_id = result + new_resources << build_resource(path, resource.path, page_id, lang) # If it's a "localizable template" - if File.fnmatch?(File.join(options[:templates_dir], "**"), resource.path) + elsif File.fnmatch?(File.join(options[:templates_dir], "**"), resource.path) page_id = File.basename(resource.path, File.extname(resource.path)) langs.each do |lang| # Remove folder name path = resource.path.sub(options[:templates_dir], "") new_resources << build_resource(path, resource.path, page_id, lang) end - elsif result = parse_locale_extension(resource.path) - lang, path, page_id = result - new_resources << build_resource(path, resource.path, page_id, lang) end end @@ -95,7 +97,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension ::I18n.reload! end end - + def convert_glob_to_regex(glob) # File.fnmatch doesn't support brackets: {rb,yml,yaml} regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "(rb|ya?ml)") @@ -168,34 +170,34 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension path = path_bits.join('.') basename = File.basename(path_bits[0..-2].join('.')) - [lang, path, basename] end def build_resource(path, source_path, page_id, lang) old_locale = ::I18n.locale ::I18n.locale = lang - localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => []) - prefix = if @mount_at_root == lang + prefix = if (options[:mount_at_root] == lang) || (options[:mount_at_root] == nil && langs[0] == lang) "/" else replacement = options[:lang_map].fetch(lang, lang) options[:path].sub(":locale", replacement.to_s) end + # path needs to be changed if file has a localizable extension. (options[mount_at_root] == lang) path = ::Middleman::Util.normalize_path( File.join(prefix, path.sub(page_id, localized_page_id)) ) + path.gsub!(options[:templates_dir]+"/", "") + @_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 - p end From dd1ba160e1229d6c250750f9c6dca1bec559004d Mon Sep 17 00:00:00 2001 From: Paul C Pederson Date: Fri, 15 Nov 2013 10:34:38 -0800 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01e1d228..f37f167a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ master * Look for assets using url_for before falling back to the "images" directory. #1017 * Do not cache generated redirect index file. #1019 * Make an effort to handle spaces in filenames in a way that url_for can handle. #961 +* Fix localization via filename extension. #1015 3.1.5 ===