diff options
author | Tusooa Zhu <tusooa@kazv.moe> | 2022-03-06 11:43:31 -0500 |
---|---|---|
committer | Tusooa Zhu <tusooa@kazv.moe> | 2022-03-06 11:43:31 -0500 |
commit | 79ccb6b9998ffffa32ba059c8e97f0f604db81f6 (patch) | |
tree | 58ed7434e457d56be3d5352f925ebd2e3d48468d /lib | |
parent | cd42e2bed0039ce4939e4c55fb7fcd7cf2568b44 (diff) | |
download | pleroma-79ccb6b9998ffffa32ba059c8e97f0f604db81f6.tar.gz pleroma-79ccb6b9998ffffa32ba059c8e97f0f604db81f6.zip |
Support fallbacking to other languages
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/web/gettext.ex | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/pleroma/web/gettext.ex b/lib/pleroma/web/gettext.ex index 89feb0bb3..51e56939e 100644 --- a/lib/pleroma/web/gettext.ex +++ b/lib/pleroma/web/gettext.ex @@ -118,6 +118,7 @@ defmodule Pleroma.Web.Gettext do put_locales(prev_locales) else Process.delete({Pleroma.Web.Gettext, :locales}) + Process.delete(Gettext) end end end @@ -149,4 +150,56 @@ defmodule Pleroma.Web.Gettext do ) end end + + defp next_locale(locale, list) do + index = Enum.find_index(list, fn item -> item == locale end) + + if not is_nil(index) do + Enum.at(list, index + 1) + else + nil + end + end + + def handle_missing_translation(locale, domain, msgctxt, msgid, bindings) do + next = next_locale(locale, get_locales()) + + if is_nil(next) do + super(locale, domain, msgctxt, msgid, bindings) + else + {:ok, + Gettext.with_locale(next, fn -> + Gettext.dpgettext(Pleroma.Web.Gettext, domain, msgctxt, msgid, bindings) + end)} + end + end + + def handle_missing_plural_translation( + locale, + domain, + msgctxt, + msgid, + msgid_plural, + n, + bindings + ) do + next = next_locale(locale, get_locales()) + + if is_nil(next) do + super(locale, domain, msgctxt, msgid, msgid_plural, n, bindings) + else + {:ok, + Gettext.with_locale(next, fn -> + Gettext.dpngettext( + Pleroma.Web.Gettext, + domain, + msgctxt, + msgid, + msgid_plural, + n, + bindings + ) + end)} + end + end end |