6. Dates & times in CLDR
# Install dependencies
Mix.install([
:ex_cldr,
:ex_cldr_dates_times,
:jason
])
# Define a backend module
defmodule DemoApp.Backend do
use Cldr,
locales: ["en", "en-GB", "es", "pt", "hi"],
default_locale: "en",
providers: [Cldr.Number, Cldr.Calendar, Cldr.DateTime],
json_library: Jason
end
# Set an app-wide default backend
Application.put_env(:ex_cldr, :default_backend, DemoApp.Backend)
DemoApp.Backend.put_locale("en-US")
:ok
09:08:50.322 [info] Downloaded locale :"en-GB"
Generating DemoApp.Backend for 6 locales named [:en, :"en-GB", :es, :hi, :pt, ...] with a default locale named :en
:ok
TODO
https://hexdocs.pm/ex_cldr_dates_times/readme.html
Cldr.Date.to_string!(Date.utc_today(), format: :short, locale: "en")
"8/15/23"
Cldr.Date.to_string!(Date.utc_today(), format: :short, locale: "en-GB")
"15/08/2023"
Cldr.known_locale_names()
[:en, :es, :hi, :pt]
Cldr.Date.to_string!(~D[1985-12-21], format: :short)
"12/21/85"
Cldr.Date.to_string!(~D[1985-12-21], format: :short, locale: :hi)
"21/12/85"
Cldr.Date.to_string!(~D[1985-12-21], format: :short, locale: :pt)
"21/12/1985"