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.

Localize country name in admin address display (#14224)

Damian Legawiec2d564dcdaffe
diff --git a/packages/dashboard-ui/src/spree/address-block.tsx b/packages/dashboard-ui/src/spree/address-block.tsx
index 14b18d90af..5ae3b08022 100644
--- a/packages/dashboard-ui/src/spree/address-block.tsx
+++ b/packages/dashboard-ui/src/spree/address-block.tsx
@@ -13,10 +13,32 @@ export interface AddressBlockValue {
city?: string | null
state_text?: string | null
postal_code?: string | null
+ country_iso?: string | null
country_name?: string | null
phone?: string | null
}
+/**
+ * Resolves a country name localized to the admin UI language via
+ * `Intl.DisplayNames`, falling back to the API-provided name (then the ISO
+ * code) when the ISO is missing or the runtime lacks coverage.
+ */
+function localizedCountryName(
+ locale: string,
+ iso?: string | null,
+ name?: string | null,
+): string | null {
+ if (iso) {
+ try {
+ const localized = new Intl.DisplayNames([locale, 'en'], { type: 'region' }).of(iso)
+ if (localized) return localized
+ } catch {
+ // Runtime without Intl.DisplayNames coverage — fall back below.
+ }
+ }
+ return name ?? iso ?? null
+}
+
export function AddressBlock({
title,
address,
@@ -24,7 +46,7 @@ export function AddressBlock({
title: string
address: AddressBlockValue | null | undefined
}) {
- const { t } = useTranslation()
+ const { t, i18n } = useTranslation()
return (
<div>
<h6 className="font-semibold text-sm mb-1.5">{title}</h6>
@@ -37,7 +59,9 @@ export function AddressBlock({
<div>
{[address.city, address.state_text, address.postal_code].filter(Boolean).join(', ')}
</div>
- <div>{address.country_name}</div>
+ <div>
+ {localizedCountryName(i18n.language, address.country_iso, address.country_name)}
+ </div>
{address.phone && <div>{address.phone}</div>}
</div>
) : (
diff --git a/spree/admin/app/views/spree/admin/shared/_address.html.erb b/spree/admin/app/views/spree/admin/shared/_address.html.erb
index 1c40824fe9..8c7e2b6af1 100644
--- a/spree/admin/app/views/spree/admin/shared/_address.html.erb
+++ b/spree/admin/app/views/spree/admin/shared/_address.html.erb
@@ -23,7 +23,7 @@
<span class="locality"><%= address.city %></span>
<span class="region"><%= address.state_text %></span>
<span class="postal-code"><%= address.zipcode %></span>
- <div class="country-name"><%= address.country.try(:name) %></div>
+ <div class="country-name"><%= localized_country_name(address.country) if address.country %></div>
</div>
</div>
<% unless address.phone.blank? %>