Skip to content

Commit

Permalink
Merge pull request #2048 from AyuntamientoMadrid/i18n-load-path
Browse files Browse the repository at this point in the history
Load custom locales after everything is loaded
  • Loading branch information
Senen authored Jul 17, 2019
2 parents 7a1bc44 + ed62609 commit a951070
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Application < Rails::Application
"nl" => "en"
}

config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}")]
config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**[^custom]*", "*.{rb,yml}")]
config.i18n.load_path += Dir[Rails.root.join("config", "locales", "custom", "**", "*.{rb,yml}")]

config.after_initialize do
Expand Down
74 changes: 54 additions & 20 deletions spec/customization_engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,73 @@
# This module tests functionality related with custom application files
# TODO test models, controllers, etc...

describe "Customization Engine" do

let(:test_key) { I18n.t("account.show.change_credentials_link") }
describe "Check Custom Locales" do
let!(:default_path) { I18n.load_path }

before do
reset_load_path_and_reload(default_path)
end

after do
reset_load_path_and_reload(default_path)
end

it "loads custom and override original locales" do
increase_load_path_and_reload(Dir[Rails.root.join("spec", "support",
"locales", "custom", "*.{rb,yml}")])
expect(test_key).to eq "Overriden string with custom locales"
describe "Customization Engine" do
let(:test_key) { I18n.t("account.show.change_credentials_link") }

before do
reset_load_path_and_reload(default_path)
end

it "loads custom and override original locales" do
increase_load_path_and_reload(Dir[Rails.root.join("spec", "support",
"locales", "custom", "*.{rb,yml}")])
expect(test_key).to eq "Overriden string with custom locales"
end

it "does not override original locales" do
increase_load_path_and_reload(Dir[Rails.root.join("spec", "support",
"locales", "*.{rb,yml}")])
expect(test_key).to eq "Not overriden string with custom locales"
end

def increase_load_path_and_reload(path)
I18n.load_path += path
I18n.reload!
end
end

it "does not override original locales" do
increase_load_path_and_reload(Dir[Rails.root.join("spec", "support",
"locales", "*.{rb,yml}")])
expect(test_key).to eq "Not overriden string with custom locales"
describe "I18n config load_path" do

let(:test_key) { I18n.t("management.document_verifications.not_in_census") }

it "loads custom and override original locales" do
i18n_load_path_correctly

expect(test_key).to eq "This document is not registered in Madrid Census."
end

it "load original locales" do
i18n_load_path_wrongly

expect(test_key).to eq "This document is not registered in Madrid."
end

def i18n_load_path_correctly
I18n.load_path = Dir[Rails.root.join("config", "locales", "**[^custom]*", "*.{rb,yml}")]
I18n.load_path += Dir[Rails.root.join("config", "locales", "custom", "**", "*.{rb,yml}")]

I18n.reload!
end

def i18n_load_path_wrongly
I18n.load_path = Dir[Rails.root.join("config", "locales", "custom", "**", "*.{rb,yml}")]
I18n.load_path += Dir[Rails.root.join("config", "locales", "**[^custom]*", "*.{rb,yml}")]

I18n.reload!
end

end

def reset_load_path_and_reload(path)
I18n.load_path = path
I18n.reload!
end

def increase_load_path_and_reload(path)
I18n.load_path += path
I18n.reload!
end

end

0 comments on commit a951070

Please sign in to comment.