convert i18n to new ext
This commit is contained in:
parent
01e85b8bf7
commit
2500e4d35d
|
@ -112,10 +112,10 @@ module Middleman
|
|||
extensions[ext][key] = ext_module.new(self.class, options, &block)
|
||||
else
|
||||
if extensions[ext]
|
||||
logger.error "== #{ext} already activated. Overwriting."
|
||||
logger.error "== #{ext} already activated."
|
||||
else
|
||||
extensions[ext] = ext_module.new(self.class, options, &block)
|
||||
end
|
||||
|
||||
extensions[ext] = ext_module.new(self.class, options, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -158,13 +158,13 @@ module Middleman
|
|||
end
|
||||
|
||||
klass.after_configuration do
|
||||
if ext.respond_to?(:manipulate_resource_list)
|
||||
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
|
||||
end
|
||||
|
||||
if ext.respond_to?(:after_configuration)
|
||||
ext.after_configuration
|
||||
end
|
||||
|
||||
if ext.respond_to?(:manipulate_resource_list)
|
||||
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,7 +23,6 @@ module Middleman
|
|||
require "middleman-more/core_extensions/default_helpers"
|
||||
Middleman::CoreExtensions::DefaultHelpers.new(app)
|
||||
|
||||
# i18n
|
||||
require "i18n"
|
||||
app.after_configuration do
|
||||
# This is for making the tests work - since the tests
|
||||
|
@ -34,10 +33,8 @@ module Middleman
|
|||
::I18n.reload!
|
||||
end
|
||||
|
||||
Middleman::Extensions.register(:i18n) do
|
||||
require "middleman-more/core_extensions/i18n"
|
||||
Middleman::CoreExtensions::Internationalization
|
||||
end
|
||||
require "middleman-more/core_extensions/i18n"
|
||||
Middleman::CoreExtensions::Internationalization.register(:i18n)
|
||||
|
||||
# Compass framework
|
||||
require "middleman-more/core_extensions/compass"
|
||||
|
@ -49,10 +46,8 @@ module Middleman
|
|||
|
||||
# CacheBuster adds a query string to assets in dynamic templates to
|
||||
# avoid browser caches failing to update to your new content.
|
||||
Middleman::Extensions.register(:cache_buster) do
|
||||
require "middleman-more/extensions/cache_buster"
|
||||
Middleman::Extensions::CacheBuster
|
||||
end
|
||||
require "middleman-more/extensions/cache_buster"
|
||||
Middleman::Extensions::CacheBuster.register
|
||||
|
||||
# MinifyCss compresses CSS
|
||||
require "middleman-more/extensions/minify_css"
|
||||
|
|
|
@ -1,174 +1,150 @@
|
|||
require "i18n"
|
||||
require "i18n/backend/fallbacks"
|
||||
class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||
option :no_fallbacks, false, "Disable I18n fallbacks"
|
||||
option :langs, nil, "List of langs, will autodiscover by default"
|
||||
option :lang_map, {}, "Language shortname map"
|
||||
option :path, "/:locale/", "URL prefix path"
|
||||
option :templates_dir, "localizable", "Location of templates to be localized"
|
||||
option :mount_at_root, nil, "Mount a specific language at the root of the site"
|
||||
option :data, "locales", "The directory holding your locale configurations"
|
||||
|
||||
module Middleman
|
||||
module CoreExtensions
|
||||
def initialize(app, options_hash={}, &block)
|
||||
super
|
||||
|
||||
# i18n Namespace
|
||||
module Internationalization
|
||||
# See https://github.com/svenfuchs/i18n/wiki/Fallbacks
|
||||
unless options[:no_fallbacks]
|
||||
require "i18n/backend/fallbacks"
|
||||
::I18n::Backend::Simple.send(:include, ::I18n::Backend::Fallbacks)
|
||||
end
|
||||
|
||||
# Setup extension
|
||||
class << self
|
||||
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
|
||||
end
|
||||
|
||||
# Once registerd
|
||||
def registered(app, options={})
|
||||
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
|
||||
def after_configuration
|
||||
@locales_glob = File.join(app.config[:locals_dir] || options[:data], "**", "*.{rb,yml,yaml}")
|
||||
|
||||
# Needed for helpers as well
|
||||
app.after_configuration do
|
||||
Localizer.new(self, options)
|
||||
end
|
||||
# File.fnmatch doesn't support brackets: {rb,yml,yaml}
|
||||
regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml")
|
||||
@locales_regex = %r{^#{regex}}
|
||||
|
||||
app.helpers do
|
||||
def t(*args)
|
||||
::I18n.t(*args)
|
||||
end
|
||||
end
|
||||
@maps = {}
|
||||
|
||||
# See https://github.com/svenfuchs/i18n/wiki/Fallbacks
|
||||
unless options[:no_fallbacks]
|
||||
::I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
::I18n.load_path += Dir[File.join(app.root, @locales_glob)]
|
||||
::I18n.reload!
|
||||
|
||||
@mount_at_root = options[:mount_at_root].nil? ? langs.first : options[:mount_at_root]
|
||||
|
||||
::I18n.default_locale = @mount_at_root
|
||||
# Reset fallbacks to fall back to our new default
|
||||
if ::I18n.respond_to? :fallbacks
|
||||
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new
|
||||
end
|
||||
|
||||
if !app.build?
|
||||
logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})"
|
||||
end
|
||||
|
||||
# Don't output localizable files
|
||||
app.ignore File.join(options[:templates_dir], "**")
|
||||
|
||||
app.sitemap.provides_metadata_for_path do |url|
|
||||
if d = get_localization_data(url)
|
||||
lang, page_id = d
|
||||
else
|
||||
# Default to the @mount_at_root lang
|
||||
page_id = nil
|
||||
lang = @mount_at_root
|
||||
end
|
||||
|
||||
# Central class for managing i18n extension
|
||||
class Localizer
|
||||
attr_reader :app
|
||||
delegate :logger, :to => :app
|
||||
|
||||
def initialize(app, options={})
|
||||
@app = app
|
||||
@locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml,yaml}")
|
||||
|
||||
# File.fnmatch doesn't support brackets: {rb,yml,yaml}
|
||||
regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml")
|
||||
@locales_regex = %r{^#{regex}}
|
||||
|
||||
@maps = {}
|
||||
@options = options
|
||||
|
||||
::I18n.load_path += Dir[File.join(app.root, @locales_glob)]
|
||||
::I18n.reload!
|
||||
|
||||
@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
|
||||
|
||||
::I18n.default_locale = @mount_at_root
|
||||
# Reset fallbacks to fall back to our new default
|
||||
if ::I18n.respond_to? :fallbacks
|
||||
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new
|
||||
end
|
||||
|
||||
if !@app.build?
|
||||
logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})"
|
||||
end
|
||||
|
||||
# Don't output localizable files
|
||||
@app.ignore File.join(@templates_dir, "**")
|
||||
|
||||
@app.sitemap.provides_metadata_for_path do |url|
|
||||
if d = get_localization_data(url)
|
||||
lang, page_id = d
|
||||
else
|
||||
# Default to the @mount_at_root lang
|
||||
page_id = nil
|
||||
lang = @mount_at_root
|
||||
end
|
||||
|
||||
instance_vars = Proc.new do
|
||||
@lang = lang
|
||||
@page_id = page_id
|
||||
end
|
||||
|
||||
locals = { :lang => lang,
|
||||
:page_id => page_id }
|
||||
{ :blocks => [instance_vars],
|
||||
:locals => locals,
|
||||
:options => { :lang => lang } }
|
||||
end
|
||||
|
||||
@app.sitemap.register_resource_list_manipulator(
|
||||
:i18n,
|
||||
self
|
||||
)
|
||||
|
||||
@app.files.changed(&method(:on_file_changed))
|
||||
@app.files.deleted(&method(:on_file_changed))
|
||||
end
|
||||
|
||||
def on_file_changed(file)
|
||||
if @locales_regex =~ file
|
||||
::I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
def langs
|
||||
if @options[:langs]
|
||||
Array(@options[:langs]).map(&:to_sym)
|
||||
else
|
||||
Dir[File.join(@app.root, @locales_glob)].map { |file|
|
||||
File.basename(file).sub(/\.ya?ml$/, "").sub(/\.rb$/, "")
|
||||
}.sort.map(&:to_sym)
|
||||
end
|
||||
end
|
||||
|
||||
def get_localization_data(path)
|
||||
@_localization_data ||= {}
|
||||
@_localization_data[path]
|
||||
end
|
||||
|
||||
# Update the main sitemap resource list
|
||||
# @return [void]
|
||||
def manipulate_resource_list(resources)
|
||||
@_localization_data = {}
|
||||
|
||||
new_resources = []
|
||||
|
||||
resources.each do |resource|
|
||||
next unless File.fnmatch(File.join(@templates_dir, "**"), resource.path)
|
||||
|
||||
page_id = File.basename(resource.path, File.extname(resource.path))
|
||||
|
||||
old_locale = ::I18n.locale
|
||||
langs.map do |lang|
|
||||
::I18n.locale = lang
|
||||
|
||||
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => [])
|
||||
path = resource.path.sub(@templates_dir, "")
|
||||
|
||||
# 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
|
||||
|
||||
path = ::Middleman::Util.normalize_path(
|
||||
File.join(prefix, path.sub(page_id, localized_page_id))
|
||||
)
|
||||
|
||||
@_localization_data[path] = [lang, path, localized_page_id]
|
||||
|
||||
p = ::Middleman::Sitemap::Resource.new(
|
||||
@app.sitemap,
|
||||
path
|
||||
)
|
||||
p.proxy_to(resource.path)
|
||||
|
||||
new_resources << p
|
||||
end
|
||||
::I18n.locale = old_locale
|
||||
|
||||
end
|
||||
|
||||
resources + new_resources
|
||||
end
|
||||
instance_vars = Proc.new do
|
||||
@lang = lang
|
||||
@page_id = page_id
|
||||
end
|
||||
|
||||
locals = { :lang => lang,
|
||||
:page_id => page_id }
|
||||
{ :blocks => [instance_vars],
|
||||
:locals => locals,
|
||||
:options => { :lang => lang } }
|
||||
end
|
||||
|
||||
app.files.changed(&method(:on_file_changed))
|
||||
app.files.deleted(&method(:on_file_changed))
|
||||
end
|
||||
|
||||
helpers do
|
||||
def t(*args)
|
||||
::I18n.t(*args)
|
||||
end
|
||||
end
|
||||
|
||||
delegate :logger, :to => :app
|
||||
|
||||
def on_file_changed(file)
|
||||
if @locales_regex =~ file
|
||||
::I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
def langs
|
||||
if options[:langs]
|
||||
Array(options[:langs]).map(&:to_sym)
|
||||
else
|
||||
Dir[File.join(app.root, @locales_glob)].map { |file|
|
||||
File.basename(file).sub(/\.ya?ml$/, "").sub(/\.rb$/, "")
|
||||
}.sort.map(&:to_sym)
|
||||
end
|
||||
end
|
||||
|
||||
def get_localization_data(path)
|
||||
@_localization_data ||= {}
|
||||
@_localization_data[path]
|
||||
end
|
||||
|
||||
# Update the main sitemap resource list
|
||||
# @return [void]
|
||||
def manipulate_resource_list(resources)
|
||||
@_localization_data = {}
|
||||
|
||||
new_resources = []
|
||||
|
||||
resources.each do |resource|
|
||||
next unless File.fnmatch(File.join(options[:templates_dir], "**"), resource.path)
|
||||
|
||||
page_id = File.basename(resource.path, File.extname(resource.path))
|
||||
|
||||
old_locale = ::I18n.locale
|
||||
langs.map do |lang|
|
||||
::I18n.locale = lang
|
||||
|
||||
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => [])
|
||||
path = resource.path.sub(options[:templates_dir], "")
|
||||
|
||||
# Build lang path
|
||||
if @mount_at_root == lang
|
||||
prefix = "/"
|
||||
else
|
||||
replacement = options[:lang_map].has_key?(lang) ? options[:lang_map][lang] : lang
|
||||
prefix = options[:path].sub(":locale", replacement.to_s)
|
||||
end
|
||||
|
||||
path = ::Middleman::Util.normalize_path(
|
||||
File.join(prefix, path.sub(page_id, localized_page_id))
|
||||
)
|
||||
|
||||
@_localization_data[path] = [lang, path, localized_page_id]
|
||||
|
||||
p = ::Middleman::Sitemap::Resource.new(
|
||||
app.sitemap,
|
||||
path
|
||||
)
|
||||
p.proxy_to(resource.path)
|
||||
|
||||
new_resources << p
|
||||
end
|
||||
::I18n.locale = old_locale
|
||||
|
||||
end
|
||||
|
||||
resources + new_resources
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue