Packages

ex_cldr

0.0.18
2.47.5 2.47.4 2.47.3 2.47.2 2.47.1 2.47.0 2.46.0 2.45.2 2.45.1 2.45.0 2.44.1 2.44.0 2.43.2 2.43.1 2.43.0 2.42.0 2.41.1 2.41.0 2.40.4 2.40.3 2.40.2 2.40.1 2.40.0 2.39.2 2.39.1 2.39.0 2.38.1 2.38.0 2.37.5 2.37.4 2.37.3 retired 2.37.2 2.37.1 2.37.0 2.36.0 2.35.1 2.35.0 retired 2.34.2 2.34.1 2.34.0 2.33.2 2.33.1 2.33.1-rc.0 2.33.0 2.32.1 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.1 2.27.0 retired 2.26.3 2.26.2 2.26.1 2.26.0 2.25.0 2.24.2 2.24.1 2.24.0 2.24.0-rc.6 2.24.0-rc.5 2.24.0-rc.4 2.24.0-rc.3 2.24.0-rc.2 2.24.0-rc.1 2.24.0-rc.0 2.23.2 2.23.1 2.23.0 2.22.1 2.22.0 2.21.0 2.20.0 2.20.0-rc.3 2.20.0-rc.2 2.20.0-rc.1 2.20.0-rc.0 2.19.1 2.19.0 2.18.2 2.18.1 2.18.0 2.18.0-rc.0 2.17.2 2.17.1 2.17.0 2.16.2 2.16.1 2.16.1-rc.0 2.16.0 2.15.0 2.14.1 2.14.0 2.14.0-rc.0 2.13.0 2.12.1 2.12.0 2.11.1 2.11.0 2.10.2 2.10.1 2.10.0 retired 2.9.0 2.8.1 2.8.0 2.7.2 2.7.1 2.7.0 2.6.2 2.6.1 2.6.0 2.5.0 2.4.3 2.4.2 2.4.1 2.4.0 2.3.2 2.3.1 2.3.0 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 2.2.1 2.2.0 2.1.0 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 2.0.0-rc.3 retired 2.0.0-rc.2 retired 2.0.0-rc.1 retired 2.0.0-rc.0 retired 1.8.2 1.8.1 1.8.0 1.7.1 1.7.0 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.2 1.5.1 1.5.0 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 retired 1.4.0-rc.3 retired 1.4.0-rc.2 retired 1.4.0-rc.1 retired 1.4.0-rc.0 retired 1.3.2 1.3.1 1.3.0 1.2.0 1.1.0 1.0.0 1.0.0-rc.3 retired 1.0.0-rc.2 retired 1.0.0-rc.1 retired 1.0.0-rc.0 retired 0.13.1 0.13.0 0.12.2 retired 0.12.1 retired 0.12.0 retired 0.11.0 retired 0.10.0 retired 0.9.0 retired 0.8.3 retired 0.8.2 retired 0.8.1 retired 0.8.0 retired 0.7.0 retired 0.6.2 retired 0.6.1 retired 0.6.0 retired 0.5.2 retired 0.5.1 retired 0.5.0 retired 0.4.2 retired 0.4.1 retired 0.4.0 retired 0.3.0 retired 0.2.1 retired 0.2.0 retired 0.1.3 retired 0.1.2 retired 0.1.1 retired 0.1.0 retired 0.0.20 retired 0.0.19 retired 0.0.18 retired 0.0.17 retired 0.0.16 retired 0.0.15 retired 0.0.14 retired 0.0.13 retired 0.0.12 retired 0.0.11 retired 0.0.10 retired 0.0.8 retired 0.0.7 retired 0.0.6 retired 0.0.5 retired 0.0.4 retired 0.0.3 retired

Common Locale Data Repository (CLDR) functions for Elixir to localize and format numbers, dates, lists, messages, languages, territories and units with support for over 700 locales for internationalized (i18n) and localized (L10N) applications.

Retired package: Deprecated

Current section

Files

Jump to
ex_cldr lib cldr.ex
Raw

lib/cldr.ex

defmodule Cldr do
@moduledoc """
Cldr provides functions to localise numbers, currencies, lists and
dates/times to an appropriate locale as defined by the CLDR data
maintained by the ICU.
The most commonly used functions are:
* `Cldr.Number.to_string/2` for formatting numbers
* `Cldr.Currency.to_string/2` for formatting currencies
* `Cldr.List.to_string/2` for formatting lists
"""
alias Cldr.Config
# TODO: Resolve this automatically from the repository data
# The version of the CLDR data repository.
@version {30,0,2}
def version do
@version
end
if Enum.any?(Config.unknown_locales()) do
raise Cldr.UnknownLocaleError,
"Some locales are configured that are not known to CLDR. " <>
"Compilation cannot continue until the configuration includes only " <>
"locales known in CLDR.\n\n" <>
"Configured locales: #{inspect Config.requested_locales()}\n" <>
"Gettext locales: #{inspect Config.gettext_locales()}\n" <>
"Unknown locales: " <>
"#{IO.ANSI.red()}#{inspect Config.unknown_locales()}" <>
"#{IO.ANSI.default_color()}\n"
end
@warn_if_greater_than 100
@known_locale_count Enum.count(Config.known_locales())
@locale_string if @known_locale_count > 1, do: "locales ", else: "locale "
IO.puts "Generating Cldr for #{@known_locale_count} " <>
@locale_string <>
"#{inspect Config.known_locales, limit: 5} with " <>
"default locale #{inspect Config.default_locale()}"
if @known_locale_count > @warn_if_greater_than do
IO.puts "Please be patient, generating functions for many locales " <>
"can take some time"
end
@doc """
Returns the directory path name where the CLDR json data
is kept.
"""
@data_dir Config.client_data_dir()
def data_dir do
@data_dir
end
@doc """
Return the current locale to be used for `Cldr` functions that
take an optional locale parameter for which a locale is not supplied.
"""
@spec get_locale :: Locale.t
def get_locale do
Process.get(:cldr, default_locale())
end
@doc """
Set the current locale to be used for `Cldr` functions that
take an optional locale parameter for which a locale is not supplied.
"""
@spec set_locale(Locale.t) :: Locale.t
def set_locale(locale) when is_binary(locale) do
if known_locale?(locale) do
Process.put(:cldr, locale)
locale
else
raise Cldr.UnknownLocaleError,
"The requested locale #{inspect locale} is not known."
end
end
@doc """
Returns the default `locale` name.
## Example
iex> Cldr.get_locale()
"en"
"""
@default_locale Config.default_locale()
@spec default_locale :: [Locale.t]
def default_locale do
@default_locale
end
@doc """
Returns a list of all the locales defined in the CLDR
repository.
Note that not necessarily all of these locales are
available since functions are only generated for configured
locales which is most cases will be a subset of locales
defined in CLDR.
See also: `requested_locales/0` and `known_locales/0`
"""
@all_locales Config.all_locales()
@spec all_locales :: [Locale.t]
def all_locales do
@all_locales
end
@doc """
Returns a list of all requested locales.
The list is the combination of configured locales,
`Gettext` locales and the default locale.
See also `known_locales/0` and `all_locales/0`
"""
@requested_locales Config.requested_locales()
@spec requested_locales :: [Locale.t] | []
def requested_locales do
@requested_locales
end
@doc """
Returns a list of the known locales.
Known locales are those locales which
are the subset of all CLDR locales that
have been configured for use either
directly in the `config.exs` file or
in `Gettext`.
"""
@known_locales Config.known_locales()
@spec known_locales :: [Locale.t] | []
def known_locales do
@known_locales
end
@doc """
Returns a list of the locales that are configured, but
not known in CLDR.
Since we check at compile time for any unknown locales
and raise and exception this function should always
return an empty list.
"""
@unknown_locales Config.unknown_locales()
@spec unknown_locales :: [Locale.t] | []
def unknown_locales do
@unknown_locales
end
@doc """
Returns a boolean indicating if the specified locale
is configured and available in Cldr.
## Examples
iex> Cldr.known_locale?("en")
true
iex> Cldr.known_locale?("!!")
false
"""
@spec known_locale?(Locale.t) :: boolean
def known_locale?(locale) when is_binary(locale) do
!!Enum.find(known_locales(), &(&1 == locale))
end
@doc """
Returns a boolean indicating if the specified locale
is available in CLDR.
The return value depends on whether the locale is
defined in the CLDR repository. It does not necessarily
mean the locale is configured for Cldr. See also
`Cldr.known_locale?/1`.
## Examples
iex> Cldr.locale_exists? "en-AU"
true
iex> Cldr.locale_exists? "en-SA"
false
"""
@spec locale_exists?(Locale.t) :: boolean
def locale_exists?(locale) when is_binary(locale) do
!!Enum.find(Config.all_locales(), &(&1 == locale))
end
end