diff --git a/middleman-more/features/i18n_builder.feature b/middleman-more/features/i18n_builder.feature index d60338cf..4905569a 100644 --- a/middleman-more/features/i18n_builder.feature +++ b/middleman-more/features/i18n_builder.feature @@ -12,14 +12,22 @@ Feature: i18n Builder Then the following files should exist: | index.html | | hello.html | + | morning.html | | es/index.html | | es/hola.html | + | es/manana.html | + | CNAME | + | password.txt | Then the following files should not exist: | en/index.html | 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 "es/index.html" should contain "Como Esta?" And the file "es/hola.html" should contain "Hola World" + And the file "es/manana.html" should contain "Buenos días" + 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" diff --git a/middleman-more/features/i18n_preview.feature b/middleman-more/features/i18n_preview.feature index ebcf5f5e..c34b7708 100644 --- a/middleman-more/features/i18n_preview.feature +++ b/middleman-more/features/i18n_preview.feature @@ -12,12 +12,18 @@ Feature: i18n Preview Then I should see "Howdy" When I go to "/hello.html" Then I should see "Hello World" + When I go to "/morning.html" + Then I should see "Good morning" When I go to "/en/index.html" Then I should see "File Not Found" + When I go to "/en/morning.html" + Then I should see "File Not Found" When I go to "/es/index.html" Then I should see "Como Esta?" When I go to "/es/hola.html" Then I should see "Hola World" + When I go to "/es/manana.html" + Then I should see "Buenos días" Scenario: A template changes i18n during preview Given a fixture app "i18n-test-app" @@ -119,8 +125,14 @@ Feature: i18n Preview Then I should see "Como Esta?" When I go to "/hola.html" Then I should see "Hola World" + When I go to "/manana.html" + Then I should see "Buenos días" When I go to "/hello.html" Then I should see "File Not Found" + When I go to "/en/morning.html" + Then I should see "Good morning" + When I go to "/es/manana.html" + Then I should see "File Not Found" When I go to "/es/index.html" Then I should see "File Not Found" When I go to "/es/hola.html" diff --git a/middleman-more/fixtures/i18n-test-app/locales/es.yml b/middleman-more/fixtures/i18n-test-app/locales/es.yml index 06756f3d..11b8a327 100644 --- a/middleman-more/fixtures/i18n-test-app/locales/es.yml +++ b/middleman-more/fixtures/i18n-test-app/locales/es.yml @@ -2,6 +2,7 @@ es: paths: hello: "hola" + morning: "manana" greetings: "Como Esta?" - hi: "Hola" \ No newline at end of file + hi: "Hola" diff --git a/middleman-more/fixtures/i18n-test-app/source/CNAME b/middleman-more/fixtures/i18n-test-app/source/CNAME new file mode 100644 index 00000000..0aca0c56 --- /dev/null +++ b/middleman-more/fixtures/i18n-test-app/source/CNAME @@ -0,0 +1 @@ +test.github.com diff --git a/middleman-more/fixtures/i18n-test-app/source/morning.en.html.erb b/middleman-more/fixtures/i18n-test-app/source/morning.en.html.erb new file mode 100644 index 00000000..9fc8e015 --- /dev/null +++ b/middleman-more/fixtures/i18n-test-app/source/morning.en.html.erb @@ -0,0 +1 @@ +Good morning diff --git a/middleman-more/fixtures/i18n-test-app/source/morning.es.html.erb b/middleman-more/fixtures/i18n-test-app/source/morning.es.html.erb new file mode 100644 index 00000000..30d65605 --- /dev/null +++ b/middleman-more/fixtures/i18n-test-app/source/morning.es.html.erb @@ -0,0 +1 @@ +Buenos días diff --git a/middleman-more/fixtures/i18n-test-app/source/password.txt b/middleman-more/fixtures/i18n-test-app/source/password.txt new file mode 100644 index 00000000..9f592eb7 --- /dev/null +++ b/middleman-more/fixtures/i18n-test-app/source/password.txt @@ -0,0 +1 @@ +hunter2 diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index a1dbe1c6..774eb857 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -17,6 +17,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations' + + # Ruby 2.0 beep beep + ::Middleman::Sitemap::Store.send :prepend, StoreInstanceMethods end def after_configuration @@ -108,43 +111,62 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension new_resources = [] resources.each do |resource| - next unless File.fnmatch(File.join(options[:templates_dir], "**"), resource.path) + if File.fnmatch?(File.join(options[:templates_dir], "**"), resource.path) + page_id = File.basename(resource.path, File.extname(resource.path)) - page_id = File.basename(resource.path, File.extname(resource.path)) - - old_locale = ::I18n.locale - langs.map do |lang| - ::I18n.locale = lang - - localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => []) - path = resource.path.sub(options[:templates_dir], "") - - # Build lang path - if @mount_at_root == lang - prefix = "/" - else - replacement = options[:lang_map].has_key?(lang) ? options[:lang_map][lang] : lang - prefix = options[:path].sub(":locale", replacement.to_s) + old_locale = ::I18n.locale + langs.each do |lang| + new_resources << build_resource(path, resource.path, page_id, lang) end - - path = ::Middleman::Util.normalize_path( - File.join(prefix, path.sub(page_id, localized_page_id)) - ) - - @_localization_data[path] = [lang, path, localized_page_id] - - p = ::Middleman::Sitemap::Resource.new( - app.sitemap, - path - ) - p.proxy_to(resource.path) - - new_resources << p + elsif lang, path, basename = parse_locale_extension(resource.path) + new_resources << build_resource(path, resource.path, page_id, lang) end - ::I18n.locale = old_locale - end resources + new_resources end + + private + + # Parse locale extension filename + # @return [Hash] with :basename, :locale, and :path + # will return +nil+ if no locale extension + def parse_locale_extension(path) + path.match(/([^.]*)\.([^.]*)/) do |m| + locale = m[2].to_sym + path = path.sub("."+m[2], "") + basename = File.basename(m[1]) + langs.include?(locale) ? [locale, path, basename] : nil + end + end + + def build_resource(path, source_path, page_id, lang) + localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => []) + path = resource.path.sub(options[:templates_dir], "") + + # Build lang path + if @mount_at_root == lang + prefix = "/" + else + replacement = options[:lang_map].fetch(lang, lang) + prefix = options[:path].sub(":locale", replacement.to_s) + end + + # Localize page id + old_locale = ::I18n.locale + ::I18n.locale = lang + + path = ::Middleman::Util.normalize_path( + File.join(prefix, path.sub(page_id, localized_page_id)) + ) + + @_localization_data[path] = [lang, path, localized_page_id] + + p = ::Middleman::Sitemap::Resource.new(app.sitemap, path) + p.proxy_to(resource.path) + + ::I18n.locale = old_locale + + p + end end