mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
add #available_locales to I18n and I18n::SimpleBackend, flatten translations load_path when loading translations [#1714 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
@@ -45,6 +45,11 @@ module I18n
|
||||
Thread.current[:locale] = locale
|
||||
end
|
||||
|
||||
# Returns an array of locales for which translations are available
|
||||
def available_locales
|
||||
backend.available_locales
|
||||
end
|
||||
|
||||
# Sets the exception handler.
|
||||
def exception_handler=(exception_handler)
|
||||
@@exception_handler = exception_handler
|
||||
|
||||
@@ -69,6 +69,12 @@ module I18n
|
||||
@initialized ||= false
|
||||
end
|
||||
|
||||
# Returns an array of locales for which translations are available
|
||||
def available_locales
|
||||
init_translations unless initialized?
|
||||
translations.keys
|
||||
end
|
||||
|
||||
def reload!
|
||||
@initialized = false
|
||||
@translations = nil
|
||||
@@ -76,7 +82,7 @@ module I18n
|
||||
|
||||
protected
|
||||
def init_translations
|
||||
load_translations(*I18n.load_path)
|
||||
load_translations(*I18n.load_path.flatten)
|
||||
@initialized = true
|
||||
end
|
||||
|
||||
|
||||
@@ -124,6 +124,16 @@ class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
class I18nSimpleBackendAvailableLocalesTest < Test::Unit::TestCase
|
||||
def test_available_locales
|
||||
@backend = I18n::Backend::Simple.new
|
||||
@backend.store_translations 'de', :foo => 'bar'
|
||||
@backend.store_translations 'en', :foo => 'foo'
|
||||
|
||||
assert_equal ['de', 'en'], @backend.available_locales.map{|locale| locale.to_s }.sort
|
||||
end
|
||||
end
|
||||
|
||||
class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
|
||||
include I18nSimpleBackendTestSetup
|
||||
|
||||
@@ -472,6 +482,18 @@ class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
class I18nSimpleBackendLoadPathTest < Test::Unit::TestCase
|
||||
include I18nSimpleBackendTestSetup
|
||||
|
||||
def test_nested_load_paths_do_not_break_locale_loading
|
||||
@backend = I18n::Backend::Simple.new
|
||||
I18n.load_path = [[File.dirname(__FILE__) + '/locale/en.yml']]
|
||||
assert_nil backend_get_translations
|
||||
assert_nothing_raised { @backend.send :init_translations }
|
||||
assert_not_nil backend_get_translations
|
||||
end
|
||||
end
|
||||
|
||||
class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
|
||||
include I18nSimpleBackendTestSetup
|
||||
|
||||
|
||||
Reference in New Issue
Block a user