2012-05-07 23:41:39 +02:00
|
|
|
module Middleman
|
|
|
|
module CoreExtensions
|
2012-05-25 01:51:36 +02:00
|
|
|
|
|
|
|
# i18n Namespace
|
2012-06-06 04:11:05 +02:00
|
|
|
module Internationalization
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Setup extension
|
|
|
|
class << self
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Once registerd
|
2012-05-25 01:51:36 +02:00
|
|
|
def registered(app, options={})
|
2012-05-07 23:41:39 +02:00
|
|
|
app.set :locales_dir, "locales"
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Needed for helpers as well
|
|
|
|
app.after_configuration do
|
|
|
|
::I18n.load_path += Dir[File.join(root, locales_dir, "*.yml")]
|
|
|
|
::I18n.reload!
|
2012-05-25 01:51:36 +02:00
|
|
|
|
|
|
|
Localizer.new(self, options)
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :included :registered
|
2012-04-16 22:30:22 +02:00
|
|
|
end
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
class Localizer
|
2012-07-15 20:04:45 +02:00
|
|
|
attr_reader :app
|
|
|
|
delegate :logger, :to => :app
|
|
|
|
|
2012-05-25 01:51:36 +02:00
|
|
|
def initialize(app, options={})
|
2012-05-07 23:41:39 +02:00
|
|
|
@app = app
|
|
|
|
@maps = {}
|
|
|
|
@options = options
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
@lang_map = @options[:lang_map] || {}
|
|
|
|
@path = @options[:path] || "/:locale/"
|
|
|
|
@templates_dir = @options[:templates_dir] || "localizable"
|
|
|
|
@mount_at_root = @options.has_key?(:mount_at_root) ? @options[:mount_at_root] : langs.first
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
if !@app.build?
|
2012-07-15 20:04:45 +02:00
|
|
|
logger.info "== Locales: #{langs.join(", ")}"
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Don't output localizable files
|
|
|
|
@app.ignore File.join(@templates_dir, "**")
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
@app.sitemap.provides_metadata_for_path do |url|
|
|
|
|
if d = get_localization_data(url)
|
|
|
|
lang, page_id = d
|
|
|
|
instance_vars = Proc.new {
|
|
|
|
::I18n.locale = lang
|
|
|
|
@lang = lang
|
|
|
|
@page_id = page_id
|
|
|
|
}
|
|
|
|
{ :blocks => [instance_vars] }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
2012-04-16 23:01:18 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
@app.sitemap.register_resource_list_manipulator(
|
|
|
|
:i18n,
|
2012-05-25 01:51:36 +02:00
|
|
|
self
|
2012-05-07 23:41:39 +02:00
|
|
|
)
|
|
|
|
end
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
def langs
|
|
|
|
@options[:langs] || begin
|
|
|
|
Dir[File.join(@app.root, @app.locales_dir, "*.yml")].map { |file|
|
|
|
|
File.basename(file).gsub(".yml", "")
|
|
|
|
}.sort.map(&:to_sym)
|
|
|
|
end
|
|
|
|
end
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
def get_localization_data(path)
|
|
|
|
@_localization_data ||= {}
|
|
|
|
@_localization_data[path]
|
|
|
|
end
|
2012-03-11 04:40:04 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Update the main sitemap resource list
|
|
|
|
# @return [void]
|
|
|
|
def manipulate_resource_list(resources)
|
|
|
|
@_localization_data = {}
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
new_resources = []
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
resources.each do |resource|
|
|
|
|
next unless File.fnmatch(File.join(@templates_dir, "**"), resource.path)
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
page_id = File.basename(resource.path, File.extname(resource.path))
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
langs.map do |lang|
|
|
|
|
::I18n.locale = lang
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id)
|
|
|
|
path = resource.path.sub(@templates_dir, "")
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Build lang path
|
|
|
|
if @mount_at_root == lang
|
|
|
|
prefix = "/"
|
|
|
|
else
|
|
|
|
replacement = @lang_map.has_key?(lang) ? @lang_map[lang] : lang
|
|
|
|
prefix = @path.sub(":locale", replacement.to_s)
|
|
|
|
end
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
path = ::Middleman::Util.normalize_path(
|
|
|
|
File.join(prefix, path.sub(page_id, localized_page_id))
|
|
|
|
)
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
@_localization_data[path] = [lang, path, localized_page_id]
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
p = ::Middleman::Sitemap::Resource.new(
|
|
|
|
@app.sitemap,
|
|
|
|
path
|
|
|
|
)
|
|
|
|
p.proxy_to(resource.path)
|
2012-04-16 22:30:22 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
new_resources << p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
resources + new_resources
|
2012-04-16 22:30:22 +02:00
|
|
|
end
|
|
|
|
end
|
2012-03-11 04:40:04 +01:00
|
|
|
end
|
|
|
|
end
|
2012-06-06 04:11:05 +02:00
|
|
|
end
|