Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add #load_path to public api, add initialize to simple backend and re…

…move #load_translations from public api
  • Loading branch information...
commit c4c5649e6bc8f020f1aaf5a5470bde048e22c82d 1 parent 9e1ac6b
@svenfuchs authored
Showing with 18 additions and 11 deletions.
  1. +1 −1  i18n.gemspec
  2. +11 −10 lib/i18n.rb
  3. +6 −0 lib/i18n/backend/simple.rb
View
2  i18n.gemspec
@@ -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
View
21 lib/i18n.rb
@@ -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
@@ -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,
@@ -175,6 +178,4 @@ def normalize_translation_keys(locale, key, scope)
keys.flatten.map{|k| k.to_sym}
end
end
-end
-
-
+end
View
6 lib/i18n/backend/simple.rb
@@ -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 ||= {}
@@ -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

3 comments on commit c4c5649

@karmi

What’s the preffered option to load locales now, instead of `I18n.load_translations`, Sven? Clemen’s demo app still uses load_translations. I wanted to make work my demo app again with edge.

@aslakhellesoy

@ ruby Dir[“#{locales_dir}/*.{rb,yml}”].uniq.each do |locale_file| Rails.logger.info “** [localized_dates] loading file #{locale_file}” I18n.load_path << locale_file end @

You can see an example in this commit

@aslakhellesoy

Ehrm – Code highlighting is not working in comments I guess :-)

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