Skip to content

Locale

Functions related to languages and locales. The currently supported locales are: en, fr, nl, de, es, it, pt, da, fi.

describe_locale(locale \\ nil, opts \\ [])

Describe / parse the given locale and returns a map containing a language, region, label and region_label keys.

Options:

  • :locale - The output locale; defaults to the user's current locale.

Examples:

iex> describe_locale("en_US")
%{"language" => "en", "label" => "English", "region" => "US", "region_label" => "United States"}
iex> describe_locale("en_GB")
%{"language" => "en", "label" => "English", "region" => "GB", "region_label" => "United Kingdom"}
iex> describe_locale("ro")
%{"language" => "ro", "label" => "Romanian", "region" => nil, "region_label" => nil}

describe_locales(locales \\ [], opts \\ [])

Returns all the available languages from the list of locales of the current bot, if no list of locales is provided. The result is a list in which each item is a map containing a language, region, label and region_label key.

Options:

  • :locale - The output locale; defaults to the user's current locale.

Examples:

iex> describe_locales(["en_US"])
[%{"language" => "en", "label" => "English", "region" => "US", "region_label" => "United States"}]
iex> describe_locales(["en_GB"])
[%{"language" => "en", "label" => "English", "region" => "GB", "region_label" => "United Kingdom"}]
iex> describe_locales(["de_AT"], locale: "nl")
[%{"language" => "de", "label" => "Duits", "region" => "AT", "region_label" => "Oostenrijk"}]

extract_language(message)

Will extract the language from a given string and return the locale code."

Examples:

iex> extract_language("I want to speak French")
"fr"
iex> extract_language("Sprichst du Deutsch?")
"de"
iex> extract_language("Do you speak Italian?")
"it"

humanize_duration(value, opts)

Will format a time duration in a human form, spelling out the quarters and halves of hours.

Options:

  • :locale - supported locales are: "en", "fr", "de", "it", "es", "pt", "nl".
  • :format - describes the way numbers should be formatted. Available formats:
    • :standard, default, will represent the numbers as numbers: 1 will be formatted as "1".
    • :spellout, will spell the numbers in words: 1 will be formatted as "one".

Examples:

iex> humanize_duration(36, "hour", locale: "en")
"a day and a half"
iex> humanize_duration(1.5, "hour", locale: "en")
"an hour and a half"
iex> humanize_duration(150, "minute", locale: "en")
"2 and a half hours"
iex> humanize_duration(150, "minute", format: :spellout)
"two and a half hours"
iex> humanize_duration(1.5, "month", format: :spellout)
"a month and a half"
iex> humanize_duration(21.025, "month", format: :spellout)
"one year, eight months, and thirty-one days"
iex> humanize_duration(42, "month", format: :spellout)
"three and a half years"
iex> humanize_duration(0.5, "hour", locale: "nl")
"een half uur"
iex> humanize_duration(1.5, "hour", locale: "nl")
"anderhalf uur"
iex> humanize_duration(1.5, "minute", locale: "nl")
"anderhalve minuut"
iex> humanize_duration(1.575, "hour", locale: "nl", format: :spellout)
"een uur, vier­en­dertig minuten en dertig seconden"
iex> humanize_duration(0.5, "minute", locale: "nl")
"een halve minuut"
iex> humanize_duration(30, "minute", locale: "fr")
"une demi-heure"
iex> humanize_duration(18, "month", locale: "fr")
"un an et demi"
iex> humanize_duration(90, "minute", locale: "de")
"anderthalb Stunden"
iex> humanize_duration(150, "minute", locale: "de", format: :spellout)
"zweieinhalb Stunden"

humanize_locale(locale_to_humanize, opts \\ [])

Returns the name of a language in a specified language.

Options:

  • :locale - The output locale; defaults to the user's current locale.

Examples:

iex> humanize_locale("nl", locale: "nl")
"Nederlands"
iex> humanize_locale("en", locale: "fr")
"anglais"

locale_supported?(locale, available_locales \\ nil)

Returns true if the given locale is supported by the current bot.

When available_locales is left nil, it will default to the locales of the current bot. Otherwise, when it's a list, the function will check whether the language of the given locale matches one of the languages of the given locales.

number_format(amount, opts)

Formats a number into the locale-specific number format.

Options:

  • :locale - The output locale; defaults to the user's current locale.

For all other options, see the Cldr.Number documentation.

Examples:

iex> number_format(1000.01, locale: "nl")
"1.000,01"
iex> number_format(1000.99, locale: "en")
"1,000.99"
iex> number_format(9.99, locale: "nl", currency: :EUR, format: :currency)
"€ 9,99"
iex> number_format(1000.01, locale: "nl.FORMAL")
"1.000,01"