i18n not ready for primetime yet

This commit is contained in:
Thomas Reynolds 2012-03-10 19:40:04 -08:00
parent 08f5e64858
commit 7d7f9a20ef
52 changed files with 593 additions and 1 deletions

View file

@ -0,0 +1,37 @@
@wip
Feature: Builder Alt Path
In order to preview localized html
Scenario: EN should be at root
Given a successfully built app at "alt-path-app"
When I cd to "build"
Then the following files should exist:
| index.html |
And the file "index.html" should contain "Howdy"
Scenario: EN mounted at root should not be in directory
Given a successfully built app at "alt-path-app"
When I cd to "build"
Then the following files should not exist:
| lang_en/index.html |
Scenario: Paths can be localized EN
Given a successfully built app at "alt-path-app"
When I cd to "build"
Then the following files should exist:
| hello.html |
And the file "hello.html" should contain "Hello World"
Scenario: ES should be under namespace
Given a successfully built app at "alt-path-app"
When I cd to "build"
Then the following files should exist:
| lang_es/index.html |
And the file "lang_es/index.html" should contain "Como Esta?"
Scenario: Paths can be localized ES
Given a successfully built app at "alt-path-app"
When I cd to "build"
Then the following files should exist:
| lang_es/hola.html |
And the file "lang_es/hola.html" should contain "Hola World"

View file

@ -0,0 +1,23 @@
@wip
Feature: Builder Alt Root
In order to preview localized html
Scenario: EN should be at root
Given a built app at "alt-root-app"
Then "index.html" should exist at "alt-root-app" and include "Howdy"
Scenario: EN mounted at root should not be in directory
Given a built app at "alt-root-app"
Then "en/index.html" should not exist at "alt-root-app"
Scenario: Paths can be localized EN
Given a built app at "alt-root-app"
Then "hello.html" should exist at "alt-root-app" and include "Hello World"
Scenario: ES should be under namespace
Given a built app at "alt-root-app"
Then "es/index.html" should exist at "alt-root-app" and include "Como Esta?"
Scenario: Paths can be localized ES
Given a built app at "alt-root-app"
Then "es/hola.html" should exist at "alt-root-app" and include "Hola World"

View file

@ -0,0 +1,23 @@
@wip
Feature: Builder Default
In order to preview localized html
Scenario: EN should be at root
Given a built app at "i18n-test-app"
Then "index.html" should exist at "i18n-test-app" and include "Howdy"
Scenario: EN mounted at root should not be in directory
Given a built app at "i18n-test-app"
Then "en/index.html" should not exist at "i18n-test-app"
Scenario: Paths can be localized EN
Given a built app at "i18n-test-app"
Then "hello.html" should exist at "i18n-test-app" and include "Hello World"
Scenario: ES should be under namespace
Given a built app at "i18n-test-app"
Then "es/index.html" should exist at "i18n-test-app" and include "Como Esta?"
Scenario: Paths can be localized ES
Given a built app at "i18n-test-app"
Then "es/hola.html" should exist at "i18n-test-app" and include "Hola World"

View file

@ -0,0 +1,23 @@
@wip
Feature: Builder Lang Map
In order to preview localized html
Scenario: EN should be at root
Given a built app at "name-map-app"
Then "index.html" should exist at "name-map-app" and include "Howdy"
Scenario: EN mounted at root should not be in directory
Given a built app at "name-map-app"
Then "english/index.html" should not exist at "name-map-app"
Scenario: Paths can be localized EN
Given a built app at "name-map-app"
Then "hello.html" should exist at "name-map-app" and include "Hello World"
Scenario: ES should be under namespace
Given a built app at "name-map-app"
Then "spanish/index.html" should exist at "name-map-app" and include "Como Esta?"
Scenario: Paths can be localized ES
Given a built app at "name-map-app"
Then "spanish/hola.html" should exist at "name-map-app" and include "Hola World"

View file

