Progress towards making localizable pages work

This commit is contained in:
Ben Hollis 2013-05-02 21:17:50 -07:00
parent 1345ae6e90
commit d58acae939
3 changed files with 75 additions and 53 deletions

View file

@ -2,6 +2,7 @@ master
=== ===
* DataStore may now be accessed like a hash with #[] and #has_key?. #880 * 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 3.1.0.beta.2
=== ===

View file

@ -187,39 +187,7 @@ module Middleman
path = remove_templating_extensions(path) path = remove_templating_extensions(path)
# If there is no extension, look for one # If there is no extension, look for one
path = find_extension(path, file) if File.extname(path).empty? path = find_extension(path, file) if File.extname(strip_away_locale(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 path
end end
@ -256,6 +224,53 @@ module Middleman
@_lookup_by_destination_path = {} @_lookup_by_destination_path = {}
} }
end 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 end
end end

View file

@ -18,8 +18,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations' app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
# Ruby 2.0 beep beep app.send :include, LocaleHelpers
::Middleman::Sitemap::Store.send :prepend, StoreInstanceMethods
end end
def after_configuration def after_configuration
@ -111,14 +110,16 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
new_resources = [] new_resources = []
resources.each do |resource| resources.each do |resource|
# If it's a "localizable template"
if 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.each do |lang| langs.each do |lang|
# Remove folder name
path = resource.path.sub(options[:templates_dir], "")
new_resources << build_resource(path, resource.path, page_id, lang) new_resources << build_resource(path, resource.path, page_id, lang)
end 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) new_resources << build_resource(path, resource.path, page_id, lang)
end end
end end
@ -129,22 +130,23 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
private private
# Parse locale extension filename # Parse locale extension filename
# @return [Hash] with :basename, :locale, and :path # @return [locale, path, basename]
# will return +nil+ if no locale extension # will return +nil+ if no locale extension
def parse_locale_extension(path) def parse_locale_extension(path)
path.match(/([^.]*)\.([^.]*)/) do |m| path.match(/([^.\/]+)\.([^.]+)$/) do |m|
locale = m[2].to_sym locale = m[2].to_sym
path = path.sub("."+m[2], "") path = m[1]
basename = File.basename(m[1]) basename = File.basename(path)
langs.include?(locale) ? [locale, path, basename] : nil langs.include?(locale) ? [locale, path, basename] : nil
end end
end end
def build_resource(path, source_path, page_id, lang) def build_resource(path, source_path, page_id, lang)
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => []) old_locale = ::I18n.locale
path = resource.path.sub(options[:templates_dir], "") ::I18n.locale = lang
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => [])
# Build lang path
if @mount_at_root == lang if @mount_at_root == lang
prefix = "/" prefix = "/"
else else
@ -152,10 +154,6 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
prefix = options[:path].sub(":locale", replacement.to_s) prefix = options[:path].sub(":locale", replacement.to_s)
end end
# Localize page id
old_locale = ::I18n.locale
::I18n.locale = lang
path = ::Middleman::Util.normalize_path( path = ::Middleman::Util.normalize_path(
File.join(prefix, path.sub(page_id, localized_page_id)) 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] @_localization_data[path] = [lang, path, localized_page_id]
p = ::Middleman::Sitemap::Resource.new(app.sitemap, path) p = ::Middleman::Sitemap::Resource.new(app.sitemap, path)
p.proxy_to(resource.path) p.proxy_to(source_path)
::I18n.locale = old_locale ::I18n.locale = old_locale
p p
end end
module LocaleHelpers
# Access the list of languages supported by this Middleman application
# @return [Array<Symbol>]
def langs
extensions[:i18n].langs
end
end
end end