Parses unstructured address strings and formats structured addresses into locale-appropriate string representations.
Address parsing is powered by libpostal via NIF. Address formatting uses templates from the OpenCageData address-formatting project, compiled to Erlang term format.
The primary functions are parse/2 for parsing raw address strings
and to_string/2 for formatting an Address struct.
Summary
Functions
Returns whether the NIF backend is available.
Capitalizes the text fields of an address struct.
Parses an unstructured address string into a Localize.Address.Address struct.
Formats an address struct into a locale-appropriate string.
Types
@type t() :: Localize.Address.Address.t()
Functions
@spec available?() :: boolean()
Returns whether the NIF backend is available.
Returns
trueif the NIF shared library was loaded successfully.falseif the NIF is not compiled or libpostal is missing.
Examples
iex> is_boolean(Localize.Address.available?())
true
Capitalizes the text fields of an address struct.
Applies Unicode-aware titlecase to place names, street names, and administrative region fields. Postcodes are uppercased. House numbers, territory codes, and the raw input string are left unchanged.
Arguments
addressis aLocalize.Address.Addressstruct.
Options
:localeis a locale identifier passed through toUnicode.String.titlecase/2for locale-specific casing rules (e.g., Dutch "ij" → "IJ", Turkish dotted-I handling).
Returns
- A new
Localize.Address.Addressstruct with capitalized fields.
Examples
iex> address = %Localize.Address.Address{
...> road: "hamilton avenue",
...> city: "palo alto",
...> postcode: "sw1a 2aa"
...> }
iex> capitalized = Localize.Address.capitalize(address)
iex> capitalized.road
"Hamilton Avenue"
iex> capitalized.city
"Palo Alto"
iex> capitalized.postcode
"SW1A 2AA"
Parses an unstructured address string into a Localize.Address.Address struct.
Uses libpostal's machine-learning models to identify address components such as house number, road, city, state, and postcode.
Arguments
address_stringis the unstructured address string to parse.
Options
:territoryis an explicit ISO 3166-1 alpha-2 territory code (e.g.,"US","GB", or an atom like:US). Used as the territory code on the resulting struct and as context for parsing.:localeis a locale identifier used to derive the territory. Accepts a string (e.g.,"en-US"), an atom (e.g.,:en_US), or aLocalize.LanguageTag.t()struct.
When neither :territory nor :locale is given, the territory is
derived from Localize.get_locale/0.
Returns
{:ok, address}whereaddressis aLocalize.Address.Addressstruct.{:error, reason}if the address cannot be parsed.
Examples
iex> {:ok, address} = Localize.Address.parse("301 Hamilton Avenue, Palo Alto, CA 94303")
iex> address.house_number
"301"
iex> {:ok, address} = Localize.Address.parse("10 Downing Street, London", territory: "GB")
iex> address.territory_code
"GB"
Formats an address struct into a locale-appropriate string.
Uses OpenCageData address formatting templates to produce a properly formatted address for the territory associated with the address.
Arguments
addressis aLocalize.Address.Addressstruct.
Options
:territoryoverrides the territory code used for template selection. Defaults toaddress.territory_code.
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.to_string(address)
iex> is_binary(formatted)
true