@ -0,0 +1,27 @@
@wip
Feature: Builder No Mount
In order to preview localized html
Scenario: EN should be at root
Given a built app at "no-mount-app"
Then "en/index.html" should exist at "no-mount-app" and include "Howdy"
Scenario: EN mounted at root should not be in directory
Given a built app at "no-mount-app"
Then "index.html" should not exist at "no-mount-app"
Scenario: Paths can be localized EN
Given a built app at "no-mount-app"
Then "en/hello.html" should exist at "no-mount-app" and include "Hello World"
Scenario: EN mounted at root should not be in directory
Given a built app at "no-mount-app"
Then "hello.html" should not exist at "no-mount-app"
Scenario: ES should be under namespace
Given a built app at "i18n-test-app"
Then "es/index.html" should exist at "i18n-test-app" and include "Como Esta?"
Scenario: Paths can be localized ES
Given a built app at "i18n-test-app"
Then "es/hola.html" should exist at "i18n-test-app" and include "Hola World"

View file

@ -0,0 +1,23 @@
@wip
Feature: Builder Subset
In order to preview localized html
Scenario: EN should be at root
Given a built app at "subset-app"
Then "index.html" should exist at "subset-app" and include "Howdy"
Scenario: EN mounted at root should not be in directory
Given a built app at "subset-app"
Then "en/index.html" should not exist at "subset-app"
Scenario: Paths can be localized EN
Given a built app at "subset-app"
Then "hello.html" should exist at "subset-app" and include "Hello World"
Scenario: ES should be under namespace
Given a built app at "subset-app"
Then "es/index.html" should not exist at "subset-app"
Scenario: Paths can be localized ES
Given a built app at "subset-app"
Then "es/hola.html" should not exist at "subset-app"

View file

@ -0,0 +1,38 @@
@wip
Feature: Preview Alt Path
In order to preview localized html
Scenario: EN should be at root
Given the Server is running at "alt-path-app"
When I go to "/"
Then I should see "Howdy"
Scenario: EN should be at root 2
Given the Server is running at "alt-path-app"
When I go to "/index.html"
Then I should see "Howdy"
Scenario: EN mounted at root should not be in directory
Given the Server is running at "alt-path-app"
When I go to "/lang_en/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized EN
Given the Server is running at "alt-path-app"
When I go to "/hello.html"
Then I should see "Hello World"
Scenario: ES should be under namespace
Given the Server is running at "alt-path-app"
When I go to "/lang_es/"
Then I should see "Como Esta?"
Scenario: ES should be under namespace 2
Given the Server is running at "alt-path-app"
When I go to "/lang_es/index.html"
Then I should see "Como Esta?"
Scenario: Paths can be localized ES
Given the Server is running at "alt-path-app"
When I go to "/lang_es/hola.html"
Then I should see "Hola World"

View file

@ -0,0 +1,38 @@
@wip
Feature: Preview Alt Root
In order to preview localized html
Scenario: EN should be at root
Given the Server is running at "alt-root-app"
When I go to "/"
Then I should see "Howdy"
Scenario: EN should be at root 2
Given the Server is running at "alt-root-app"
When I go to "/index.html"
Then I should see "Howdy"
Scenario: EN mounted at root should not be in directory
Given the Server is running at "alt-root-app"
When I go to "/en/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized EN
Given the Server is running at "alt-root-app"
When I go to "/hello.html"
Then I should see "Hello World"
Scenario: ES should be under namespace
Given the Server is running at "alt-root-app"
When I go to "/es/"
Then I should see "Como Esta?"
Scenario: ES should be under namespace 2
Given the Server is running at "alt-root-app"
When I go to "/es/index.html"
Then I should see "Como Esta?"
Scenario: Paths can be localized ES
Given the Server is running at "alt-root-app"
When I go to "/es/hola.html"
Then I should see "Hola World"

View file

