Skip to content

Commit

Permalink
add #load_path to public api, add initialize to simple backend and re…
Browse files Browse the repository at this point in the history
…move #load_translations from public api
  • Loading branch information
Sven Fuchs committed Sep 14, 2008
1 parent 9e1ac6b commit c4c5649
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion i18n.gemspec
Expand Up @@ -19,6 +19,6 @@ Gem::Specification.new do |s|
"spec/core_ext_spec.rb",
"spec/i18n_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
"spec/spec/helper.rb"
]
end
21 changes: 11 additions & 10 deletions lib/i18n.rb
Expand Up @@ -10,7 +10,8 @@

module I18n
@@backend = nil
@@default_locale = 'en-US'
@@load_paths = []
@@default_locale = :'en-US'
@@exception_handler = :default_exception_handler

class << self
Expand Down Expand Up @@ -49,14 +50,16 @@ def exception_handler=(exception_handler)
@@exception_handler = exception_handler
end

# Allows client libraries to pass arguments that specify a source for
# translation data to be loaded by the backend. The backend defines
# acceptable sources.
# Allow clients to register paths providing translation data sources. The
# backend defines acceptable sources.
#
# E.g. the provided SimpleBackend accepts a list of paths to translation
# files which are either named *.rb and contain plain Ruby Hashes or are
# named *.yml and contain YAML data.)
def load_translations(*args)
backend.load_translations(*args)
# named *.yml and contain YAML data. So for the SimpleBackend clients may
# register translation files like this:
# I18n.load_paths << 'path/to/locale/en-US.yml'
def load_paths
@@load_paths
end

# Translates, pluralizes and interpolates a given key using a given locale,
Expand Down Expand Up @@ -175,6 +178,4 @@ def normalize_translation_keys(locale, key, scope)
keys.flatten.map{|k| k.to_sym}
end
end
end


end
6 changes: 6 additions & 0 deletions lib/i18n/backend/simple.rb
Expand Up @@ -60,6 +60,11 @@ def localize(locale, object, format = :default)
end

protected

def initialize
load_translations *I18n.load_paths unless I18n.load_paths.empty?
@initialized = true
end

def translations
@translations ||= {}
Expand All @@ -72,6 +77,7 @@ def translations
# <tt>%w(currency format)</tt>.
def lookup(locale, key, scope = [])
return unless key
initialize unless @initialized
keys = I18n.send :normalize_translation_keys, locale, key, scope
keys.inject(translations){|result, k| result[k.to_sym] or return nil }
end
Expand Down

0 comments on commit c4c5649

Please sign in to comment.