Skip to content

Commit

Permalink
Correct country name localization error
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustMiller committed Aug 14, 2024
1 parent b740267 commit 5aa85a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 14 additions & 3 deletions docs/5.x/reference/element-types/addresses.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Expanding upon our previous example, we could output a nicely organized list of
Either repository’s `getList()` method is a shortcut that returns only key-value pairs, suitable for our examples—it also accepts an array of “parent” groups (beginning with country code) to narrow the subdivisions.

::: tip
These designations are deliberately generic, and won’t generally be recognized by users. Check out the labels section for information on how to output localized or context-aware names for each level (i.e. _Provinces_ in Canada or _States_ and _Counties_ in the United States).
These designations are deliberately generic, and won’t generally be recognized by users. Check out the [labels](#attribute-labels) section for information on how to output localized or context-aware names for each level (i.e. _Provinces_ in Canada or _States_ and _Counties_ in the United States).
:::

You may supplement the subdivision data provided by the [upstream repository](https://github.com/commerceguys/addressing) by listening to the <craft5:craft\services\Addresses::EVENT_DEFINE_ADDRESS_SUBDIVISIONS> event in a plugin or module. Similarly, deeper customization of the required [fields](#fields-and-formatting) (and those fields’ [labels](#attribute-labels)) may require modifying the defaults via the [EVENT_DEFINE_USED_SUBDIVISION_FIELDS](craft5:craft\services\Addresses::EVENT_DEFINE_USED_SUBDIVISION_FIELDS) or [EVENT_DEFINE_FIELD_LABEL](craft5:craft\services\Addresses::EVENT_DEFINE_FIELD_LABEL) events.
Expand Down Expand Up @@ -256,7 +256,7 @@ The default formatter includes the following options:

#### Country Names

Only the two-letter “country code” is stored on addresses. To display the full country name (localized for the viewer), use the attached [`Country`](repo:commerceguys/addressing/blob/master/src/Country/Country.php) model: <Since ver="5.3.0" feature="Address.getCountry()" />
Only the two-letter “country code” is stored on addresses. To display the full country name, use the attached [`Country`](repo:commerceguys/addressing/blob/master/src/Country/Country.php) model: <Since ver="5.3.0" feature="Address.getCountry()" />

```twig
{% set country = address.country %}
Expand All @@ -282,6 +282,17 @@ In earlier versions of Craft, you must directly retrieve its definition from the
{{ country.name }}
```

To get the localized name of a country outside of a formatted address, you must re-fetch it from the address repository:

```twig{2}
{% set repo = craft.app.addresses.getCountryRepository() %}
{% set country = repo.get(entry.country, currentSite.locale) %}
{{ country.name }}
{# -> In a site set to use US English (en-US): "United States" #}
{# -> In a site set to use Swiss French (fr-CH): "États-Unis" #}
```

### Customizing the Formatter

You can also pass your own formatter to the `|address` filter. The addressing library includes [PostalLabelFormatter](https://github.com/commerceguys/addressing/blob/master/src/Formatter/PostalLabelFormatter.php) to make it easier to print shipping labels. Here, we can specify that formatter and set its additional `origin_country` option:
Expand Down Expand Up @@ -413,7 +424,7 @@ return [
```

::: warning
The default formatter is used in the control panel as well as your templates, so make sure it includes all the information required for administrators to act on users’ information!
The default formatter is used in the control panel as well as your templates, so make sure it includes all the information required for administrators to act on users’ information! Complete address data is always be available when viewing or editing it in a slideout.
:::

## Managing Addresses
Expand Down
17 changes: 16 additions & 1 deletion docs/5.x/reference/field-types/country.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Craft stores the field’s value as a capitalized, two-letter country code.
{% endif %}
```

Craft actually returns a [`Country`](repo:commerceguys/addressing/blob/master/src/Country/Country.php) object, which has a few [additional features](../element-types/addresses.md#country-names), including the complete name (localized for the current site): <Since ver="5.3.0" description="{product} {ver} changed the data type for Country fields." />
Craft actually returns a [`Country`](repo:commerceguys/addressing/blob/master/src/Country/Country.php) object, which has a few [additional features](../element-types/addresses.md#country-names), including the complete name: <Since ver="5.3.0" description="{product} {ver} changed the data type for Country fields." />

```twig
<h1>Mail from {{ entry.country.name }}</h1>
Expand All @@ -47,6 +47,21 @@ In earlier versions of Craft, use the [address repository](../element-types/addr

The `country` variable in this example is an instance of [`CommerceGuys\Addressing\Country\Country`](repo:commerceguys/addressing/blob/master/src/Country/Country.php).

#### Localization

To get the localized name of a country, you must re-fetch it from the address repository:

```twig{2}
{% set repo = craft.app.addresses.getCountryRepository() %}
{% set country = repo.get(entry.country, currentSite.locale) %}
{{ country.name }}
{# -> In a site set to use US English (en-US): "United States" #}
{# -> In a site set to use Swiss French (fr-CH): "États-Unis" #}
```

Note that outputting the country object directly (i.e. `{{ country }}`) will always return the two-letter country code, which is standard across locales.

### Querying by Country

You can query for elements based on a country field’s value in a familiar way:
Expand Down

0 comments on commit 5aa85a4

Please sign in to comment.