mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Make i18n a normal extension with options
This commit is contained in:
parent
64f84bacd2
commit
d79acc7a78
8 changed files with 47 additions and 59 deletions
|
@ -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"))
|
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)
|
$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
|
# 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
|
ENV["MM_ROOT"] = found_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -29,6 +37,8 @@ require "middleman-core/extensions"
|
||||||
# Default command is server
|
# Default command is server
|
||||||
ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-")
|
ARGV.unshift("server") if ARGV.length < 1 || ARGV.first.include?("-")
|
||||||
|
|
||||||
|
require "middleman-core/cli"
|
||||||
|
|
||||||
# Start the CLI
|
# Start the CLI
|
||||||
if ENV["MM_ROOT"]
|
if ENV["MM_ROOT"]
|
||||||
# Change directory to the root
|
# Change directory to the root
|
||||||
|
|
|
@ -2,18 +2,8 @@
|
||||||
require 'thor'
|
require 'thor'
|
||||||
require "thor/group"
|
require "thor/group"
|
||||||
|
|
||||||
# Core Pathname library used for traversal
|
|
||||||
require "pathname"
|
|
||||||
|
|
||||||
# CLI Module
|
# CLI Module
|
||||||
module Middleman
|
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
|
module Cli
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require "middleman-core"
|
||||||
|
|
||||||
# CLI Module
|
# CLI Module
|
||||||
module Middleman::Cli
|
module Middleman::Cli
|
||||||
|
|
||||||
|
|
|
@ -100,9 +100,6 @@ module Middleman
|
||||||
# @param [Symbol, Module] ext Which extension to activate
|
# @param [Symbol, Module] ext Which extension to activate
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def activate(ext, options={}, &block)
|
def activate(ext, options={}, &block)
|
||||||
# Make :i18n a no-op
|
|
||||||
return if ext == :i18n
|
|
||||||
|
|
||||||
ext_module = if ext.is_a?(Module)
|
ext_module = if ext.is_a?(Module)
|
||||||
ext
|
ext
|
||||||
else
|
else
|
||||||
|
|
|
@ -5,7 +5,7 @@ Feature: i18n Builder
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
And a file named "config.rb" with:
|
||||||
"""
|
"""
|
||||||
localize
|
activate :i18n
|
||||||
"""
|
"""
|
||||||
Given a successfully built app at "i18n-test-app"
|
Given a successfully built app at "i18n-test-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
@ -25,7 +25,7 @@ Feature: i18n Builder
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given a successfully built app at "i18n-test-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
@ -45,7 +45,7 @@ Feature: i18n Builder
|
||||||
Given a fixture app "i18n-alt-root-app"
|
Given a fixture app "i18n-alt-root-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given a successfully built app at "i18n-alt-root-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
@ -65,7 +65,7 @@ Feature: i18n Builder
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given a successfully built app at "i18n-test-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
@ -85,7 +85,7 @@ Feature: i18n Builder
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given a successfully built app at "i18n-test-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
@ -106,7 +106,7 @@ Feature: i18n Builder
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
And a file named "config.rb" with:
|
||||||
"""
|
"""
|
||||||
localize :langs => [:en]
|
activate :i18n, :langs => [:en]
|
||||||
"""
|
"""
|
||||||
Given a successfully built app at "i18n-test-app"
|
Given a successfully built app at "i18n-test-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
|
|
@ -5,7 +5,7 @@ Feature: i18n Preview
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
And a file named "config.rb" with:
|
||||||
"""
|
"""
|
||||||
localize
|
activate :i18n
|
||||||
"""
|
"""
|
||||||
Given the Server is running at "i18n-test-app"
|
Given the Server is running at "i18n-test-app"
|
||||||
When I go to "/"
|
When I go to "/"
|
||||||
|
@ -23,7 +23,7 @@ Feature: i18n Preview
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given the Server is running at "i18n-test-app"
|
||||||
When I go to "/"
|
When I go to "/"
|
||||||
|
@ -42,7 +42,7 @@ Feature: i18n Preview
|
||||||
Given a fixture app "i18n-alt-root-app"
|
Given a fixture app "i18n-alt-root-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given the Server is running at "i18n-alt-root-app"
|
||||||
When I go to "/"
|
When I go to "/"
|
||||||
|
@ -60,7 +60,7 @@ Feature: i18n Preview
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given the Server is running at "i18n-test-app"
|
||||||
When I go to "/"
|
When I go to "/"
|
||||||
|
@ -78,7 +78,7 @@ Feature: i18n Preview
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
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"
|
Given the Server is running at "i18n-test-app"
|
||||||
When I go to "/en/index.html"
|
When I go to "/en/index.html"
|
||||||
|
@ -98,7 +98,7 @@ Feature: i18n Preview
|
||||||
Given a fixture app "i18n-test-app"
|
Given a fixture app "i18n-test-app"
|
||||||
And a file named "config.rb" with:
|
And a file named "config.rb" with:
|
||||||
"""
|
"""
|
||||||
localize :langs => [:en]
|
activate :i18n, :langs => [:en]
|
||||||
"""
|
"""
|
||||||
Given the Server is running at "i18n-test-app"
|
Given the Server is running at "i18n-test-app"
|
||||||
When I go to "/"
|
When I go to "/"
|
||||||
|
|
|
@ -52,8 +52,20 @@ module Middleman::More
|
||||||
Middleman::Application.register Middleman::CoreExtensions::Assets
|
Middleman::Application.register Middleman::CoreExtensions::Assets
|
||||||
|
|
||||||
# i18n
|
# i18n
|
||||||
require "middleman-more/core_extensions/i18n"
|
require "i18n"
|
||||||
Middleman::Application.register Middleman::CoreExtensions::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
|
# Compass framework
|
||||||
require "middleman-more/core_extensions/compass"
|
require "middleman-more/core_extensions/compass"
|
||||||
|
|
|
@ -1,38 +1,31 @@
|
||||||
# i18n Namespace
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
|
|
||||||
|
# i18n Namespace
|
||||||
module I18n
|
module I18n
|
||||||
|
|
||||||
# Setup extension
|
# Setup extension
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
# Once registerd
|
# Once registerd
|
||||||
def registered(app)
|
def registered(app, options={})
|
||||||
app.set :locales_dir, "locales"
|
app.set :locales_dir, "locales"
|
||||||
|
|
||||||
app.send :include, InstanceMethods
|
|
||||||
|
|
||||||
# Needed for helpers as well
|
# Needed for helpers as well
|
||||||
app.after_configuration do
|
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.load_path += Dir[File.join(root, locales_dir, "*.yml")]
|
||||||
::I18n.reload!
|
::I18n.reload!
|
||||||
|
|
||||||
|
Localizer.new(self, options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :included :registered
|
alias :included :registered
|
||||||
end
|
end
|
||||||
|
|
||||||
class Localizer
|
class Localizer
|
||||||
def initialize(app)
|
def initialize(app, options={})
|
||||||
@app = app
|
@app = app
|
||||||
@maps = {}
|
@maps = {}
|
||||||
end
|
|
||||||
|
|
||||||
def setup(options)
|
|
||||||
@options = options
|
@options = options
|
||||||
|
|
||||||
@lang_map = @options[:lang_map] || {}
|
@lang_map = @options[:lang_map] || {}
|
||||||
|
@ -63,7 +56,7 @@ module Middleman
|
||||||
|
|
||||||
@app.sitemap.register_resource_list_manipulator(
|
@app.sitemap.register_resource_list_manipulator(
|
||||||
:i18n,
|
:i18n,
|
||||||
@app.i18n
|
self
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -125,22 +118,6 @@ module Middleman
|
||||||
resources + new_resources
|
resources + new_resources
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue