localization by filename extension

This commit is contained in:
Paul C Pederson 2013-10-28 16:42:08 -07:00
parent f136af2aad
commit 56343c84ed
8 changed files with 44 additions and 32 deletions

View file

@ -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:

View file

@ -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"

View file

@ -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