Tweak locale links
This commit is contained in:
parent
6502b37934
commit
d3c7436647
|
@ -1,6 +1,6 @@
|
||||||
Feature: i18n Links
|
Feature: i18n Paths
|
||||||
|
|
||||||
Scenario: A template changes i18n during preview
|
Scenario: link_to is i18n aware
|
||||||
Given a fixture app "empty-app"
|
Given a fixture app "empty-app"
|
||||||
And a file named "data/pages.yml" with:
|
And a file named "data/pages.yml" with:
|
||||||
"""
|
"""
|
||||||
|
@ -46,4 +46,46 @@ Feature: i18n Links
|
||||||
Then I should see '<a class="current" href="/es/hola.html">Current hello.html</a>'
|
Then I should see '<a class="current" href="/es/hola.html">Current hello.html</a>'
|
||||||
Then I should see '<a title="Other hello.html" href="/hello.html">Other hello.html</a>'
|
Then I should see '<a title="Other hello.html" href="/hello.html">Other hello.html</a>'
|
||||||
Then I should see '<a class="current" href="/es/hola.html"><span>Current Block</span></a>'
|
Then I should see '<a class="current" href="/es/hola.html"><span>Current Block</span></a>'
|
||||||
Then I should see '<a title="Other hello.html" href="/hello.html"><span>Other Block</span></a>'
|
Then I should see '<a title="Other hello.html" href="/hello.html"><span>Other Block</span></a>'
|
||||||
|
|
||||||
|
Scenario: url_for is i18n aware
|
||||||
|
Given a fixture app "empty-app"
|
||||||
|
And a file named "data/pages.yml" with:
|
||||||
|
"""
|
||||||
|
- hello.html
|
||||||
|
"""
|
||||||
|
And a file named "locales/en.yml" with:
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
en:
|
||||||
|
msg: Hello
|
||||||
|
"""
|
||||||
|
And a file named "locales/es.yml" with:
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
es:
|
||||||
|
paths:
|
||||||
|
hello: "hola"
|
||||||
|
msg: Hola
|
||||||
|
"""
|
||||||
|
And a file named "source/localizable/hello.html.erb" with:
|
||||||
|
"""
|
||||||
|
Page: <%= t(:msg) %>
|
||||||
|
<% data.pages.each_with_index do |p, i| %>
|
||||||
|
Current: <%= url_for "/#{p}" %>
|
||||||
|
Other: <%= url_for "/#{p}", locale: ::I18n.locale == :en ? :es : :en %>
|
||||||
|
<% end %>
|
||||||
|
"""
|
||||||
|
And a file named "config.rb" with:
|
||||||
|
"""
|
||||||
|
activate :i18n
|
||||||
|
"""
|
||||||
|
Given the Server is running at "empty-app"
|
||||||
|
When I go to "/hello.html"
|
||||||
|
Then I should see "Page: Hello"
|
||||||
|
Then I should see 'Current: /hello.html'
|
||||||
|
Then I should see 'Other: /es/hola.html'
|
||||||
|
When I go to "/es/hola.html"
|
||||||
|
Then I should see "Page: Hola"
|
||||||
|
Then I should see 'Current: /es/hola.html'
|
||||||
|
Then I should see 'Other: /hello.html'
|
||||||
|
|
|
@ -73,7 +73,7 @@ module Middleman
|
||||||
|
|
||||||
# Which server name should be used
|
# Which server name should be used
|
||||||
# @return [NilClass, String]
|
# @return [NilClass, String]
|
||||||
config.define_setting :host, nil , 'The preview host name'
|
config.define_setting :host, nil, 'The preview host name'
|
||||||
|
|
||||||
# Whether to serve the preview server over HTTPS.
|
# Whether to serve the preview server over HTTPS.
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
|
|
|
@ -34,6 +34,6 @@ module Middleman::Cli
|
||||||
|
|
||||||
# Map "u" to "upgrade"
|
# Map "u" to "upgrade"
|
||||||
Base.map(
|
Base.map(
|
||||||
'u' => 'upgrade'
|
'u' => 'upgrade'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -64,8 +64,8 @@ module Middleman::Cli
|
||||||
|
|
||||||
# Map "i", "new" and "n" to "init"
|
# Map "i", "new" and "n" to "init"
|
||||||
Base.map(
|
Base.map(
|
||||||
'i' => 'init',
|
'i' => 'init',
|
||||||
'new' => 'init',
|
'new' => 'init',
|
||||||
'n' => 'init'
|
'n' => 'init'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -101,7 +101,7 @@ module Middleman::CoreExtensions
|
||||||
data, content = frontmatter_and_content(p)
|
data, content = frontmatter_and_content(p)
|
||||||
|
|
||||||
if app.files.exists?("#{path}.frontmatter")
|
if app.files.exists?("#{path}.frontmatter")
|
||||||
external_data, _ = frontmatter_and_content("#{p}.frontmatter")
|
external_data, = frontmatter_and_content("#{p}.frontmatter")
|
||||||
data = external_data.deep_merge(data)
|
data = external_data.deep_merge(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -422,7 +422,8 @@ module Middleman
|
||||||
engine = extension[1..-1].to_sym
|
engine = extension[1..-1].to_sym
|
||||||
|
|
||||||
# Store last engine for later (could be inside nested renders)
|
# Store last engine for later (could be inside nested renders)
|
||||||
self.current_engine, engine_was = engine, current_engine
|
self.current_engine = engine
|
||||||
|
engine_was = current_engine
|
||||||
|
|
||||||
begin
|
begin
|
||||||
content = if block_given?
|
content = if block_given?
|
||||||
|
|
|
@ -173,8 +173,8 @@ module Middleman
|
||||||
def current_path=(path)
|
def current_path=(path)
|
||||||
Thread.current[:current_path] = path
|
Thread.current[:current_path] = path
|
||||||
Thread.current[:legacy_request] = ::Thor::CoreExt::HashWithIndifferentAccess.new(
|
Thread.current[:legacy_request] = ::Thor::CoreExt::HashWithIndifferentAccess.new(
|
||||||
path: path,
|
path: path,
|
||||||
params: req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {}
|
params: req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -198,12 +198,12 @@ module Middleman
|
||||||
http_opts[:SSLEnable] = true
|
http_opts[:SSLEnable] = true
|
||||||
|
|
||||||
if ssl_certificate || ssl_private_key
|
if ssl_certificate || ssl_private_key
|
||||||
raise "You must provide both :ssl_certificate and :ssl_private_key" unless ssl_private_key && ssl_certificate
|
raise 'You must provide both :ssl_certificate and :ssl_private_key' unless ssl_private_key && ssl_certificate
|
||||||
http_opts[:SSLCertificate] = OpenSSL::X509::Certificate.new File.read ssl_certificate
|
http_opts[:SSLCertificate] = OpenSSL::X509::Certificate.new File.read ssl_certificate
|
||||||
http_opts[:SSLPrivateKey] = OpenSSL::PKey::RSA.new File.read ssl_private_key
|
http_opts[:SSLPrivateKey] = OpenSSL::PKey::RSA.new File.read ssl_private_key
|
||||||
else
|
else
|
||||||
# use a generated self-signed cert
|
# use a generated self-signed cert
|
||||||
cert, key = create_self_signed_cert(1024, [["CN", host]], "Middleman Preview Server")
|
cert, key = create_self_signed_cert(1024, [['CN', host]], 'Middleman Preview Server')
|
||||||
http_opts[:SSLCertificate] = cert
|
http_opts[:SSLCertificate] = cert
|
||||||
http_opts[:SSLPrivateKey] = key
|
http_opts[:SSLPrivateKey] = key
|
||||||
end
|
end
|
||||||
|
@ -251,24 +251,24 @@ module Middleman
|
||||||
cert.subject = name
|
cert.subject = name
|
||||||
cert.issuer = name
|
cert.issuer = name
|
||||||
cert.not_before = Time.now
|
cert.not_before = Time.now
|
||||||
cert.not_after = Time.now + (365*24*60*60)
|
cert.not_after = Time.now + (365 * 24 * 60 * 60)
|
||||||
cert.public_key = rsa.public_key
|
cert.public_key = rsa.public_key
|
||||||
|
|
||||||
ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
|
ef = OpenSSL::X509::ExtensionFactory.new(nil, cert)
|
||||||
ef.issuer_certificate = cert
|
ef.issuer_certificate = cert
|
||||||
cert.extensions = [
|
cert.extensions = [
|
||||||
ef.create_extension("basicConstraints","CA:FALSE"),
|
ef.create_extension('basicConstraints', 'CA:FALSE'),
|
||||||
ef.create_extension("keyUsage", "keyEncipherment"),
|
ef.create_extension('keyUsage', 'keyEncipherment'),
|
||||||
ef.create_extension("subjectKeyIdentifier", "hash"),
|
ef.create_extension('subjectKeyIdentifier', 'hash'),
|
||||||
ef.create_extension("extendedKeyUsage", "serverAuth"),
|
ef.create_extension('extendedKeyUsage', 'serverAuth'),
|
||||||
ef.create_extension("nsComment", comment),
|
ef.create_extension('nsComment', comment)
|
||||||
]
|
]
|
||||||
aki = ef.create_extension("authorityKeyIdentifier",
|
aki = ef.create_extension('authorityKeyIdentifier',
|
||||||
"keyid:always,issuer:always")
|
'keyid:always,issuer:always')
|
||||||
cert.add_extension(aki)
|
cert.add_extension(aki)
|
||||||
cert.sign(rsa, OpenSSL::Digest::SHA1.new)
|
cert.sign(rsa, OpenSSL::Digest::SHA1.new)
|
||||||
|
|
||||||
return [ cert, rsa ]
|
[cert, rsa]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Attach a new Middleman::Application instance
|
# Attach a new Middleman::Application instance
|
||||||
|
|
|
@ -28,16 +28,16 @@ module Middleman
|
||||||
root_dotfiles: proc { |file| file.start_with?('.') },
|
root_dotfiles: proc { |file| file.start_with?('.') },
|
||||||
|
|
||||||
# Files starting with an dot, but not .htaccess
|
# Files starting with an dot, but not .htaccess
|
||||||
source_dotfiles: proc { |file|
|
source_dotfiles: proc do |file|
|
||||||
file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd|nojekyll)}
|
file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd|nojekyll)}
|
||||||
},
|
end,
|
||||||
|
|
||||||
# Files starting with an underscore, but not a double-underscore
|
# Files starting with an underscore, but not a double-underscore
|
||||||
partials: proc { |file| file =~ %r{/_[^_]} },
|
partials: proc { |file| file =~ %r{/_[^_]} },
|
||||||
|
|
||||||
layout: proc { |file, sitemap_app|
|
layout: proc do |file, sitemap_app|
|
||||||
file.start_with?(File.join(sitemap_app.config[:source], 'layout.')) || file.start_with?(File.join(sitemap_app.config[:source], 'layouts/'))
|
file.start_with?(File.join(sitemap_app.config[:source], 'layout.')) || file.start_with?(File.join(sitemap_app.config[:source], 'layouts/'))
|
||||||
}
|
end
|
||||||
}, 'Callbacks that can exclude paths from the sitemap'
|
}, 'Callbacks that can exclude paths from the sitemap'
|
||||||
|
|
||||||
# Include instance methods
|
# Include instance methods
|
||||||
|
|
|
@ -80,7 +80,7 @@ module Middleman
|
||||||
url = ::Middleman::Util.url_for(store.app, @request_path,
|
url = ::Middleman::Util.url_for(store.app, @request_path,
|
||||||
relative: false,
|
relative: false,
|
||||||
find_resource: true
|
find_resource: true
|
||||||
)
|
)
|
||||||
|
|
||||||
if output
|
if output
|
||||||
output.call(path, url)
|
output.call(path, url)
|
||||||
|
|
|
@ -125,7 +125,8 @@ module Middleman
|
||||||
unless VALID_OPERATORS.include? opts[:operator]
|
unless VALID_OPERATORS.include? opts[:operator]
|
||||||
raise OperatorNotSupportedError
|
raise OperatorNotSupportedError
|
||||||
end
|
end
|
||||||
@attribute, @operator = opts[:attribute], opts[:operator]
|
@attribute = opts[:attribute]
|
||||||
|
@operator = opts[:operator]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -231,10 +231,10 @@ module Middleman
|
||||||
private
|
private
|
||||||
|
|
||||||
def reset_lookup_cache!
|
def reset_lookup_cache!
|
||||||
@lock.synchronize {
|
@lock.synchronize do
|
||||||
@_lookup_by_path = {}
|
@_lookup_by_path = {}
|
||||||
@_lookup_by_destination_path = {}
|
@_lookup_by_destination_path = {}
|
||||||
}
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Removes the templating extensions, while keeping the others
|
# Removes the templating extensions, while keeping the others
|
||||||
|
|
|
@ -8,7 +8,8 @@ class Padrino::Helpers::OutputHelpers::ErbHandler
|
||||||
# Force Erb capture not to use safebuffer
|
# Force Erb capture not to use safebuffer
|
||||||
# rubocop:disable UnderscorePrefixedVariableName
|
# rubocop:disable UnderscorePrefixedVariableName
|
||||||
def capture_from_template(*args, &block)
|
def capture_from_template(*args, &block)
|
||||||
self.output_buffer, _buf_was = '', output_buffer
|
self.output_buffer = ''
|
||||||
|
_buf_was = output_buffer
|
||||||
raw = block.call(*args)
|
raw = block.call(*args)
|
||||||
captured = template.instance_variable_get(:@_out_buf)
|
captured = template.instance_variable_get(:@_out_buf)
|
||||||
self.output_buffer = _buf_was
|
self.output_buffer = _buf_was
|
||||||
|
@ -255,12 +256,12 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
|
||||||
url = url_for(url, options)
|
url = url_for(url, options)
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
# Modified Padrino image_tag so that it finds the paths for srcset
|
# Modified Padrino image_tag so that it finds the paths for srcset
|
||||||
# using asset_path for the images listed in the srcset param
|
# using asset_path for the images listed in the srcset param
|
||||||
def image_tag(path, params={})
|
def image_tag(path, params={})
|
||||||
params.symbolize_keys!
|
params.symbolize_keys!
|
||||||
|
|
||||||
if params.key?(:srcset)
|
if params.key?(:srcset)
|
||||||
images_sources = params[:srcset].split(',').map do |src_def|
|
images_sources = params[:srcset].split(',').map do |src_def|
|
||||||
if src_def.include?('//')
|
if src_def.include?('//')
|
||||||
|
|
|
@ -58,20 +58,11 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
::I18n.t(*args)
|
::I18n.t(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_to(*args, &block)
|
def url_for(path_or_resource, options={})
|
||||||
options = args.extract_options!
|
|
||||||
name = block_given? ? '' : args.shift
|
|
||||||
href = args.first
|
|
||||||
|
|
||||||
locale = options.delete(:locale) || ::I18n.locale
|
locale = options.delete(:locale) || ::I18n.locale
|
||||||
|
|
||||||
url = extensions[:i18n].localized_path(href, locale)
|
|
||||||
new_args = []
|
|
||||||
new_args << name unless block_given?
|
|
||||||
new_args << url || href
|
|
||||||
new_args << options
|
|
||||||
|
|
||||||
super(*new_args, &block)
|
href = super(path_or_resource, options)
|
||||||
|
extensions[:i18n].localized_path(href, locale) || href
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -177,9 +168,9 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
||||||
p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length == 2)
|
p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length == 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
known_langs.map { |p|
|
known_langs.map do |p|
|
||||||
File.basename(p.to_s).sub(/\.ya?ml$/, '').sub(/\.rb$/, '')
|
File.basename(p.to_s).sub(/\.ya?ml$/, '').sub(/\.rb$/, '')
|
||||||
}.sort.map(&:to_sym)
|
end.sort.map(&:to_sym)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -27,8 +27,8 @@ class Middleman::Extensions::AutomaticImageSizes < ::Middleman::Extension
|
||||||
retina = full_path.match(/@(\d)x\.[a-zA-Z]{3,4}$/)
|
retina = full_path.match(/@(\d)x\.[a-zA-Z]{3,4}$/)
|
||||||
if retina
|
if retina
|
||||||
factor = retina[1].to_i
|
factor = retina[1].to_i
|
||||||
width = width / factor
|
width /= factor
|
||||||
height = height / factor
|
height /= factor
|
||||||
end
|
end
|
||||||
params[:width] = width
|
params[:width] = width
|
||||||
params[:height] = height
|
params[:height] = height
|
||||||
|
|
|
@ -103,7 +103,9 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def minify_inline(content)
|
def minify_inline(content)
|
||||||
content.gsub(INLINE_JS_REGEX) do |match|
|
content.gsub(INLINE_JS_REGEX) do |match|
|
||||||
first, inline_content, last = $1, $2, $3
|
first = $1
|
||||||
|
inline_content = $2
|
||||||
|
last = $3
|
||||||
|
|
||||||
# Only compress script tags that contain JavaScript (as opposed to
|
# Only compress script tags that contain JavaScript (as opposed to
|
||||||
# something like jQuery templates, identified with a "text/html" type).
|
# something like jQuery templates, identified with a "text/html" type).
|
||||||
|
|
Loading…
Reference in a new issue