I18n: Add support for localized files (fixes #816)

This commit is contained in:
Jonathan Allard 2013-03-20 16:46:20 -04:00 committed by Ben Hollis
parent 23b6efbb06
commit 1345ae6e90
8 changed files with 80 additions and 33 deletions

View file

@ -12,14 +12,22 @@ Feature: i18n Builder
Then the following files should exist: Then the following files should exist:
| index.html | | index.html |
| hello.html | | hello.html |
| morning.html |
| es/index.html | | es/index.html |
| es/hola.html | | es/hola.html |
| es/manana.html |
| CNAME |
| password.txt |
Then the following files should not exist: Then the following files should not exist:
| en/index.html | | 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 "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/index.html" should contain "Como Esta?"
And the file "es/hola.html" should contain "Hola World" 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 Scenario: Running localize with the alt path config
Given a fixture app "i18n-test-app" Given a fixture app "i18n-test-app"

View file

@ -12,12 +12,18 @@ Feature: i18n Preview
Then I should see "Howdy" Then I should see "Howdy"
When I go to "/hello.html" When I go to "/hello.html"
Then I should see "Hello World" 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" When I go to "/en/index.html"
Then I should see "File Not Found" 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" When I go to "/es/index.html"
Then I should see "Como Esta?" Then I should see "Como Esta?"
When I go to "/es/hola.html" When I go to "/es/hola.html"
Then I should see "Hola World" 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 Scenario: A template changes i18n during preview
Given a fixture app "i18n-test-app" Given a fixture app "i18n-test-app"
@ -119,8 +125,14 @@ Feature: i18n Preview
Then I should see "Como Esta?" Then I should see "Como Esta?"
When I go to "/hola.html" When I go to "/hola.html"
Then I should see "Hola World" 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" When I go to "/hello.html"
Then I should see "File Not Found" 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" When I go to "/es/index.html"
Then I should see "File Not Found" Then I should see "File Not Found"
When I go to "/es/hola.html" When I go to "/es/hola.html"

View file

@ -2,6 +2,7 @@
es: es:
paths: paths:
hello: "hola" hello: "hola"
morning: "manana"
greetings: "Como Esta?" greetings: "Como Esta?"
hi: "Hola" hi: "Hola"

View file

@ -0,0 +1 @@
test.github.com

View file

@ -0,0 +1 @@
Good morning

View file

@ -0,0 +1 @@
Buenos días

View file

@ -0,0 +1 @@
hunter2

View file

@ -17,6 +17,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations' 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 end
def after_configuration def after_configuration
@ -108,43 +111,62 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
new_resources = [] new_resources = []
resources.each do |resource| 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.each do |lang|
old_locale = ::I18n.locale new_resources << build_resource(path, resource.path, page_id, lang)
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)
end end
elsif lang, path, basename = parse_locale_extension(resource.path)
path = ::Middleman::Util.normalize_path( new_resources << build_resource(path, resource.path, page_id, lang)
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
end end
::I18n.locale = old_locale
end end
resources + new_resources resources + new_resources
end 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 end