Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: resolve subdivisions the countries gem names with a locale tag (#14674)

Abdul Haseeb kamboh0b89171d135f
diff --git a/packages/dashboard-core/src/components/address-form-dialog.tsx b/packages/dashboard-core/src/components/address-form-dialog.tsx
index 4ae41f9528..d317b12862 100644
--- a/packages/dashboard-core/src/components/address-form-dialog.tsx
+++ b/packages/dashboard-core/src/components/address-form-dialog.tsx
@@ -226,8 +226,11 @@ export function AddressFormDialog({
}, [address?.country_code, storeDefaultCountryCode, form])
const countryCode = form.watch('country_code')
- const { states, statesRequired } = useCountryStates(countryCode)
- const useStateCombobox = statesRequired && states.length > 0
+ const { states } = useCountryStates(countryCode)
+ // Show the dropdown whenever the country enumerates subdivisions, not only
+ // when it strictly requires one — so a country like Pakistan still lets the
+ // user pick a province instead of typing a name that may not match.
+ const useStateCombobox = states.length > 0
async function onSubmit(values: AddressFormValues) {
try {
diff --git a/spree/core/lib/spree/iso_data.rb b/spree/core/lib/spree/iso_data.rb
index 6aa437f202..e02c1760b7 100644
--- a/spree/core/lib/spree/iso_data.rb
+++ b/spree/core/lib/spree/iso_data.rb
@@ -53,6 +53,13 @@ module Spree
}
}.freeze
+ # Some gem subdivision names are a placeholder the gem falls back to when it
+ # has no proper name for the entry: the value it would have translated,
+ # tagged with the locale it belongs to — Pakistan's "Sind (en)",
+ # "Baluchistan (en)". The clean name is the locale's own translation, so a
+ # name matching this is replaced rather than shown or matched against.
+ LOCALE_TAGGED_NAME = /\s\([a-z]{2}(?:-[a-z]{2})?\)\z/i
+
# Retired ISO codes mapped to their current equivalent. Reading an address
# written under the old code still finds the right subdivision; writing
# always stores the successor.
@@ -172,16 +179,28 @@ module Spree
# English deliberately reads the canonical +name+ rather than the +en+
# translation: the two disagree in places (the gem translates DC as
# "Washington DC", where the canonical name is "District of Columbia"),
- # and the canonical spelling is the one Spree has always stored.
+ # and the canonical spelling is the one Spree has always stored. The
+ # exception is a name the gem left as a locale-tagged placeholder
+ # ("Sind (en)"): that is not a spelling to preserve, so the clean English
+ # translation wins.
#
# Note the translation keys are symbols here, unlike +Country#translation+,
# which takes a string.
def subdivision_display_name(subdivision)
locale = I18n.locale.to_s.downcase
- return subdivision.name if locale.start_with?('en')
-
translations = subdivision.translations || {}
- translations[locale.to_sym].presence || subdivision.name
+ english = translations[:en].presence
+
+ if locale.start_with?('en')
+ name = subdivision.name
+ return name.match?(LOCALE_TAGGED_NAME) ? (english || name) : name
+ end
+
+ translated = translations[locale.to_sym].presence
+ return translated if translated
+
+ name = subdivision.name
+ name.match?(LOCALE_TAGGED_NAME) ? (english || name) : name
end
end
end
diff --git a/spree/core/spec/lib/spree/iso_data_spec.rb b/spree/core/spec/lib/spree/iso_data_spec.rb
index 6ad6b1215b..d9daddee63 100644
--- a/spree/core/spec/lib/spree/iso_data_spec.rb
+++ b/spree/core/spec/lib/spree/iso_data_spec.rb
@@ -46,6 +46,16 @@ RSpec.describe Spree::IsoData do
expect(described_class.subdivisions('HK')).to be_empty
end
+ # The gem ships some subdivision names as locale-tagged placeholders
+ # ("Sind (en)") instead of a real name; the clean name is its translation.
+ it 'cleans up names the gem left as locale-tagged placeholders' do
+ subdivisions = described_class.subdivisions('PK')
+
+ expect(subdivisions['SD']).to eq('Sindh')
+ expect(subdivisions['BA']).to eq('Balochistan')
+ expect(subdivisions.values.join(', ')).not_to include('(en)')
+ end
+
# The gem keys subdivision translations by symbol, unlike country
# translations, so a string lookup silently falls back to English.
context 'when the locale has subdivision translations' do
@@ -85,6 +95,14 @@ RSpec.describe Spree::IsoData do
expect(described_class.subdivision_code('US', 'california')).to eq('CA')
end
+ # Regression: the gem's placeholder name for Pakistan's Sindh ("Sind (en)")
+ # used to shadow the real one, so the correct spelling matched nothing.
+ it 'resolves a name the gem shipped as a locale-tagged placeholder' do
+ expect(described_class.subdivision_code('PK', 'Sindh')).to eq('SD')
+ expect(described_class.subdivision_code('PK', 'sindh')).to eq('SD')
+ expect(described_class.subdivision_code('PK', 'Balochistan')).to eq('BA')
+ end
+
it 'returns nil when nothing matches' do
expect(described_class.subdivision_code('US', 'Nonsense')).to be_nil
expect(described_class.subdivision_code('US', nil)).to be_nil