From 7067173a5ac3bcd33a957cf39c06aed2627afac0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 21 Mar 2022 17:50:38 +0100 Subject: [PATCH] Refactor to avoid calling Mobilizon.Cldr.known_locale_name with a string Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/admin.ex | 3 +-- lib/mobilizon/cldr.ex | 14 ++++++++++++++ lib/service/date_time/date_time.ex | 25 +++++++++++++------------ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/graphql/resolvers/admin.ex b/lib/graphql/resolvers/admin.ex index 12488d653..369eb26b6 100644 --- a/lib/graphql/resolvers/admin.ex +++ b/lib/graphql/resolvers/admin.ex @@ -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) -> diff --git a/lib/mobilizon/cldr.ex b/lib/mobilizon/cldr.ex index a64bf3e6d..e7b2fffad 100644 --- a/lib/mobilizon/cldr.ex +++ b/lib/mobilizon/cldr.ex @@ -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 diff --git a/lib/service/date_time/date_time.ex b/lib/service/date_time/date_time.ex index d59f10010..d1aec54f2 100644 --- a/lib/service/date_time/date_time.ex +++ b/lib/service/date_time/date_time.ex @@ -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)