Locale-aware person name formatting built on the Unicode CLDR person names specification.
A person name is represented as a Localize.PersonName.t/0
struct containing name parts (title, given name, surname, etc.)
plus optional locale and ordering preferences.
Formatting is driven by the locale's CLDR person name patterns,
which vary by length (:short, :medium, :long), usage
(:addressing, :referring, :monogram), formality (:formal,
:informal), and name order (:given_first, :surname_first,
:sorting).
Primary API
new/1— creates a validatedLocalize.PersonNamestruct.to_string/2— formats a person name as a string.to_string!/2— formats a person name, raising on error.to_iodata/2— formats a person name as iodata.to_iodata!/2— formats a person name as iodata, raising on error.
Integrating existing structs
Any struct can participate in person name formatting in two ways:
Implement the
Localize.PersonName.Convertibleprotocol — a single function that returns aLocalize.PersonNamestruct. Recommended for most cases, including third-party structs.Implement the
Localize.PersonNamebehaviour — eleven callbacks on the struct's module, each returning one name field. Use this when the struct's module is under your control and you want individual name fields exposed as module functions.
When the formatter receives a struct, it first looks for a
Convertible protocol implementation; if none is found, it falls
back to the behaviour callbacks via cast_to_person_name/1.
Summary
Types
Standard error response.
Valid :formality option.
Valid :format option.
An option to to_string/2 and to_iodata/2.
A keyword list of options for to_string/2 and to_iodata/2.
Valid :order option.
A PersonName struct containing the fields supported for person name formatting.
Valid :usage option.
Callbacks
Return the credentials as a String.t/0 or nil for the given struct.
Return the generation as a String.t/0 or nil for the given struct.
Return the given name as a String.t/0 or nil for the given struct.
Return the informal given name as a String.t/0 or nil for the given struct.
Return the locale or nil for the given struct.
Return the other given names as a String.t/0 or nil for the given struct.
Return the other surnames as a String.t/0 or nil for the given struct.
Return the preferred name order for the given struct.
Return the surname as a String.t/0 or nil for the given struct.
Return the surname prefix as a String.t/0 or nil for the given struct.
Return the title as a String.t/0 or nil for the given struct.
Functions
Casts any struct that implements the Localize.PersonName
behaviour into a Localize.PersonName.t/0 struct.
Returns a Localize.PersonName.t/0 struct crafted
from a keyword list of attributes.
Returns a formatted person name as iodata.
Same as to_iodata/2 but raises on error.
Returns a formatted person name as a string.
Same as to_string/2 but raises on error.
Types
Standard error response.
@type formality() :: :formal | :informal
Valid :formality option.
@type format() :: :short | :medium | :long
Valid :format option.
@type format_option() :: {:format, format()} | {:order, name_order()} | {:usage, usage()} | {:formality, formality()} | {:locale, Localize.LanguageTag.t() | atom() | String.t()} | {:locale_switching, boolean()}
An option to to_string/2 and to_iodata/2.
@type format_options() :: [format_option()]
A keyword list of options for to_string/2 and to_iodata/2.
@type name_order() :: :given_first | :surname_first | :sorting
Valid :order option.
@type t() :: %Localize.PersonName{ credentials: String.t() | nil, generation: String.t() | nil, given_name: String.t() | nil, informal_given_name: String.t() | nil, locale: Localize.LanguageTag.t() | nil, other_given_names: String.t() | nil, other_surnames: String.t() | nil, preferred_order: name_order() | nil, surname: String.t() | nil, surname_prefix: String.t() | nil, title: String.t() | nil }
A PersonName struct containing the fields supported for person name formatting.
@type usage() :: :addressing | :referring | :monogram
Valid :usage option.
Callbacks
Return the credentials as a String.t/0 or nil for the given struct.
Return the generation as a String.t/0 or nil for the given struct.
Return the given name as a String.t/0 or nil for the given struct.
Return the informal given name as a String.t/0 or nil for the given struct.
@callback locale(name :: struct()) :: Localize.LanguageTag.t() | nil
Return the locale or nil for the given struct.
Return the other given names as a String.t/0 or nil for the given struct.
Return the other surnames as a String.t/0 or nil for the given struct.
@callback preferred_order(name :: struct()) :: name_order()
Return the preferred name order for the given struct.
Return the surname as a String.t/0 or nil for the given struct.
Return the surname prefix as a String.t/0 or nil for the given struct.
Return the title as a String.t/0 or nil for the given struct.
Functions
Casts any struct that implements the Localize.PersonName
behaviour into a Localize.PersonName.t/0 struct.
Arguments
structis any struct that implements theLocalize.PersonNamebehaviour.
Returns
- A
Localize.PersonName.t/0struct.
@spec new(attributes :: Keyword.t()) :: {:ok, t()} | {:error, error_message()}
Returns a Localize.PersonName.t/0 struct crafted
from a keyword list of attributes.
Arguments
attributesis a keyword list of person name attributes that is used to construct aLocalize.PersonName.t/0.
Options
:given_nameis a person's given name. This is a required attribute. The value is anyString.t/0.:titleis a person's title such as "Mr." or "Dr.".:other_given_namesis anyString.t/0ornil. The default isnil.:informal_given_nameis anyString.t/0ornil. The default isnil.:surname_prefixis anyString.t/0ornil. The default isnil.:surnameis anyString.t/0ornil. The default isnil.:other_surnamesis anyString.t/0ornil. The default isnil.:generationis anyString.t/0ornil. The default isnil.:credentialsis anyString.t/0ornil. The default isnil.:localeis a locale identifier orLocalize.LanguageTag.t/0ornil. The default isnil.:preferred_orderis one of:given_first,:surname_firstor:sorting. The default isnil, meaning that the name order is derived from the name's locale and the formatting locale.
Returns
{:ok, person_name_struct}or{:error, reason}.
Examples
iex> Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
{:ok,
%Localize.PersonName{
title: "Mr.",
given_name: "José",
other_given_names: nil,
informal_given_name: nil,
surname_prefix: nil,
surname: "Valim",
other_surnames: nil,
generation: nil,
credentials: "Ph.D.",
preferred_order: nil,
locale: %Localize.LanguageTag{
language: :pt,
language_subtags: [],
script: :Latn,
territory: :BR,
language_variants: [],
locale: %{},
transform: %{},
extensions: %{},
private_use: [],
requested_locale_id: "pt",
canonical_locale_id: "pt",
cldr_locale_id: :pt
}
}}
iex> Localize.PersonName.new(surname: "Valim")
{:error, "Person Name requires at least a :given_name"}
@spec to_iodata(person_name :: struct(), options :: format_options()) :: {:ok, :erlang.iodata()} | {:error, error_message()}
Returns a formatted person name as iodata.
Accepts the same arguments and options as to_string/2.
Returns
{:ok, iodata}or{:error, reason}.
Examples
iex> {:ok, jose} = Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
iex> Localize.PersonName.to_iodata(jose)
{:ok, ["José"]}
iex> {:ok, jose} = Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
iex> Localize.PersonName.to_iodata(jose, format: :long, formality: :formal)
{:ok, ["Mr.", " ", "Valim"]}
@spec to_iodata!(person_name :: struct(), options :: format_options()) :: :erlang.iodata() | no_return()
Same as to_iodata/2 but raises on error.
@spec to_string(name :: struct(), options :: format_options()) :: {:ok, String.t()} | {:error, error_message()}
Returns a formatted person name as a string.
Arguments
person_nameis any struct that implements theLocalize.PersonNamebehaviour, including the nativeLocalize.PersonName.t/0struct.optionsis a keyword list of options.
Options
:formatis the relative length of a formatted name. The valid values are:short,:mediumand:long. The default is derived from the formatting locale's preferences.:usageindicates how the formatted name is used. The valid values are:addressing,:referringor:monogram. The default is:addressing.:formalityindicates the formality of usage. The valid values are:formaland:informal. The default is derived from the formatting locale's preferences.:orderexpresses preference for name part order. The valid values are:given_first,:surname_firstand:sorting. The default is based on the person name struct and the formatting locale.:localeis a locale identifier orLocalize.LanguageTag.t/0. The default isLocalize.get_locale().:locale_switchingwhentrue, switches the formatting locale to match the name's script when they differ. For example, a Latin-script name formatted in a Japanese locale will use Latin-based formatting patterns. The default isfalsefor compatibility with the CLDR test data. SeeTODO.mdfor details.
Returns
{:ok, formatted_name}or{:error, reason}.
Examples
iex> {:ok, jose} = Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
iex> Localize.PersonName.to_string(jose)
{:ok, "José"}
iex> {:ok, jose} = Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
iex> Localize.PersonName.to_string(jose, format: :long, formality: :formal, usage: :referring)
{:ok, "Mr. José Valim Ph.D."}
@spec to_string!(name :: struct(), options :: format_options()) :: String.t() | no_return()
Same as to_string/2 but raises on error.
Examples
iex> {:ok, jose} = Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
iex> Localize.PersonName.to_string!(jose)
"José"
iex> {:ok, jose} = Localize.PersonName.new(title: "Mr.", given_name: "José", surname: "Valim", credentials: "Ph.D.", locale: "pt")
iex> Localize.PersonName.to_string!(jose, format: :long, formality: :formal, usage: :referring)
"Mr. José Valim Ph.D."