Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for reseting available locales Cache on load_path changes #272

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/i18n/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def available_locales_set #:nodoc:
def available_locales=(locales)
@@available_locales = Array(locales).map { |locale| locale.to_sym }
@@available_locales = nil if @@available_locales.empty?
@@available_locales_set = nil
reset_available_locales_set
end

# Returns the current default scope separator. Defaults to '.'
Expand Down Expand Up @@ -110,12 +110,14 @@ def missing_interpolation_argument_handler=(exception_handler)
# register translation files like this:
# I18n.load_path << 'path/to/locale/en.yml'
def load_path
reset_available_locales_set
@@load_path ||= []
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_path= must be defined as a method, and in case of resetting it should trigger reset of available locales.

end

# Sets the load path instance. Custom implementations are expected to
# behave like a Ruby Array.
def load_path=(load_path)
reset_available_locales_set
@@load_path = load_path
end

Expand All @@ -128,5 +130,11 @@ def enforce_available_locales
def enforce_available_locales=(enforce_available_locales)
@@enforce_available_locales = enforce_available_locales
end

private

def reset_available_locales_set
@@available_locales_set = nil
end
end
end
7 changes: 7 additions & 0 deletions test/i18n/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ def setup
test "adding arrays of filenames to the load path does not break locale loading" do
I18n.load_path << Dir[locales_dir + '/*.{rb,yml}']
assert_equal "baz", I18n.t(:'foo.bar')
# should include other locales
assert_equal true, I18n.config.available_locales.include?(:pt)
end

test "available locale set reset after changing load path, should include pt" do
I18n.load_path = Dir[locales_dir + '/*.{rb,yml}']
assert_equal true,I18n.config.available_locales.include?(:pt)
end
end
3 changes: 3 additions & 0 deletions test/test_data/locales/pt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# encoding: utf-8

{ :en => { :fuh => { :bah => "bax" } } }
3 changes: 3 additions & 0 deletions test/test_data/locales/pt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pt:
foo:
bar: bax