From 2500e4d35daff4d4d3f763e1e069bd01d6da747b Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 20 Apr 2013 14:58:06 -0700 Subject: [PATCH] convert i18n to new ext --- .../core_extensions/extensions.rb | 6 +- .../lib/middleman-core/extensions.rb | 8 +- middleman-more/lib/middleman-more.rb | 13 +- .../middleman-more/core_extensions/i18n.rb | 298 ++++++++---------- 4 files changed, 148 insertions(+), 177 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index 12469eed..161a7584 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index 9a29d035..df1f6737 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -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 diff --git a/middleman-more/lib/middleman-more.rb b/middleman-more/lib/middleman-more.rb index c5d500c0..1f03c891 100644 --- a/middleman-more/lib/middleman-more.rb +++ b/middleman-more/lib/middleman-more.rb @@ -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" diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 8f8630a0..a1dbe1c6 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -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