View Source MyApp.Cldr.Locale (Cldr v2.33.1)
Backend module that provides functions to define new locales and display human-readable locale names for presentation purposes.
Link to this section Summary
Functions
Returns the list of fallback locale names, starting with the provided locale name.
Returns the list of fallback locales, starting with the provided locale name.
Returns the "best fit" locale for a given territory.
Returns a "best fit" locale for a host name.
Returns the last segment of a host that might be a territory.
Returns the territory from a language tag or locale name.
Returns the time zone from a language tag or locale name.
Link to this section Functions
@spec fallback_locale_names(Cldr.LanguageTag.t() | Cldr.Locale.locale_reference()) :: {:ok, [Cldr.Locale.locale_name(), ...]} | {:error, {module(), String.t()}}
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
Arguments
locale_name
is any locale name returned byMyApp.Cldr.known_locale_names/0
returns
Returns
{:ok, list_of_locale_names}
or{:error, {exception, reason}}
examples
Examples
In these examples the default locale is :"en-001"
.
iex> MyApp.Cldr.Locale.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> MyApp.Cldr.Locale.fallback_locale_names(:nb)
{:ok, [:nb, :no, :"en-001", :en]}
@spec fallback_locales(Cldr.LanguageTag.t() | Cldr.Locale.locale_reference()) :: {:ok, [Cldr.LanguageTag.t(), ...]} | {:error, {module(), String.t()}}
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
Arguments
locale_name
is any locale name returned byMyApp.Cldr.known_locale_names/0
returns
Returns
{:ok, list_of_locales}
or{:error, {exception, reason}}
examples
Examples
In these examples the default locale is :"en-001"
.
MyApp.Cldr.Locale.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.
MyApp.Cldr.Locale.fallback_locales(:nb))
=> {:ok,
[#Cldr.LanguageTag<nb [validated]>, #Cldr.LanguageTag<no [validated]>,
#Cldr.LanguageTag<en [validated]>]}
@spec locale_for_territory(Cldr.Locale.territory_code()) :: {:ok, Cldr.LanguageTag.t()} | {:error, {module(), String.t()}}
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
Arguments
territory
is any ISO 3166 Alpha-2 territory code that can be validated byCldr.validate_territory/1
returns
Returns
{:ok, language_tag}
or{:error, {exception, reason}}
examples
Examples
iex> MyApp.Cldr.Locale.locale_for_territory(:AU) Elixir.MyApp.Cldr.validate_locale(:"en-AU")
iex> MyApp.Cldr.Locale.locale_for_territory(:US) Elixir.MyApp.Cldr.validate_locale(:"en-US")
iex> MyApp.Cldr.Locale.locale_for_territory(:ZZ) {:error, {Cldr.UnknownTerritoryError, "The territory :ZZ is unknown"}}
@spec locale_from_host(String.t(), Keyword.t()) :: {:ok, Cldr.LanguageTag.t()} | {:error, {module(), String.t()}}
Returns a "best fit" locale for a host name.
arguments
Arguments
host
is any valid host nameoptions
is a keyword list of options. The default is[]
.
options
Options
:tlds
is a list of territory codes as upper-cased atoms that are to be considered as top-level domains. SeeCldr.Locale.locale_from_host/2
for the default list.
returns
Returns
{:ok, langauge_tag}
or{:error, {exception, reason}}
notes
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
Examples
iex> MyApp.Cldr.Locale.locale_from_host "a.b.com.au"
Elixir.MyApp.Cldr.validate_locale(:"en-AU")
iex> MyApp.Cldr.Locale.locale_from_host("a.b.com.tv")
{:error,
{Cldr.UnknownLocaleError, "No locale was identified for territory \"tv\""}}
iex> MyApp.Cldr.Locale.locale_from_host("a.b.com")
{:error,
{Cldr.UnknownLocaleError, "No locale was identified for territory \"com\""}}
@spec territory_from_host(String.t()) :: {:ok, Cldr.Locale.territory_code()} | {:error, {module(), String.t()}}
Returns the last segment of a host that might be a territory.
arguments
Arguments
host
is any valid host name
returns
Returns
{:ok, territory}
or{:error, {exception, reason}}
examples
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\""}}
@spec territory_from_locale(Cldr.LanguageTag.t() | Cldr.Locale.locale_name()) :: Cldr.Locale.territory_code()
Returns the territory from a language tag or locale name.
arguments
Arguments
locale
is any language tag returned byMyApp.Cldr.Locale.new/1
or a locale name in the list returned byMyApp.Cldr.known_locale_names/0
returns
Returns
- A territory code as an atom
examples
Examples
iex> MyApp.Cldr.Locale.territory_from_locale "en-US"
:US
iex> MyApp.Cldr.Locale.territory_from_locale "en-US-u-rg-GBzzzz"
:GB
@spec timezone_from_locale(Cldr.LanguageTag.t() | Cldr.Locale.locale_name()) :: String.t() | {:error, {module(), String.t()}}
Returns the time zone from a language tag or locale name.
arguments
Arguments
locale
is any language tag returned byMyApp.Cldr.Locale.new/1
or a locale name in the list returned byMyApp.Cldr.known_locale_names/0
returns
Returns
A time zone ID as a string or
:error
if no time zone can be determined
examples
Examples
iex> MyApp.Cldr.Locale.timezone_from_locale "en-US-u-tz-ausyd"
"Australia/Sydney"