Skip to content
Browse files

use autoload for standard constant lookup (instead of manual require)

  • Loading branch information...
1 parent 170ca6a commit 9f0b3650ae8b12df43d715518a6ede4ade6e3662 @svenfuchs committed Nov 16, 2009
View
11 lib/i18n.rb
@@ -1,17 +1,20 @@
# encoding: utf-8
-# Authors:: Matt Aimonetti (http://railsontherun.com/),
-# Sven Fuchs (http://www.artweb-design.de),
+# Authors:: Sven Fuchs (http://www.artweb-design.de),
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
+# Stephan Soller (http://www.arkanis-development.de/),
# Saimon Moore (http://saimonmoore.net),
-# Stephan Soller (http://www.arkanis-development.de/)
+# Matt Aimonetti (http://railsontherun.com/)
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
# License:: MIT
-require 'i18n/backend/simple'
require 'i18n/exceptions'
require 'i18n/core_ext/string/interpolate'
module I18n
+ autoload :Backend, 'i18n/backend'
+ autoload :Helpers, 'i18n/helpers'
+ autoload :Locale, 'i18n/locale'
+
@@backend = nil
@@load_path = nil
@@default_locale = :en
View
14 lib/i18n/backend.rb
@@ -0,0 +1,14 @@
+module I18n
+ module Backend
+ autoload :ActiveRecord, 'i18n/backend/active_record'
+ autoload :Base, 'i18n/backend/base'
+ autoload :Cache, 'i18n/backend/cache'
+ autoload :Chain, 'i18n/backend/chain'
+ autoload :Fallbacks, 'i18n/backend/fallbacks'
+ autoload :Gettext, 'i18n/backend/gettext'
+ autoload :Helpers, 'i18n/backend/helpers'
+ autoload :Metadata, 'i18n/backend/metadata'
+ autoload :Pluralization, 'i18n/backend/pluralization'
+ autoload :Simple, 'i18n/backend/simple'
+ end
+end
View
6 lib/i18n/backend/active_record.rb
@@ -1,9 +1,9 @@
-require 'i18n/backend/base'
-require 'i18n/backend/active_record/translation'
-
module I18n
module Backend
class ActiveRecord
+ autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
+ autoload :Translation, 'i18n/backend/active_record/translation'
+
include Base
def reload!
View
1 lib/i18n/backend/base.rb
@@ -1,7 +1,6 @@
# encoding: utf-8
require 'yaml'
-require 'i18n/backend/helpers'
module I18n
module Backend
View
2 lib/i18n/backend/fallbacks.rb
@@ -1,7 +1,5 @@
# encoding: utf-8
-require 'i18n/locale/fallbacks'
-
# I18n locale fallbacks are useful when you want your application to use
# translations from other locales when translations for the current locale are
# missing. E.g. you might want to use :en translations when translations in
View
2 lib/i18n/backend/simple.rb
@@ -1,7 +1,5 @@
# encoding: utf-8
-require 'i18n/backend/base'
-
# Stub class for the Simple backend. The actual implementation is provided by
# the backend Base class. This makes it easier to extend the Simple backend's
# behaviour by including modules. E.g.:
View
4 lib/i18n/gettext.rb
@@ -6,7 +6,7 @@ module Gettext
CONTEXT_SEPARATOR = "\004"
@@plural_keys = { :en => [:one, :other] }
-
+
class << self
# returns an array of plural keys for the given locale so that we can
# convert from gettext's integer-index based style
@@ -22,4 +22,4 @@ def extract_scope(msgid, separator = nil)
end
end
end
-end
+end
View
5 lib/i18n/helpers.rb
@@ -0,0 +1,5 @@
+module I18n
+ module Helpers
+ autoload :Gettext, 'i18n/helpers/gettext'
+ end
+end
View
6 lib/i18n/locale.rb
@@ -0,0 +1,6 @@
+module I18n
+ module Locale
+ autoload :Fallbacks, 'lib/i18n/locale/fallbacks'
+ autoload :Tag, 'lib/i18n/locale/tag'
+ end
+end
View
2 lib/i18n/locale/fallbacks.rb
@@ -1,7 +1,5 @@
# encoding: utf-8
-require 'i18n/locale/tag'
-
# Locale Fallbacks
#
# Extends the I18n module to hold a fallbacks instance which is set to an
View
7 lib/i18n/locale/tag.rb
@@ -1,11 +1,12 @@
# encoding: utf-8
-require 'i18n/locale/tag/simple'
-require 'i18n/locale/tag/rfc4646'
-
module I18n
module Locale
module Tag
+ autoload :Parents, 'lib/i18n/locale/tag/parents'
+ autoload :Rfc4646, 'lib/i18n/locale/tag/rfc4646'
+ autoload :Simple, 'lib/i18n/locale/tag/simple'
+
class << self
# Returns the current locale tag implementation. Defaults to +I18n::Locale::Tag::Simple+.
def implementation
View
2 lib/i18n/locale/tag/rfc4646.rb
@@ -7,8 +7,6 @@
#
# Rfc4646::Parser does not implement grandfathered tags.
-require 'i18n/locale/tag/parents'
-
module I18n
module Locale
module Tag
View
3 lib/i18n/locale/tag/simple.rb
@@ -1,10 +1,7 @@
# encoding: utf-8
-require 'i18n/locale/tag/parents'
-
# Simple Locale tag implementation that computes subtags by simply splitting
# the locale tag at '-' occurences.
-
module I18n
module Locale
module Tag
View
8 test/locale/tag/rfc4646_test.rb
@@ -1,14 +1,12 @@
# encoding: utf-8
require File.dirname(__FILE__) + '/../../test_helper.rb'
-require 'i18n/locale/tag'
-require 'i18n/locale/tag/rfc4646'
# Rfc4646::Parser
class I18nLocaleTagRfc4646ParserTest < Test::Unit::TestCase
include I18n::Locale
-
+
test "Rfc4646::Parser given a valid tag 'de' returns an array of subtags" do
assert_equal ['de', nil, nil, nil, nil, nil, nil], Tag::Rfc4646::Parser.match('de')
end
@@ -38,7 +36,7 @@ class I18nLocaleTagRfc4646ParserTest < Test::Unit::TestCase
class I18nLocaleTagSubtagsTest < Test::Unit::TestCase
include I18n::Locale
-
+
def setup
subtags = %w(de Latn DE variant a-ext x-phonebk i-klingon)
@tag = Tag::Rfc4646.new *subtags
@@ -144,4 +142,4 @@ class I18nLocaleTagSubtagsTest < Test::Unit::TestCase
tag = Tag::Rfc4646.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
assert_equal parents, tag.self_and_parents.map{|tag| tag.to_s}
end
-end
+end
View
4 test/locale/tag/simple_test.rb
@@ -14,7 +14,7 @@ class I18nLocaleTagSimpleTest < Test::Unit::TestCase
test "returns a formatted tag string from #to_s" do
assert_equal 'de-Latn-DE', Tag::Simple.new('de-Latn-DE').to_s
end
-
+
test "returns an array containing the formatted subtags from #to_a" do
assert_equal %w(de Latn DE), Tag::Simple.new('de-Latn-DE').to_a
end
@@ -32,4 +32,4 @@ class I18nLocaleTagSimpleTest < Test::Unit::TestCase
test "#self_and_parents returns an array of 3 tags for 'de-Latn-DE'" do
assert_equal %w(de-Latn-DE de-Latn de), Tag::Simple.new('de-Latn-DE').self_and_parents.map { |tag| tag.to_s}
end
-end
+end

0 comments on commit 9f0b365

Please sign in to comment.
Something went wrong with that request. Please try again.