Refactor to avoid calling Mobilizon.Cldr.known_locale_name with a string

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-03-21 17:50:38 +01:00
parent 3dd0b60606
commit 7067173a5a
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 28 additions and 14 deletions

View file

@ -176,8 +176,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
@spec get_list_of_languages(any(), any(), any()) :: {:ok, String.t()} | {:error, any()}
def get_list_of_languages(_parent, %{codes: codes}, _resolution) when is_list(codes) do
locale = Gettext.get_locale()
locale = if Cldr.known_locale_name?(locale), do: locale, else: "en"
locale = Mobilizon.Cldr.locale_or_default(Gettext.get_locale())
case Language.known_languages(locale) do
data when is_map(data) ->

View file

@ -12,4 +12,18 @@ defmodule Mobilizon.Cldr do
else: nil
),
providers: [Cldr.Number, Cldr.Calendar, Cldr.DateTime, Cldr.Language]
def known_locale?(locale) do
Mobilizon.Cldr.known_locale_names()
|> Enum.map(&Atom.to_string/1)
|> Enum.member?(locale)
end
def locale_or_default(locale, default \\ "en") do
if known_locale?(locale) do
locale
else
default
end
end
end

View file

@ -8,17 +8,26 @@ defmodule Mobilizon.Service.DateTime do
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
Mobilizon.Cldr.DateTime.to_string!(datetime, format: format, locale: locale_or_default(locale))
Mobilizon.Cldr.DateTime.to_string!(datetime,
format: format,
locale: Mobilizon.Cldr.locale_or_default(locale)
)
end
@spec datetime_to_time_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
def datetime_to_time_string(%DateTime{} = datetime, locale \\ "en", format \\ :short) do
Mobilizon.Cldr.Time.to_string!(datetime, format: format, locale: locale_or_default(locale))
Mobilizon.Cldr.Time.to_string!(datetime,
format: format,
locale: Mobilizon.Cldr.locale_or_default(locale)
)
end
@spec datetime_to_date_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
def datetime_to_date_string(%DateTime{} = datetime, locale \\ "en", format \\ :short) do
Mobilizon.Cldr.Date.to_string!(datetime, format: format, locale: locale_or_default(locale))
Mobilizon.Cldr.Date.to_string!(datetime,
format: format,
locale: Mobilizon.Cldr.locale_or_default(locale)
)
end
@spec datetime_tz_convert(DateTime.t(), String.t() | nil) :: DateTime.t()
@ -38,18 +47,10 @@ defmodule Mobilizon.Service.DateTime do
def datetime_relative(%DateTime{} = datetime, locale \\ "en") do
Relative.to_string!(datetime, Mobilizon.Cldr,
relative_to: DateTime.utc_now(),
locale: locale_or_default(locale)
locale: Mobilizon.Cldr.locale_or_default(locale)
)
end
defp locale_or_default(locale) do
if Mobilizon.Cldr.known_locale_name(locale) do
locale
else
"en"
end
end
@spec is_first_day_of_week(Date.t(), String.t()) :: boolean()
defp is_first_day_of_week(%Date{} = date, locale) do
Date.day_of_week(date) == Cldr.Calendar.first_day_for_locale(locale)