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)
|
||||
else
|
||||
if extensions[ext]
|
||||
logger.error "== #{ext} already activated. Overwriting."
|
||||
end
|
||||
|
||||
logger.error "== #{ext} already activated."
|
||||
else
|
||||
extensions[ext] = ext_module.new(self.class, options, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Access activated extensions
|
||||
#
|
||||
|
|
|
@ -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
|
||||
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
|
||||
Middleman::Extensions::CacheBuster.register
|
||||
|
||||
# MinifyCss compresses CSS
|
||||
require "middleman-more/extensions/minify_css"
|
||||
|
|
|
@ -1,61 +1,37 @@
|
|||
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
|
||||
|
||||
# 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
|
||||
def initialize(app, options_hash={}, &block)
|
||||
super
|
||||
|
||||
# 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
|
||||
require "i18n/backend/fallbacks"
|
||||
::I18n::Backend::Simple.send(:include, ::I18n::Backend::Fallbacks)
|
||||
end
|
||||
|
||||
# Central class for managing i18n extension
|
||||
class Localizer
|
||||
attr_reader :app
|
||||
delegate :logger, :to => :app
|
||||
app.config.define_setting :locales_dir, "locales", 'The directory holding your locale configurations'
|
||||
end
|
||||
|
||||
def initialize(app, options={})
|
||||
@app = app
|
||||
@locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml,yaml}")
|
||||
def after_configuration
|
||||
@locales_glob = File.join(app.config[:locals_dir] || options[:data], "**", "*.{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
|
||||
@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
|
||||
|
@ -63,14 +39,14 @@ module Middleman
|
|||
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new
|
||||
end
|
||||
|
||||
if !@app.build?
|
||||
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.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)
|
||||
lang, page_id = d
|
||||
else
|
||||
|
@ -91,15 +67,18 @@ module Middleman
|
|||
: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))
|
||||
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!
|
||||
|
@ -107,10 +86,10 @@ module Middleman
|
|||
end
|
||||
|
||||
def langs
|
||||
if @options[:langs]
|
||||
Array(@options[:langs]).map(&:to_sym)
|
||||
if options[:langs]
|
||||
Array(options[:langs]).map(&:to_sym)
|
||||
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$/, "")
|
||||
}.sort.map(&:to_sym)
|
||||
end
|
||||
|
@ -129,7 +108,7 @@ module Middleman
|
|||
new_resources = []
|
||||
|
||||
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))
|
||||
|
||||
|
@ -138,14 +117,14 @@ module Middleman
|
|||
::I18n.locale = lang
|
||||
|
||||
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
|
||||
if @mount_at_root == lang
|
||||
prefix = "/"
|
||||
else
|
||||
replacement = @lang_map.has_key?(lang) ? @lang_map[lang] : lang
|
||||
prefix = @path.sub(":locale", replacement.to_s)
|
||||
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(
|
||||
|
@ -155,7 +134,7 @@ module Middleman
|
|||
@_localization_data[path] = [lang, path, localized_page_id]
|
||||
|
||||
p = ::Middleman::Sitemap::Resource.new(
|
||||
@app.sitemap,
|
||||
app.sitemap,
|
||||
path
|
||||
)
|
||||
p.proxy_to(resource.path)
|
||||
|
@ -169,6 +148,3 @@ module Middleman
|
|||
resources + new_resources
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue