Progress towards making localizable pages work
This commit is contained in:
parent
1345ae6e90
commit
d58acae939
|
@ -2,6 +2,7 @@ master
|
|||
===
|
||||
|
||||
* DataStore may now be accessed like a hash with #[] and #has_key?. #880
|
||||
* The i18n extension now supports providing localized templates as separate files, like index.es.html.haml. #816, #823
|
||||
|
||||
3.1.0.beta.2
|
||||
===
|
||||
|
|
|
@ -187,39 +187,7 @@ module Middleman
|
|||
path = remove_templating_extensions(path)
|
||||
|
||||
# If there is no extension, look for one
|
||||
path = find_extension(path, file) if File.extname(path).empty?
|
||||
path
|
||||
end
|
||||
|
||||
# Removes the templating extensions, while keeping the others
|
||||
# @param [String] path
|
||||
# @return [String]
|
||||
def remove_templating_extensions(path)
|
||||
end_of_the_line = false
|
||||
while !end_of_the_line
|
||||
if !::Tilt[path].nil?
|
||||
path = path.sub(File.extname(path), "")
|
||||
else
|
||||
end_of_the_line = true
|
||||
end
|
||||
end
|
||||
|
||||
path
|
||||
end
|
||||
|
||||
# Finds an extension for path according to file's extension
|
||||
# @param [String] path without extension
|
||||
# @param [String] file path with original extensions
|
||||
def find_extension(path, file)
|
||||
input_ext = File.extname(file)
|
||||
|
||||
if !input_ext.empty?
|
||||
input_ext = input_ext.split(".").last.to_sym
|
||||
if @app.template_extensions.has_key?(input_ext)
|
||||
path << ".#{@app.template_extensions[input_ext]}"
|
||||
end
|
||||
end
|
||||
|
||||
path = find_extension(path, file) if File.extname(strip_away_locale(path)).empty?
|
||||
path
|
||||
end
|
||||
|
||||
|
@ -256,6 +224,53 @@ module Middleman
|
|||
@_lookup_by_destination_path = {}
|
||||
}
|
||||
end
|
||||
|
||||
# Removes the templating extensions, while keeping the others
|
||||
# @param [String] path
|
||||
# @return [String]
|
||||
def remove_templating_extensions(path)
|
||||
end_of_the_line = false
|
||||
while !end_of_the_line
|
||||
if !::Tilt[path].nil?
|
||||
path = path.sub(File.extname(path), "")
|
||||
else
|
||||
end_of_the_line = true
|
||||
end
|
||||
end
|
||||
|
||||
path
|
||||
end
|
||||
|
||||
# Remove the locale token from the end of the path
|
||||
# @param [String] path
|
||||
# @return [String]
|
||||
def strip_away_locale(path)
|
||||
if app.respond_to? :langs
|
||||
path.match(/([^.\/]+)\.([^.]+)$/) do |m|
|
||||
if app.langs.include?(m[2].to_sym)
|
||||
return m[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
path
|
||||
end
|
||||
|
||||
# Finds an extension for path according to file's extension
|
||||
# @param [String] path without extension
|
||||
# @param [String] file path with original extensions
|
||||
def find_extension(path, file)
|
||||
input_ext = File.extname(file)
|
||||
|
||||
if !input_ext.empty?
|
||||
input_ext = input_ext.split(".").last.to_sym
|
||||
if @app.template_extensions.has_key?(input_ext)
|
||||
path << ".#{@app.template_extensions[input_ext]}"
|
||||
end
|
||||
end
|
||||
|
||||
path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,8 +18,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
|||
|
||||
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
|
||||
|
||||
# Ruby 2.0 beep beep
|
||||
::Middleman::Sitemap::Store.send :prepend, StoreInstanceMethods
|
||||
app.send :include, LocaleHelpers
|
||||
end
|
||||
|
||||
def after_configuration
|
||||
|
@ -111,14 +110,16 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
|||
new_resources = []
|
||||
|
||||
resources.each do |resource|
|
||||
# If it's a "localizable template"
|
||||
if File.fnmatch?(File.join(options[:templates_dir], "**"), resource.path)
|
||||
page_id = File.basename(resource.path, File.extname(resource.path))
|
||||
|
||||
old_locale = ::I18n.locale
|
||||
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 lang, path, basename = parse_locale_extension(resource.path)
|
||||
elsif m = result = parse_locale_extension(resource.path)
|
||||
lang, path, page_id = result
|
||||
new_resources << build_resource(path, resource.path, page_id, lang)
|
||||
end
|
||||
end
|
||||
|
@ -129,22 +130,23 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
|||
private
|
||||
|
||||
# Parse locale extension filename
|
||||
# @return [Hash] with :basename, :locale, and :path
|
||||
# @return [locale, path, basename]
|
||||
# will return +nil+ if no locale extension
|
||||
def parse_locale_extension(path)
|
||||
path.match(/([^.]*)\.([^.]*)/) do |m|
|
||||
path.match(/([^.\/]+)\.([^.]+)$/) do |m|
|
||||
locale = m[2].to_sym
|
||||
path = path.sub("."+m[2], "")
|
||||
basename = File.basename(m[1])
|
||||
path = m[1]
|
||||
basename = File.basename(path)
|
||||
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], "")
|
||||
old_locale = ::I18n.locale
|
||||
::I18n.locale = lang
|
||||
|
||||
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => [])
|
||||
|
||||
# Build lang path
|
||||
if @mount_at_root == lang
|
||||
prefix = "/"
|
||||
else
|
||||
|
@ -152,10 +154,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
|||
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))
|
||||
)
|
||||
|
@ -163,10 +161,18 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
|||
@_localization_data[path] = [lang, path, localized_page_id]
|
||||
|
||||
p = ::Middleman::Sitemap::Resource.new(app.sitemap, path)
|
||||
p.proxy_to(resource.path)
|
||||
p.proxy_to(source_path)
|
||||
|
||||
::I18n.locale = old_locale
|
||||
|
||||
p
|
||||
end
|
||||
|
||||
module LocaleHelpers
|
||||
# Access the list of languages supported by this Middleman application
|
||||
# @return [Array<Symbol>]
|
||||
def langs
|
||||
extensions[:i18n].langs
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue