Skip to content

Commit

Permalink
Respect available locales set in application
Browse files Browse the repository at this point in the history
If the application has set

    Rails.application.config.i18n.available_locales

we use them for Alchemy::I81n.available_locales and
only scan the locale files if not.
  • Loading branch information
tvdeyen committed May 24, 2023
1 parent 945b753 commit 7f663b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/alchemy/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def translate(msg, **options)
end

def available_locales
@@available_locales ||= nil
@@available_locales || translation_files.collect { |f|
f.match(LOCALE_FILE_PATTERN)[1].to_sym
}.uniq.sort
Rails.application.config.i18n.available_locales || begin
@@available_locales ||= nil
@@available_locales || translation_files.collect { |f|
f.match(LOCALE_FILE_PATTERN)[1].to_sym
}.uniq.sort
end
end

def available_locales=(locales)
Expand Down
7 changes: 7 additions & 0 deletions spec/libraries/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ module Alchemy
it { is_expected.to be_a Array }
it { is_expected.to include(:en) }

context "when locales are already set in application available_locales" do
it "returns them" do
expect(Rails.application.config.i18n).to receive(:available_locales) { [:de, :it] }
is_expected.to match_array([:de, :it])
end
end

context "when locales are already set in @@available_locales" do
before { I18n.class_variable_set(:@@available_locales, [:kl, :jp]) }
it { is_expected.to match_array([:kl, :jp]) }
Expand Down

0 comments on commit 7f663b8

Please sign in to comment.