Packages
ex_cldr
2.26.3
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.
Current section
Files
Jump to
Current section
Files
lib/cldr/backend/locale.ex
defmodule Cldr.Locale.Backend do
@moduledoc false
def define_locale_backend(config) do
quote location: :keep, bind_quoted: [config: Macro.escape(config)] do
defmodule Locale do
@moduledoc false
if Cldr.Config.include_module_docs?(config.generate_docs) do
@moduledoc """
Backend module that provides functions
to define new locales and display human-readable
locale names for presentation purposes.
"""
end
alias Cldr.{Locale, LanguageTag}
def new(locale_name), do: Locale.new(locale_name, unquote(config.backend))
def new!(locale_name), do: Locale.new!(locale_name, unquote(config.backend))
@doc """
Returns the territory from a language tag or
locale name.
## Arguments
* `locale` is any language tag returned by
`#{inspect(__MODULE__)}.new/1`
or a locale name in the list returned by
`#{inspect(config.backend)}.known_locale_names/0`
## Returns
* A territory code as an atom
## Examples
iex> #{inspect(__MODULE__)}.territory_from_locale "en-US"
:US
iex> #{inspect(__MODULE__)}.territory_from_locale "en-US-u-rg-GBzzzz"
:GB
"""
@spec territory_from_locale(LanguageTag.t() | Locale.locale_name()) ::
Locale.territory_code()
@doc since: "2.18.2"
def territory_from_locale(%LanguageTag{} = locale) do
Locale.territory_from_locale(locale)
end
def territory_from_locale(locale) do
Locale.territory_from_locale(locale, unquote(config.backend))
end
@doc """
Returns the time zone from a language tag or
locale name.
## Arguments
* `locale` is any language tag returned by
`#{inspect(__MODULE__)}.new/1`
or a locale name in the list returned by
`#{inspect(config.backend)}.known_locale_names/0`
## Returns
* A time zone ID as a string or
* `:error` if no time zone can be determined
## Examples
iex> #{inspect(__MODULE__)}.timezone_from_locale "en-US-u-tz-ausyd"
"Australia/Sydney"
"""
@doc since: "2.19.0"
@spec timezone_from_locale(LanguageTag.t() | Locale.locale_name()) ::
String.t() | {:error, {module(), String.t()}}
def timezone_from_locale(%LanguageTag{} = locale) do
Locale.timezone_from_locale(locale)
end
def timezone_from_locale(locale) do
Locale.timezone_from_locale(locale, unquote(config.backend))
end
@doc """
Returns the "best fit" locale for a given territory.
Using the population percentage data from CLDR, the
language most commonly spoken in the given territory
is used to form a locale name which is then validated
against the given backend.
First a territory-specific locale is validated and if
that fails, the base language only is validate.
For example, if the territory is `AU` then then the
language most spoken is "en". First, the locale "en-AU"
is validated and if that fails, "en" is validated.
## Arguments
* `territory` is any ISO 3166 Alpha-2 territory
code that can be validated by `Cldr.validate_territory/1`
## Returns
* `{:ok, language_tag}` or
* `{:error, {exception, reason}}`
## Examples
iex> #{inspect(__MODULE__)}.locale_for_territory(:AU)
#{config.backend}.validate_locale(:"en-AU")
iex> #{inspect(__MODULE__)}.locale_for_territory(:US)
#{config.backend}.validate_locale(:"en-US")
iex> #{inspect(__MODULE__)}.locale_for_territory(:ZZ)
{:error, {Cldr.UnknownTerritoryError, "The territory :ZZ is unknown"}}
"""
@doc since: "2.26.0"
@spec locale_for_territory(Locale.territory_code()) ::
{:ok, LanguageTag.t()} | {:error, {module(), String.t()}}
def locale_for_territory(territory) do
Locale.locale_for_territory(territory)
end
@doc """
Returns a "best fit" locale for a host name.
## Arguments
* `host` is any valid host name
* `options` is a keyword list of options. The default
is `[]`.
## Options
* `:tlds` is a list of territory codes as upper-cased
atoms that are to be considered as top-level domains.
See `Cldr.Locale.locale_from_host/2` for the default
list.
## Returns
* `{:ok, langauge_tag}` or
* `{:error, {exception, reason}}`
## Notes
Certain top-level domains have become associated with content
underlated to the territory for who the domain is registered.
Therefore Google (and perhaps others) do not associate these
TLDs as belonging to the territory but rather are considered
generic top-level domain names.
## Examples
iex> #{inspect(__MODULE__)}.locale_from_host "a.b.com.au"
#{config.backend}.validate_locale(:"en-AU")
iex> #{inspect(__MODULE__)}.locale_from_host("a.b.com.tv")
{:error,
{Cldr.UnknownLocaleError, "No locale was identified for territory \\"tv\\""}}
iex> #{inspect(__MODULE__)}.locale_from_host("a.b.com")
{:error,
{Cldr.UnknownLocaleError, "No locale was identified for territory \\"com\\""}}
"""
@doc since: "2.26.0"
@spec locale_from_host(String.t(), Keyword.t()) ::
{:ok, LanguageTag.t()} | {:error, {module(), String.t()}}
def locale_from_host(host, options \\ []) do
Locale.locale_from_host(host, unquote(config.backend), options)
end
@doc """
Returns the last segment of a host that might
be a territory.
## Arguments
* `host` is any valid host name
## Returns
* `{:ok, territory}` or
* `{:error, {exception, reason}}`
## Examples
iex> Cldr.Locale.territory_from_host("a.b.com.au")
{:ok, :AU}
iex> Cldr.Locale.territory_from_host("a.b.com")
{:error,
{Cldr.UnknownLocaleError, "No locale was identified for territory \\"com\\""}}
"""
@doc since: "2.26.0"
@spec territory_from_host(String.t()) ::
{:ok, Locale.territory_code()} | {:error, {module(), String.t()}}
def territory_from_host(host) do
Cldr.Locale.territory_from_host(host)
end
@doc """
Returns the list of fallback locales, starting
with the provided locale.
Fallbacks are a list of locate names which can
be used to resolve translation or other localization
data if such localised data does not exist for
this specific locale. After locale-specific fallbacks
are determined, the the default locale and its fallbacks
are added to the chain.
## Arguments
* `locale` is any `LanguageTag.t`
## Returns
* `{:ok, list_of_locales}` or
* `{:error, {exception, reason}}`
## Examples
In these examples the default locale is `:"en-001"`.
#{inspect __MODULE__}.fallback_locales(#{inspect __MODULE__}.new!("fr-CA"))
=> {:ok,
[#Cldr.LanguageTag<fr-CA [validated]>, #Cldr.LanguageTag<fr [validated]>,
#Cldr.LanguageTag<en [validated]>]}
# Fallbacks are typically formed by progressively
# stripping variant, territory and script from the
# given locale name. But not always - there are
# certain fallbacks that take a different path.
#{inspect __MODULE__}.fallback_locales(#{inspect __MODULE__}.new!("nb"))
=> {:ok,
[#Cldr.LanguageTag<nb [validated]>, #Cldr.LanguageTag<no [validated]>,
#Cldr.LanguageTag<en [validated]>]}
"""
@spec fallback_locales(LanguageTag.t() | Cldr.Locale.locale_reference) ::
{:ok, [LanguageTag.t(), ...]} | {:error, {module(), binary()}}
@doc since: "2.26.0"
def fallback_locales(%LanguageTag{} = locale) do
Cldr.Locale.fallback_locales(locale)
end
@doc """
Returns the list of fallback locales, starting
with the provided locale name.
Fallbacks are a list of locate names which can
be used to resolve translation or other localization
data if such localised data does not exist for
this specific locale. After locale-specific fallbacks
are determined, the the default locale and its fallbacks
are added to the chain.
## Arguments
* `locale_name` is any locale name returned by
`#{inspect config.backend}.known_locale_names/0`
## Returns
* `{:ok, list_of_locales}` or
* `{:error, {exception, reason}}`
## Examples
In these examples the default locale is `:"en-001"`.
#{inspect __MODULE__}.fallback_locales(:"fr-CA")
=> {:ok,
[#Cldr.LanguageTag<fr-CA [validated]>, #Cldr.LanguageTag<fr [validated]>,
#Cldr.LanguageTag<en [validated]>]}
# Fallbacks are typically formed by progressively
# stripping variant, territory and script from the
# given locale name. But not always - there are
# certain fallbacks that take a different path.
#{inspect __MODULE__}.fallback_locales(:nb))
=> {:ok,
[#Cldr.LanguageTag<nb [validated]>, #Cldr.LanguageTag<no [validated]>,
#Cldr.LanguageTag<en [validated]>]}
"""
@doc since: "2.26.0"
def fallback_locales(locale_name) do
Cldr.Locale.fallback_locales(locale_name, unquote(config.backend))
end
@doc """
Returns the list of fallback locale names, starting
with the provided locale.
Fallbacks are a list of locate names which can
be used to resolve translation or other localization
data if such localised data does not exist for
this specific locale. After locale-specific fallbacks
are determined, the the default locale and its fallbacks
are added to the chain.
## Arguments
* `locale` is any `Cldr,LangaugeTag.t`
## Returns
* `{:ok, list_of_locale_names}` or
* `{:error, {exception, reason}}`
## Examples
In these examples the default locale is `:"en-001"`.
iex> #{inspect __MODULE__}.fallback_locale_names(#{inspect __MODULE__}.new!("fr-CA"))
{:ok, [:"fr-CA", :fr, :"en-001", :en]}
# Fallbacks are typically formed by progressively
# stripping variant, territory and script from the
# given locale name. But not always - there are
# certain fallbacks that take a different path.
iex> #{inspect __MODULE__}.fallback_locale_names(#{inspect __MODULE__}.new!("nb"))
{:ok, [:nb, :no, :"en-001", :en]}
"""
@spec fallback_locale_names(LanguageTag.t() | Cldr.Locale.locale_reference) ::
{:ok, [Cldr.Locale.locale_name, ...]} | {:error, {module(), binary()}}
@doc since: "2.26.0"
def fallback_locale_names(%LanguageTag{} = locale) do
Cldr.Locale.fallback_locale_names(locale)
end
@doc """
Returns the list of fallback locale names, starting
with the provided locale name.
Fallbacks are a list of locate names which can
be used to resolve translation or other localization
data if such localised data does not exist for
this specific locale. After locale-specific fallbacks
are determined, the the default locale and its fallbacks
are added to the chain.
## Arguments
* `locale_name` is any locale name returned by
`#{inspect config.backend}.known_locale_names/0`
## Returns
* `{:ok, list_of_locale_names}` or
* `{:error, {exception, reason}}`
## Examples
In these examples the default locale is `:"en-001"`.
iex> #{inspect __MODULE__}.fallback_locale_names(:"fr-CA")
{:ok, [:"fr-CA", :fr, :"en-001", :en]}
# Fallbacks are typically formed by progressively
# stripping variant, territory and script from the
# given locale name. But not always - there are
# certain fallbacks that take a different path.
iex> #{inspect __MODULE__}.fallback_locale_names(:nb)
{:ok, [:nb, :no, :"en-001", :en]}
"""
@doc since: "2.26.0"
def fallback_locale_names(locale_name) do
Cldr.Locale.fallback_locale_names(locale_name, unquote(config.backend))
end
end
end
end
end