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:
parent
47f188a277
commit
2d99fe877c
@ -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) ->
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user