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

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