@ -0,0 +1,38 @@
@wip
Feature: Preview Default
In order to preview localized html
Scenario: EN should be at root
Given the Server is running at "i18n-test-app"
When I go to "/"
Then I should see "Howdy"
Scenario: EN should be at root 2
Given the Server is running at "i18n-test-app"
When I go to "/index.html"
Then I should see "Howdy"
Scenario: EN mounted at root should not be in directory
Given the Server is running at "i18n-test-app"
When I go to "/en/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized EN
Given the Server is running at "i18n-test-app"
When I go to "/hello.html"
Then I should see "Hello World"
Scenario: ES should be under namespace
Given the Server is running at "i18n-test-app"
When I go to "/es/"
Then I should see "Como Esta?"
Scenario: ES should be under namespace 2
Given the Server is running at "i18n-test-app"
When I go to "/es/index.html"
Then I should see "Como Esta?"
Scenario: Paths can be localized ES
Given the Server is running at "i18n-test-app"
When I go to "/es/hola.html"
Then I should see "Hola World"

View file

@ -0,0 +1,38 @@
@wip
Feature: Preview Lang Map
In order to preview localized html
Scenario: EN should be at root
Given the Server is running at "name-map-app"
When I go to "/"
Then I should see "Howdy"
Scenario: EN should be at root 2
Given the Server is running at "name-map-app"
When I go to "/index.html"
Then I should see "Howdy"
Scenario: EN mounted at root should not be in directory
Given the Server is running at "name-map-app"
When I go to "/english/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized EN
Given the Server is running at "name-map-app"
When I go to "/hello.html"
Then I should see "Hello World"
Scenario: ES should be under namespace
Given the Server is running at "name-map-app"
When I go to "/spanish/"
Then I should see "Como Esta?"
Scenario: ES should be under namespace 2
Given the Server is running at "name-map-app"
When I go to "/spanish/index.html"
Then I should see "Como Esta?"
Scenario: Paths can be localized ES
Given the Server is running at "name-map-app"
When I go to "/spanish/hola.html"
Then I should see "Hola World"

View file

@ -0,0 +1,38 @@
@wip
Feature: Preview No Mount
In order to preview localized html
Scenario: EN should be at root
Given the Server is running at "no-mount-app"
When I go to "/en/"
Then I should see "Howdy"
Scenario: EN should be at root 2
Given the Server is running at "no-mount-app"
When I go to "/en/index.html"
Then I should see "Howdy"
Scenario: EN mounted at root should not be in directory
Given the Server is running at "no-mount-app"
When I go to "/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized EN
Given the Server is running at "no-mount-app"
When I go to "/en/hello.html"
Then I should see "Hello World"
Scenario: ES should be under namespace
Given the Server is running at "no-mount-app"
When I go to "/es/"
Then I should see "Como Esta?"
Scenario: ES should be under namespace 2
Given the Server is running at "no-mount-app"
When I go to "/es/index.html"
Then I should see "Como Esta?"
Scenario: Paths can be localized ES
Given the Server is running at "no-mount-app"
When I go to "/es/hola.html"
Then I should see "Hola World"

View file

@ -0,0 +1,38 @@
@wip
Feature: Preview Subset
In order to preview localized html
Scenario: EN should be at root
Given the Server is running at "subset-app"
When I go to "/"
Then I should see "Howdy"
Scenario: EN should be at root 2
Given the Server is running at "subset-app"
When I go to "/index.html"
Then I should see "Howdy"
Scenario: EN mounted at root should not be in directory
Given the Server is running at "subset-app"
When I go to "/en/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized EN
Given the Server is running at "subset-app"
When I go to "/hello.html"
Then I should see "Hello World"
Scenario: ES should be under namespace
Given the Server is running at "subset-app"
When I go to "/es/"
Then I should see "File Not Found"
Scenario: ES should be under namespace 2
Given the Server is running at "subset-app"
When I go to "/es/index.html"
Then I should see "File Not Found"
Scenario: Paths can be localized ES
Given the Server is running at "subset-app"
When I go to "/es/hola.html"
Then I should see "File Not Found"

