Fix incorrect fallback when English is set to first language

This commit is contained in:
Tusooa Zhu 2022-04-17 22:39:52 -04:00 committed by FloatingGhost
parent e3107fee98
commit 33d821256e
2 changed files with 28 additions and 2 deletions

View file

@ -161,10 +161,25 @@ defp next_locale(locale, list) do
end end
end end
# We do not yet have a proper English translation. The "English"
# version is currently but the fallback msgid. However, this
# will not work if the user puts English as the first language,
# and at the same time specifies other languages, as gettext will
# think the English translation is missing, and call
# handle_missing_translation functions. This may result in
# text in other languages being shown even if English is preferred
# by the user.
#
# To prevent this, we do not allow fallbacking when the current
# locale missing a translation is English.
defp should_fallback?(locale) do
locale != "en"
end
def handle_missing_translation(locale, domain, msgctxt, msgid, bindings) do def handle_missing_translation(locale, domain, msgctxt, msgid, bindings) do
next = next_locale(locale, get_locales()) next = next_locale(locale, get_locales())
if is_nil(next) do if is_nil(next) or not should_fallback?(locale) do
super(locale, domain, msgctxt, msgid, bindings) super(locale, domain, msgctxt, msgid, bindings)
else else
{:ok, {:ok,
@ -185,7 +200,7 @@ def handle_missing_plural_translation(
) do ) do
next = next_locale(locale, get_locales()) next = next_locale(locale, get_locales())
if is_nil(next) do if is_nil(next) or not should_fallback?(locale) do
super(locale, domain, msgctxt, msgid, msgid_plural, n, bindings) super(locale, domain, msgctxt, msgid, msgid_plural, n, bindings)
else else
{:ok, {:ok,

View file

@ -38,6 +38,17 @@ test "fallback to next locale if some translation is not available" do
end end
end end
test "putting en locale at the front should not make gettext fallback unexpectedly" do
Pleroma.Web.Gettext.with_locales ["en", "en_test"] do
assert "Your account is awaiting approval" ==
Pleroma.Web.Gettext.dpgettext(
"static_pages",
"approval pending email subject",
"Your account is awaiting approval"
)
end
end
test "duplicated locale in list should not result in infinite loops" do test "duplicated locale in list should not result in infinite loops" do
Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do
assert "xxYour account is awaiting approvalxx" == assert "xxYour account is awaiting approvalxx" ==