Make i18n a normal extension with options

This commit is contained in:
Thomas Reynolds 2012-05-24 16:51:36 -07:00
parent 64f84bacd2
commit d79acc7a78
8 changed files with 47 additions and 59 deletions

View file

@ -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

View file

@ -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

View file

@ -1,3 +1,5 @@
require "middleman-core"
# CLI Module
module Middleman::Cli

View file

@ -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

View file

@ -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"

View file

@ -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 "/"

View file

@ -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"

View file

@ -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