From d79acc7a78d11c32888affb780a62444a899259e Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Thu, 24 May 2012 16:51:36 -0700 Subject: [PATCH] Make i18n a normal extension with options --- middleman-core/bin/middleman | 14 ++++++- middleman-core/lib/middleman-core/cli.rb | 10 ----- .../lib/middleman-core/cli/build.rb | 2 + .../core_extensions/extensions.rb | 3 -- middleman-more/features/i18n_builder.feature | 12 +++--- middleman-more/features/i18n_preview.feature | 12 +++--- middleman-more/lib/middleman-more.rb | 16 +++++++- .../middleman-more/core_extensions/i18n.rb | 37 ++++--------------- 8 files changed, 47 insertions(+), 59 deletions(-) diff --git a/middleman-core/bin/middleman b/middleman-core/bin/middleman index a2f90c1a..fbf07871 100755 --- a/middleman-core/bin/middleman +++ b/middleman-core/bin/middleman @@ -7,10 +7,18 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) moredir = File.expand_path(File.join(File.dirname(File.dirname(libdir)), "middleman-more", "lib", "middleman-more")) $LOAD_PATH.unshift(moredir) unless $LOAD_PATH.include?(moredir) -require "middleman-core/cli" +# Core Pathname library used for traversal +require "pathname" + +# Recursive method to find config.rb +def locate_root(cwd = Pathname.new(Dir.pwd)) + return cwd.to_s if File.exists?(File.join(cwd, 'config.rb')) + return false if cwd.root? + locate_root(cwd.parent) +end # Only look for config.rb if MM_ROOT isn't set -if !ENV["MM_ROOT"] && found_path = ::Middleman.locate_root +if !ENV["MM_ROOT"] && found_path = locate_root ENV["MM_ROOT"] = found_path end @@ -29,6 +37,8 @@ require "middleman-core/extensions" # Default command is server ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-") +require "middleman-core/cli" + # Start the CLI if ENV["MM_ROOT"] # Change directory to the root diff --git a/middleman-core/lib/middleman-core/cli.rb b/middleman-core/lib/middleman-core/cli.rb index de41418b..56cc6927 100644 --- a/middleman-core/lib/middleman-core/cli.rb +++ b/middleman-core/lib/middleman-core/cli.rb @@ -2,18 +2,8 @@ require 'thor' require "thor/group" -# Core Pathname library used for traversal -require "pathname" - # CLI Module module Middleman - - # Recursive method to find config.rb - def self.locate_root(cwd = Pathname.new(Dir.pwd)) - return cwd.to_s if File.exists?(File.join(cwd, 'config.rb')) - return false if cwd.root? - locate_root(cwd.parent) - end module Cli diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 83b15e50..7754b91c 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -1,3 +1,5 @@ +require "middleman-core" + # CLI Module module Middleman::Cli diff --git a/middleman-core/lib/middleman-core/core_extensions/extensions.rb b/middleman-core/lib/middleman-core/core_extensions/extensions.rb index 48974fcd..9f708352 100644 --- a/middleman-core/lib/middleman-core/core_extensions/extensions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/extensions.rb @@ -100,9 +100,6 @@ module Middleman # @param [Symbol, Module] ext Which extension to activate # @return [void] def activate(ext, options={}, &block) - # Make :i18n a no-op - return if ext == :i18n - ext_module = if ext.is_a?(Module) ext else diff --git a/middleman-more/features/i18n_builder.feature b/middleman-more/features/i18n_builder.feature index f3380be4..97fb5025 100644 --- a/middleman-more/features/i18n_builder.feature +++ b/middleman-more/features/i18n_builder.feature @@ -5,7 +5,7 @@ Feature: i18n Builder Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize + activate :i18n """ Given a successfully built app at "i18n-test-app" When I cd to "build" @@ -25,7 +25,7 @@ Feature: i18n Builder Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :path => "/lang_:locale/" + activate :i18n, :path => "/lang_:locale/" """ Given a successfully built app at "i18n-test-app" When I cd to "build" @@ -45,7 +45,7 @@ Feature: i18n Builder Given a fixture app "i18n-alt-root-app" And a file named "config.rb" with: """ - localize :templates_dir => "lang_data" + activate :i18n, :templates_dir => "lang_data" """ Given a successfully built app at "i18n-alt-root-app" When I cd to "build" @@ -65,7 +65,7 @@ Feature: i18n Builder Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :lang_map => { :en => :english, :es => :spanish } + activate :i18n, :lang_map => { :en => :english, :es => :spanish } """ Given a successfully built app at "i18n-test-app" When I cd to "build" @@ -85,7 +85,7 @@ Feature: i18n Builder Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :mount_at_root => false + activate :i18n, :mount_at_root => false """ Given a successfully built app at "i18n-test-app" When I cd to "build" @@ -106,7 +106,7 @@ Feature: i18n Builder Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :langs => [:en] + activate :i18n, :langs => [:en] """ Given a successfully built app at "i18n-test-app" When I cd to "build" diff --git a/middleman-more/features/i18n_preview.feature b/middleman-more/features/i18n_preview.feature index d228ebe7..490b454d 100644 --- a/middleman-more/features/i18n_preview.feature +++ b/middleman-more/features/i18n_preview.feature @@ -5,7 +5,7 @@ Feature: i18n Preview Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize + activate :i18n """ Given the Server is running at "i18n-test-app" When I go to "/" @@ -23,7 +23,7 @@ Feature: i18n Preview Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :path => "/lang_:locale/" + activate :i18n, :path => "/lang_:locale/" """ Given the Server is running at "i18n-test-app" When I go to "/" @@ -42,7 +42,7 @@ Feature: i18n Preview Given a fixture app "i18n-alt-root-app" And a file named "config.rb" with: """ - localize :templates_dir => "lang_data" + activate :i18n, :templates_dir => "lang_data" """ Given the Server is running at "i18n-alt-root-app" When I go to "/" @@ -60,7 +60,7 @@ Feature: i18n Preview Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :lang_map => { :en => :english, :es => :spanish } + activate :i18n, :lang_map => { :en => :english, :es => :spanish } """ Given the Server is running at "i18n-test-app" When I go to "/" @@ -78,7 +78,7 @@ Feature: i18n Preview Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :mount_at_root => false + activate :i18n, :mount_at_root => false """ Given the Server is running at "i18n-test-app" When I go to "/en/index.html" @@ -98,7 +98,7 @@ Feature: i18n Preview Given a fixture app "i18n-test-app" And a file named "config.rb" with: """ - localize :langs => [:en] + activate :i18n, :langs => [:en] """ Given the Server is running at "i18n-test-app" When I go to "/" diff --git a/middleman-more/lib/middleman-more.rb b/middleman-more/lib/middleman-more.rb index a95f21cb..49a32b92 100644 --- a/middleman-more/lib/middleman-more.rb +++ b/middleman-more/lib/middleman-more.rb @@ -52,8 +52,20 @@ module Middleman::More Middleman::Application.register Middleman::CoreExtensions::Assets # i18n - require "middleman-more/core_extensions/i18n" - Middleman::Application.register Middleman::CoreExtensions::I18n + require "i18n" + app.after_configuration do + # This is for making the tests work - since the tests + # don't completely reload middleman, I18n.load_path can get + # polluted with paths from other test app directories that don't + # exist anymore. + ::I18n.load_path.delete_if {|path| path =~ %r{tmp/aruba}} + ::I18n.reload! + end + + Middleman::Extensions.register(:i18n) do + require "middleman-more/core_extensions/i18n" + Middleman::CoreExtensions::I18n + end # Compass framework require "middleman-more/core_extensions/compass" diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 7113197a..d9d6f310 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -1,38 +1,31 @@ -# i18n Namespace module Middleman module CoreExtensions + + # i18n Namespace module I18n # Setup extension class << self # Once registerd - def registered(app) + def registered(app, options={}) app.set :locales_dir, "locales" - - app.send :include, InstanceMethods # Needed for helpers as well app.after_configuration do - # This is for making the tests work - since the tests - # don't completely reload middleman, I18n.load_path can get - # polluted with paths from other test app directories that don't - # exist anymore. - ::I18n.load_path.delete_if {|path| path =~ %r{tmp/aruba}} ::I18n.load_path += Dir[File.join(root, locales_dir, "*.yml")] ::I18n.reload! + + Localizer.new(self, options) end end alias :included :registered end class Localizer - def initialize(app) + def initialize(app, options={}) @app = app @maps = {} - end - - def setup(options) @options = options @lang_map = @options[:lang_map] || {} @@ -63,7 +56,7 @@ module Middleman @app.sitemap.register_resource_list_manipulator( :i18n, - @app.i18n + self ) end @@ -125,22 +118,6 @@ module Middleman resources + new_resources end end - - # Frontmatter class methods - module InstanceMethods - - # Initialize the i18n - def i18n - @_i18n ||= Localizer.new(self) - end - - # Main i18n API - def localize(options={}) - settings.after_configuration do - i18n.setup(options) - end - end - end end end end \ No newline at end of file