Terbium-135
Ex_cldr.number: currency with a default symbol for the particular locale
I’m struggling to get a ‘default currency symbol’ for the actual locale used. Perhaps it is wrong to assume that there is such a thing for a certain region/country?
Using the :currency format just gives the number but without a currency symbol:
Cldr.Number.to_string 1234.31, format: :currency, locale: "de"
{:ok, "1.234,31"}
Explicitly specifying the currency gives the desired result:
Cldr.Number.to_string 1345, currency: :EUR, locale: "de"
{:ok, "1.345,00 €"}
Can :EUR somehow be derived from the given locale?
My Cldr config is as follows:
defmodule Backend.Cldr do
use Cldr,
locales: ["de", "en"],
default_locale: "en",
json_library: Jason,
gettext: Backend.Gettext,
data_dir: "./priv/cldr",
otp_app: :backend,
precompile_number_formats: ["¤¤#.##0,##"],
providers: [Cldr.Number, Cldr.Unit, Cldr.List, Cldr.Calendar, Cldr.DateTime],
generate_docs: true
end
Marked As Solved
kip
My preference is typically to derive everything possible from the language tag since that expresses the users preferences. But of course not everyone agrees with that. Part of the rational is that for a given language (like “de”) a territory can always be derived. Typically it is the territory that has the highest population of speakers of that language.
# What is the derived territory for "de"?
iex> {:ok, locale} = Cldr.validate_locale("de")
{:ok, #Cldr.LanguageTag<de [validated]>}
iex> locale.territory
:DE
# And each territory has one (or none) current currency:
iex> Cldr.Currency.current_currency_for_territory locale.territory
:EUR
So in that respect I think its quite “clean” and consistent.
The functions that can help most commonly are:
iex> Cldr.Currency.current_currency_for_territory "de"
:EUR
iex> Cldr.Currency.currency_from_locale "de-AT"
:EUR
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









