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()}
|
@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
|
def get_list_of_languages(_parent, %{codes: codes}, _resolution) when is_list(codes) do
|
||||||
locale = Gettext.get_locale()
|
locale = Mobilizon.Cldr.locale_or_default(Gettext.get_locale())
|
||||||
locale = if Cldr.known_locale_name?(locale), do: locale, else: "en"
|
|
||||||
|
|
||||||
case Language.known_languages(locale) do
|
case Language.known_languages(locale) do
|
||||||
data when is_map(data) ->
|
data when is_map(data) ->
|
||||||
|
@ -12,4 +12,18 @@ defmodule Mobilizon.Cldr do
|
|||||||
else: nil
|
else: nil
|
||||||
),
|
),
|
||||||
providers: [Cldr.Number, Cldr.Calendar, Cldr.DateTime, Cldr.Language]
|
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
|
end
|
||||||
|
@ -8,17 +8,26 @@ defmodule Mobilizon.Service.DateTime do
|
|||||||
|
|
||||||
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
||||||
def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
|
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
|
end
|
||||||
|
|
||||||
@spec datetime_to_time_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
@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
|
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
|
end
|
||||||
|
|
||||||
@spec datetime_to_date_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
@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
|
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
|
end
|
||||||
|
|
||||||
@spec datetime_tz_convert(DateTime.t(), String.t() | nil) :: DateTime.t()
|
@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
|
def datetime_relative(%DateTime{} = datetime, locale \\ "en") do
|
||||||
Relative.to_string!(datetime, Mobilizon.Cldr,
|
Relative.to_string!(datetime, Mobilizon.Cldr,
|
||||||
relative_to: DateTime.utc_now(),
|
relative_to: DateTime.utc_now(),
|
||||||
locale: locale_or_default(locale)
|
locale: Mobilizon.Cldr.locale_or_default(locale)
|
||||||
)
|
)
|
||||||
end
|
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()
|
@spec is_first_day_of_week(Date.t(), String.t()) :: boolean()
|
||||||
defp is_first_day_of_week(%Date{} = date, locale) do
|
defp is_first_day_of_week(%Date{} = date, locale) do
|
||||||
Date.day_of_week(date) == Cldr.Calendar.first_day_for_locale(locale)
|
Date.day_of_week(date) == Cldr.Calendar.first_day_for_locale(locale)
|
||||||
|
Loading…
Reference in New Issue
Block a user