Refactor some of i18n for better codeclimate scores. In the process, make nested i18n files work like rails

i18n_v4
Thomas Reynolds 2013-05-29 14:00:37 -04:00
parent 86cd626084
commit 31f238f881
9 changed files with 109 additions and 69 deletions

View File

@ -228,3 +228,17 @@ Feature: i18n Preview
Then I should see "Current locale: en"
Then I should see "Buenos días"
Then I should see "Howdy"
Scenario: Nested i18n yaml
Given a fixture app "i18n-nested-app"
And a file named "config.rb" with:
"""
activate :i18n
"""
Given the Server is running at "i18n-nested-app"
When I go to "/"
Then I should see "Howdy"
Then I should see "More"
When I go to "/es/"
Then I should see "Como Esta?"
Then I should see "Mucho"

View File

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

View File

@ -0,0 +1,3 @@
---
en:
more: "More"

View File

@ -0,0 +1,4 @@
---
es:
greetings: "Como Esta?"
hi: "Hola"

View File

@ -0,0 +1,3 @@
---
es:
more: "Mucho"

View File

@ -0,0 +1,3 @@
<%= t(:greetings) %>
<%= t(:more) %>

View File

@ -37,24 +37,11 @@ module Middleman
cattr_accessor :middleman_app
def image(link, title, alt_text)
if middleman_app && middleman_app.respond_to?(:image_tag)
middleman_app.image_tag(link, :title => title, :alt => alt_text)
else
img = "<img src=\"#{link}\""
img << " title=\"#{title}\"" if title
img << " alt=\"#{alt_text}\"" if alt_text
img << ">"
end
middleman_app.image_tag(link, :title => title, :alt => alt_text)
end
def link(link, title, content)
if middleman_app && middleman_app.respond_to?(:link_to)
middleman_app.link_to(content, link, :title => title)
else
a = "<a href=\"#{link}\""
a << " title=\"#{title}\"" if title
a << ">#{content}</a>"
end
middleman_app.link_to(content, link, :title => title)
end
end

View File

@ -1,5 +1,5 @@
module Middleman
# Current Version
# @return [String]
VERSION = '3.1.0.rc.1' unless const_defined?(:VERSION)
VERSION = '3.1.0.rc.2' unless const_defined?(:VERSION)
end

View File

@ -22,24 +22,15 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end
def after_configuration
@locales_glob = File.join(app.config[:locals_dir] || options[:data], "**", "*.{rb,yml,yaml}")
app.files.reload_path(app.config[:locals_dir] || options[:data])
# File.fnmatch doesn't support brackets: {rb,yml,yaml}
regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml")
@locales_regex = %r{^#{regex}}
@locales_glob = File.join(app.config[:locals_dir] || options[:data], "**", "*.{rb,yml,yaml}")
@locales_regex = convert_glob_to_regex(@locales_glob)
@maps = {}
::I18n.load_path += Dir[File.join(app.root, @locales_glob)]
::I18n.reload!
@mount_at_root = options[:mount_at_root].nil? ? langs.first : options[:mount_at_root]
::I18n.default_locale = @mount_at_root
# Reset fallbacks to fall back to our new default
if ::I18n.respond_to? :fallbacks
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new
end
configure_i18n
if !app.build?
logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})"
@ -48,27 +39,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
# Don't output localizable files
app.ignore File.join(options[:templates_dir], "**")
app.sitemap.provides_metadata_for_path do |url|
if d = get_localization_data(url)
lang, page_id = d
else
# Default to the @mount_at_root lang
page_id = nil
lang = @mount_at_root
end
instance_vars = Proc.new do
@lang = lang
@page_id = page_id
end
locals = { :lang => lang,
:page_id => page_id }
{ :blocks => [instance_vars],
:locals => locals,
:options => { :lang => lang } }
end
app.sitemap.provides_metadata_for_path(&method(:metadata_for_path))
app.files.changed(&method(:on_file_changed))
app.files.deleted(&method(:on_file_changed))
end
@ -81,27 +52,10 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
delegate :logger, :to => :app
def on_file_changed(file)
if @locales_regex =~ file
::I18n.reload!
end
end
def langs
if options[:langs]
Array(options[:langs]).map(&:to_sym)
else
Dir[File.join(app.root, @locales_glob)].map { |file|
File.basename(file).sub(/\.ya?ml$/, "").sub(/\.rb$/, "")
}.sort.map(&:to_sym)
end
@_langs ||= get_known_languages
end
def get_localization_data(path)
@_localization_data ||= {}
@_localization_data[path]
end
# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
@ -129,6 +83,74 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
private
def on_file_changed(file)
if @locales_regex =~ file
@_langs = nil # Clear langs cache
::I18n.reload!
end
end
def convert_glob_to_regex(glob)
# File.fnmatch doesn't support brackets: {rb,yml,yaml}
regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml")
%r{^#{regex}}
end
def configure_i18n
::I18n.load_path += Dir[File.join(app.root, @locales_glob)]
::I18n.reload!
::I18n.default_locale = @mount_at_root
# Reset fallbacks to fall back to our new default
if ::I18n.respond_to? :fallbacks
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new
end
end
def metadata_for_path(url)
if d = get_localization_data(url)
lang, page_id = d
else
# Default to the @mount_at_root lang
page_id = nil
lang = @mount_at_root
end
instance_vars = Proc.new do
@lang = lang
@page_id = page_id
end
locals = {
:lang => lang,
:page_id => page_id
}
{
:blocks => [instance_vars],
:locals => locals,
:options => { :lang => lang }
}
end
def get_known_languages
if options[:langs]
Array(options[:langs]).map(&:to_sym)
else
known_langs = app.files.known_paths.select do |p|
p.to_s.match(@locales_regex) && (p.to_s.split(File::SEPARATOR).length === 2)
end.map { |p|
File.basename(p.to_s).sub(/\.ya?ml$/, "").sub(/\.rb$/, "")
}.sort.map(&:to_sym)
end
end
def get_localization_data(path)
@_localization_data ||= {}
@_localization_data[path]
end
# Parse locale extension filename
# @return [lang, path, basename]
# will return +nil+ if no locale extension