I18n: Add support for localized files (fixes #816)
This commit is contained in:
parent
23b6efbb06
commit
1345ae6e90
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
es:
|
||||
paths:
|
||||
hello: "hola"
|
||||
morning: "manana"
|
||||
|
||||
greetings: "Como Esta?"
|
||||
hi: "Hola"
|
1
middleman-more/fixtures/i18n-test-app/source/CNAME
Normal file
1
middleman-more/fixtures/i18n-test-app/source/CNAME
Normal file
|
@ -0,0 +1 @@
|
|||
test.github.com
|
|
@ -0,0 +1 @@
|
|||
Good morning
|
|
@ -0,0 +1 @@
|
|||
Buenos días
|
|
@ -0,0 +1 @@
|
|||
hunter2
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue