Merge pull request #1057 from paulcpederson/master

File Name Extension Localization Bug Fix
This commit is contained in:
Thomas Reynolds 2013-11-18 16:39:00 -08:00
commit 319cf938bd
9 changed files with 45 additions and 32 deletions

View file

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

View file

@ -22,6 +22,16 @@ 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"

View file

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