Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Base backend now provides a raw implementation, showing the minimum a…

…mount of methods required to implement a functional backend.
  • Loading branch information...
commit 67cb16c1e880629d2aba664953e068fdecf649f1 1 parent a889482
@josevalim josevalim authored
View
5 lib/i18n/backend/active_record.rb
@@ -11,12 +11,7 @@ class ActiveRecord
module Implementation
include Base, Flatten
- def reload!
- end
-
def available_locales
- init_translations unless initialized?
-
begin
Translation.available_locales
rescue ::ActiveRecord::StatementInvalid
View
60 lib/i18n/backend/base.rb
@@ -16,26 +16,21 @@ module Base
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details.
def load_translations(*filenames)
+ filenames = I18n.load_path.flatten if filenames.empty?
filenames.each { |filename| load_file(filename) }
end
- # Stores translations for the given locale in memory.
- # This uses a deep merge for the translations hash, so existing
- # translations will be overwritten by new ones only at the deepest
- # level of the hash.
+ # This method receives a locale, a data hash and options for storing translations.
+ # Should be implemented
def store_translations(locale, data, options = {})
- locale = locale.to_sym
- translations[locale] ||= {}
-
- data = data.deep_symbolize_keys
- translations[locale].deep_merge!(data)
+ raise NotImplementedError
end
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) unless locale
return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
- entry = lookup!(locale, key, options)
+ entry = key && lookup(locale, key, options[:scope], options)
if options.empty?
entry = resolve(locale, key, entry, options)
@@ -80,60 +75,21 @@ def localize(locale, object, format = :default, options = {})
object.strftime(format)
end
- def initialized?
- @initialized ||= false
- end
-
# Returns an array of locales for which translations are available
# ignoring the reserved translation meta data key :i18n.
def available_locales
- init_translations unless initialized?
- translations.inject([]) do |locales, (locale, data)|
- locales << locale unless (data.keys - [:i18n]).empty?
- locales
- end
+ raise NotImplementedError
end
def reload!
- @initialized = false
- @translations = nil
@skip_syntax_deprecation = false
end
protected
- def init_translations
- load_translations(*I18n.load_path.flatten)
- @initialized = true
- end
-
- def translations
- @translations ||= {}
- end
-
- # Check if the key is valid and then initialize the translation and
- # trigger the default lookup behavior.
- def lookup!(locale, key, options)
- return unless key
- init_translations unless initialized?
- lookup(locale, key, options[:scope], options)
- end
-
- # Looks up a translation from the translations hash. Returns nil if
- # eiher key is nil, or locale, scope or key do not exist as a key in the
- # nested translations hash. Splits keys or scopes containing dots
- # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
- # <tt>%w(currency format)</tt>.
+ # The method which actually looks up for the translation in the store.
def lookup(locale, key, scope = [], options = {})
- keys = I18n.normalize_keys(locale, key, scope, options[:separator])
-
- keys.inject(translations) do |result, key|
- key = key.to_sym
- return nil unless result.is_a?(Hash) && result.has_key?(key)
- result = result[key]
- result = resolve(locale, key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
- result
- end
+ raise NotImplementedError
end
# Evaluates defaults.
View
8 lib/i18n/backend/key_value.rb
@@ -1,4 +1,5 @@
# encoding: utf-8
+
require 'i18n/backend/base'
require 'active_support/json'
@@ -47,6 +48,7 @@ module Backend
#
# I18n::Backend::KeyValue.new(@store, false)
#
+ # This is useful if you are using a KeyValue backend chained to a Simple backend.
class KeyValue
module Implementation
attr_accessor :store
@@ -57,10 +59,6 @@ def initialize(store, subtrees=true)
@store, @subtrees = store, subtrees
end
- # Mute reload! since we really don't want to clean the database.
- def reload!
- end
-
def store_translations(locale, data, options = {})
flatten_translations(locale, data, @subtrees).each do |key, value|
key = "#{locale}.#{key}"
@@ -80,8 +78,6 @@ def store_translations(locale, data, options = {})
end
def available_locales
- init_translations unless initialized?
-
locales = @store.keys.map { |k| k =~ /\./; $` }
locales.uniq!
locales.compact!
View
93 lib/i18n/backend/simple.rb
@@ -1,22 +1,87 @@
# encoding: utf-8
-# 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.:
-#
-# module I18n::Backend::Pluralization
-# def pluralize(*args)
-# # extended pluralization logic
-# super
-# end
-# end
-#
-# I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
-
module I18n
module Backend
+ # A simple backend that reads translations from YAML files and stores them in
+ # an in-memory hash. Relies on the Base backend.
+ #
+ # The implementation is provided by a Implementation module allowing to easily
+ # extend Simple backend's behavior by including modules. E.g.:
+ #
+ # module I18n::Backend::Pluralization
+ # def pluralize(*args)
+ # # extended pluralization logic
+ # super
+ # end
+ # end
+ #
+ # I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
class Simple
- include Base
+ module Implementation
+ include Base
+
+ def initialized?
+ @initialized ||= false
+ end
+
+ # Stores translations for the given locale in memory.
+ # This uses a deep merge for the translations hash, so existing
+ # translations will be overwritten by new ones only at the deepest
+ # level of the hash.
+ def store_translations(locale, data, options = {})
+ locale = locale.to_sym
+ translations[locale] ||= {}
+ data = data.deep_symbolize_keys
+ translations[locale].deep_merge!(data)
+ end
+
+ # Get available locales from the translations hash
+ def available_locales
+ init_translations unless initialized?
+ translations.inject([]) do |locales, (locale, data)|
+ locales << locale unless (data.keys - [:i18n]).empty?
+ locales
+ end
+ end
+
+ # Clean up translations hash and set initialized to false on reload!
+ def reload!
+ @initialized = false
+ @translations = nil
+ super
+ end
+
+ protected
+
+ def init_translations
+ load_translations
+ @initialized = true
+ end
+
+ def translations
+ @translations ||= {}
+ end
+
+ # Looks up a translation from the translations hash. Returns nil if
+ # eiher key is nil, or locale, scope or key do not exist as a key in the
+ # nested translations hash. Splits keys or scopes containing dots
+ # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
+ # <tt>%w(currency format)</tt>.
+ def lookup(locale, key, scope = [], options = {})
+ init_translations unless initialized?
+ keys = I18n.normalize_keys(locale, key, scope, options[:separator])
+
+ keys.inject(translations) do |result, key|
+ key = key.to_sym
+ return nil unless result.is_a?(Hash) && result.has_key?(key)
+ result = result[key]
+ result = resolve(locale, key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
+ result
+ end
+ end
+ end
+
+ include Implementation
end
end
end
View
3  test/api/all_features_test.rb
@@ -11,8 +11,7 @@
end
class I18nAllFeaturesApiTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Cache
include I18n::Backend::Metadata
include I18n::Backend::Cascade
View
3  test/api/cascade_test.rb
@@ -4,8 +4,7 @@
require 'api'
class I18nCascadeApiTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Cascade
end
View
3  test/api/fallbacks_test.rb
@@ -4,8 +4,7 @@
require 'api'
class I18nFallbacksApiTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end
View
3  test/api/memoize_test.rb
@@ -16,8 +16,7 @@ class I18nMemoizeBackendWithSimpleApiTest < Test::Unit::TestCase
include Tests::Api::Localization::Time
include Tests::Api::Localization::Procs
- class MemoizeBackend
- include I18n::Backend::Base
+ class MemoizeBackend < I18n::Backend::Simple
include I18n::Backend::Memoize
end
View
3  test/api/pluralization_test.rb
@@ -4,8 +4,7 @@
require 'api'
class I18nPluralizationApiTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Pluralization
end
View
3  test/backend/cache_test.rb
@@ -9,8 +9,7 @@
else
class I18nBackendCacheTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Cache
end
View
3  test/backend/cascade_test.rb
@@ -3,8 +3,7 @@
require 'test_helper'
class I18nBackendCascadeTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Cascade
end
View
3  test/backend/cldr_test.rb
@@ -13,8 +13,7 @@
require 'date'
class I18nBackendCldrTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Cldr
end
View
9 test/backend/fallbacks_test.rb
@@ -3,8 +3,7 @@
require 'test_helper'
class I18nBackendFallbacksTranslateTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end
@@ -57,8 +56,7 @@ def setup
end
class I18nBackendFallbacksLocalizeTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end
@@ -90,8 +88,7 @@ def setup
end
class I18nBackendFallbacksWithChainTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end
View
3  test/backend/interpolation_compiler_test.rb
@@ -82,8 +82,7 @@ def test_handles_weird_strings
end
class I18nBackendInterpolationCompilerTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::InterpolationCompiler
end
View
3  test/backend/memoize_test.rb
@@ -4,8 +4,7 @@
require 'backend/simple_test'
class I18nBackendMemoizeTest < I18nBackendSimpleTest
- class MemoizeBackend
- include I18n::Backend::Base
+ class MemoizeBackend < I18n::Backend::Simple
include I18n::Backend::Memoize
end
View
3  test/backend/metadata_test.rb
@@ -3,8 +3,7 @@
require 'test_helper'
class I18nBackendMetadataTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Metadata
end
View
3  test/backend/pluralization_test.rb
@@ -3,8 +3,7 @@
require 'test_helper'
class I18nBackendPluralizationTest < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Pluralization
include I18n::Backend::Fallbacks
end
View
10 test/backend/simple_test.rb
@@ -9,12 +9,11 @@ def setup
end
# useful because this way we can use the backend with no key for interpolation/pluralization
- test "simple backend lookup: given nil as a key it returns nil" do
- assert_nil I18n.backend.send(:lookup, :en, nil)
+ test "simple backend translate: given nil as a key it still interpolations the default value" do
+ assert_equal "Hi David", I18n.t(nil, :default => "Hi %{name}", :name => "David")
end
# loading translations
-
test "simple load_translations: given an unknown file type it raises I18n::UnknownFileType" do
assert_raise(I18n::UnknownFileType) { I18n.backend.load_translations("#{locales_dir}/en.xml") }
end
@@ -22,6 +21,11 @@ def setup
test "simple load_translations: given a Ruby file name it does not raise anything" do
assert_nothing_raised { I18n.backend.load_translations("#{locales_dir}/en.rb") }
end
+
+ test "simple load_translations: given no argument, it uses I18n.load_path" do
+ I18n.backend.load_translations
+ assert_equal({ :en => { :foo => { :bar => 'baz' } } }, I18n.backend.send(:translations))
+ end
test "simple load_rb: loads data from a Ruby file" do
data = I18n.backend.send(:load_rb, "#{locales_dir}/en.rb")
View
6 test/backend/transliterator_test.rb
@@ -4,12 +4,8 @@
class I18nBackendTransliterator < Test::Unit::TestCase
- class Backend
- include I18n::Backend::Base
- end
-
def setup
- I18n.backend = Backend.new
+ I18n.backend = I18n::Backend::Simple.new
@proc = lambda { |n| n.upcase }
@hash = { :"ü" => "ue", :"ö" => "oe" }
@transliterator = I18n::Backend::Transliterator.get
View
3  test/gettext/backend_test.rb
@@ -5,8 +5,7 @@
class I18nGettextBackendTest < Test::Unit::TestCase
include I18n::Gettext::Helpers
- class Backend
- include I18n::Backend::Base
+ class Backend < I18n::Backend::Simple
include I18n::Backend::Gettext
end
Please sign in to comment.
Something went wrong with that request. Please try again.