Fix incorrect fallback when English is set to first language
authorTusooa Zhu <tusooa@kazv.moe>
Mon, 18 Apr 2022 02:39:52 +0000 (22:39 -0400)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Wed, 29 Jun 2022 19:47:10 +0000 (20:47 +0100)
lib/pleroma/web/gettext.ex
test/pleroma/web/gettext_test.exs

index 51e56939e4dad2b5c49d6d14b2357fe1307c8584..7afcd38f03892cf390bd8b2ca6bb99ff3cc911c9 100644 (file)
@@ -161,10 +161,25 @@ defmodule Pleroma.Web.Gettext do
     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
     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)
     else
       {:ok,
@@ -185,7 +200,7 @@ defmodule Pleroma.Web.Gettext do
       ) do
     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)
     else
       {:ok,
index 9ede4827e8026be9486df2701a7c0be83e7a927d..e186f1ab334e06c6047947f20b77228d91a1bab2 100644 (file)
@@ -38,6 +38,17 @@ defmodule Pleroma.Web.GettextTest do
       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
       Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do
         assert "xxYour account is awaiting approvalxx" ==