Localize.Address.Formatter (Localize.Address v1.0.0)

Copy Markdown View Source

Formats a Localize.Address struct into a human-readable string using OpenCageData address formatting templates.

Templates must be downloaded and compiled before use by running mix localize.address.download_templates.

Summary

Functions

Formats an address struct as a string for the given territory.

Formats an address with additional component bindings.

Functions

format(address, territory_code)

@spec format(Localize.Address.Address.t(), String.t()) ::
  {:ok, String.t()} | {:error, String.t()}

Formats an address struct as a string for the given territory.

Arguments

  • address is a Localize.Address.Address struct.

  • territory_code is an ISO 3166-1 alpha-2 territory code string (e.g., "US", "GB").

Returns

  • {:ok, formatted_string} on success.

  • {:error, reason} if formatting fails.

Examples

iex> address = %Localize.Address.Address{
...>   house_number: "301",
...>   road: "Hamilton Avenue",
...>   city: "Palo Alto",
...>   state: "CA",
...>   postcode: "94303",
...>   territory: "United States of America",
...>   territory_code: "US"
...> }
iex> {:ok, formatted} = Localize.Address.Formatter.format(address, "US")
iex> is_binary(formatted)
true

format_with_bindings(address, extra_bindings, territory_code)

@spec format_with_bindings(Localize.Address.Address.t(), map(), String.t()) ::
  {:ok, String.t()} | {:error, String.t()}

Formats an address with additional component bindings.

Like format/2 but accepts a map of extra string-keyed bindings that supplement the struct fields. This is used when the caller has component values that don't map directly to struct fields (e.g., "suburb", "town", "pedestrian") but are referenced by templates.

Arguments

  • address is a Localize.Address.Address struct.

  • extra_bindings is a map of %{String.t() => String.t()} providing additional template variable values.

  • territory_code is an ISO 3166-1 alpha-2 territory code string.

Returns

  • {:ok, formatted_string} on success.

  • {:error, reason} if formatting fails.