Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Person Name Formatting Explorer

person_name_formatting_explorer.livemd

Person Name Formatting Explorer

Mix.install(
  [
    {:ex_cldr_person_names, github: "elixir-cldr/cldr_person_names", env: :dev},
    :jason,
    :kino
  ],
  config: [
    ex_cldr: [default_backend: MyApp.Cldr]
  ],
  force: true
)

Formatting Parameters

locale =
  Kino.Input.select("Locale",
    en: "English",
    fr: "French",
    ja: "Japanese",
    zh: "Chinese"
  )
format =
  Kino.Input.select("Format",
    medium: "Medium",
    short: "Short",
    long: "Long"
  )
name_order =
  Kino.Input.select("Name order",
    given_first: "Given name first",
    surname_first: "Surname first",
    sorting: "Sorted order"
  )
usage =
  Kino.Input.select("Usage",
    addressing: "Addressing a person",
    referring: "Referring to a person",
    monogram: "Monogram"
  )
formality =
  Kino.Input.select("Formality",
    formal: "Formal",
    informal: "Informal"
  )

Selecting a name to format

random = Cldr.PersonName.Names.random()

names =
  Cldr.PersonName.Names.names()
  |> Enum.sort()
  |> Enum.map(fn {k, v} -> {v, to_string(k)} end)

names = [{random, "random"} | names]
name = Kino.Input.select("Name to format", names)
name = Kino.Input.read(name)

Format the name

locale = Kino.Input.read(locale)
name_order = Kino.Input.read(name_order)
format = Kino.Input.read(format)
usage = Kino.Input.read(usage)
formality = Kino.Input.read(formality)

MyApp.Cldr.PersonName.to_string(name,
  format: format,
  usage: usage,
  locale: locale,
  order: name_order,
  formality: formality
)

Formatting with all options

all_formats =
  for format <- [:short, :medium, :long],
      usage <- [:addressing, :referring],
      order <- [:given_first, :surname_first, :sorting],
      formality <- [:formal, :informal] do
    {:ok, formatted_name} =
      MyApp.Cldr.PersonName.to_string(name,
        format: format,
        usage: usage,
        locale: locale,
        order: name_order,
        formality: formality
      )

    %{
      "Format" => format,
      "Usage" => usage,
      "Name order" => order,
      "Formality" => formality,
      "Formatted" => formatted_name
    }
  end

Kino.DataTable.new(all_formats, keys: ["Format", "Usage", "Name order", "Formality", "Formatted"])
Mix.env()