View file

@ -0,0 +1 @@
12945

View file

@ -0,0 +1,2 @@
activate :i18n
localize :path => "/lang_:locale/"

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,7 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1 @@
<%= I18n.t(:hi) %> World

View file

@ -0,0 +1 @@
<%= I18n.t(:greetings) %>

View file

@ -0,0 +1,2 @@
activate :i18n
localize :templates_dir => "lang_data"

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,7 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"

View file

@ -0,0 +1 @@
<%= I18n.t(:hi) %> World

View file

@ -0,0 +1 @@
<%= I18n.t(:greetings) %>

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1,2 @@
activate :i18n
localize

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,7 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1 @@
<%= I18n.t(:hi) %> World

View file

@ -0,0 +1 @@
<%= I18n.t(:greetings) %>

View file

@ -0,0 +1,2 @@
activate :i18n
localize :lang_map => { :en => :english, :es => :spanish }

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,7 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1 @@
<%= I18n.t(:hi) %> World

View file

@ -0,0 +1 @@
<%= I18n.t(:greetings) %>

View file

@ -0,0 +1,2 @@
activate :i18n
localize :mount_at_root => false

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,7 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1 @@
<%= I18n.t(:hi) %> World

View file

@ -0,0 +1 @@
<%= I18n.t(:greetings) %>

View file

@ -0,0 +1,2 @@
activate :i18n
localize :langs => [:en]

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,7 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"

View file

@ -0,0 +1 @@
<%= yield %>

View file

@ -0,0 +1 @@
<%= I18n.t(:hi) %> World

View file

@ -0,0 +1 @@
<%= I18n.t(:greetings) %>

View file

@ -14,7 +14,7 @@ module Middleman::CoreExtensions::DefaultHelpers
app.helpers ::Padrino::Helpers::FormatHelpers app.helpers ::Padrino::Helpers::FormatHelpers
app.helpers ::Padrino::Helpers::RenderHelpers app.helpers ::Padrino::Helpers::RenderHelpers
app.helpers ::Padrino::Helpers::NumberHelpers app.helpers ::Padrino::Helpers::NumberHelpers
app.helpers ::Padrino::Helpers::TranslationHelpers # app.helpers ::Padrino::Helpers::TranslationHelpers
app.helpers Helpers app.helpers Helpers

View file

@ -97,6 +97,9 @@ module Middleman::CoreExtensions::Extensions
# @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

View file

@ -0,0 +1,108 @@
# i18n Namespace
module Middleman::CoreExtensions::I18n
# Setup extension
class << self
# Once registerd
def registered(app)
app.set :locales_dir, "locales"
app.send :include, InstanceMethods
app.after_configuration do
::I18n.load_path += Dir[File.join(root, locales_dir, "*.yml")]
end
end
alias :included :registered
end
class Localizer
def initialize(app)
@app = app
@maps = {}
end
def setup(options)
@options = options
@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
if !@app.build?
puts "== Locales: #{langs.join(", ")}"
end
# Don't output localizable files
ignore File.join(@templates_dir, "**/*")
provides_metadata_for_path do |url|
if d = get_localization_data(url)
lang, page_id = d
instance_vars = Proc.new {
::I18n.locale = lang
@lang = lang
@page_id = page_id
}
{ :blocks => [instance_vars] }
else
{}
end
end
end
def langs
@options[:langs] || begin
Dir[File.join(@app.root, @app.locales_dir, "*.yml")].map do |file|
File.basename(file).gsub(".yml", "").to_sym
end
end
end
def get_localization_data(url)
if @mount_at_root
else
end
end
# def paths_for_file(file)
# url = @app.sitemap.source_map.index(file)
# page_id = File.basename(url, File.extname(url))
#
# langs.map do |lang|
# ::I18n.locale = lang
#
# # 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
#
# localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id)
#
# path = File.join(prefix, url.sub(page_id, localized_page_id))
# [lang, path, localized_page_id]
# 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