Skip to content

Commit

Permalink
Merge pull request #2537 from jhawthorn/translate_countries
Browse files Browse the repository at this point in the history
Use carmen to translate available_countries helper
  • Loading branch information
jhawthorn committed Feb 8, 2018
2 parents 2fcabc9 + 910e7cd commit b84e28b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion core/app/helpers/spree/base_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'carmen'

module Spree
module BaseHelper
def link_to_cart(text = nil)
Expand Down Expand Up @@ -113,8 +115,14 @@ def available_countries
countries = Country.all
end

country_names = Carmen::Country.all.map do |country|
[country.code, country.name]
end.to_h

country_names.update I18n.t('spree.country_names', default: {}).stringify_keys

countries.collect do |country|
country.name = t(country.iso, scope: 'spree.country_names', default: country.name)
country.name = country_names.fetch(country.iso, country.name)
country
end.sort_by { |c| c.name.parameterize }
end
Expand Down
3 changes: 3 additions & 0 deletions core/lib/spree/core/controller_helpers/common.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'carmen'

module Spree
module Core
module ControllerHelpers
Expand Down Expand Up @@ -49,6 +51,7 @@ def set_user_language
locale ||= Rails.application.config.i18n.default_locale
locale ||= I18n.default_locale
I18n.locale = locale
Carmen.i18n_backend.locale = locale
end

# Returns which layout to render.
Expand Down
4 changes: 4 additions & 0 deletions core/spec/helpers/base_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
it "return complete list of countries" do
expect(available_countries.count).to eq(Spree::Country.count)
end

it "uses locales for country names" do
expect(available_countries).to include(having_attributes(name: "United States of America"))
end
end

context "with a checkout zone defined" do
Expand Down

0 comments on commit b84e28b

Please sign in to comment.