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