Support fallbacking to other languages
[akkoma] / lib / pleroma / web / gettext.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Gettext do
6 @moduledoc """
7 A module providing Internationalization with a gettext-based API.
8
9 By using [Gettext](https://hexdocs.pm/gettext),
10 your module gains a set of macros for translations, for example:
11
12 import Pleroma.Web.Gettext
13
14 # Simple translation
15 gettext "Here is the string to translate"
16
17 # Plural translation
18 ngettext "Here is the string to translate",
19 "Here are the strings to translate",
20 3
21
22 # Domain-based translation
23 dgettext "errors", "Here is the error message to translate"
24
25 See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
26 """
27 use Gettext, otp_app: :pleroma
28
29 def language_tag do
30 # Naive implementation: HTML lang attribute uses BCP 47, which
31 # uses - as a separator.
32 # https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang
33
34 Gettext.get_locale()
35 |> String.replace("_", "-", global: true)
36 end
37
38 def normalize_locale(locale) do
39 if is_binary(locale) do
40 String.replace(locale, "-", "_", global: true)
41 else
42 nil
43 end
44 end
45
46 def supports_locale?(locale) do
47 Pleroma.Web.Gettext
48 |> Gettext.known_locales()
49 |> Enum.member?(locale)
50 end
51
52 def variant?(locale), do: String.contains?(locale, "_")
53
54 def language_for_variant(locale) do
55 Enum.at(String.split(locale, "_"), 0)
56 end
57
58 def ensure_fallbacks(locales) do
59 locales
60 |> Enum.flat_map(fn locale ->
61 others =
62 other_supported_variants_of_locale(locale)
63 |> Enum.filter(fn l -> not Enum.member?(locales, l) end)
64
65 [locale] ++ others
66 end)
67 end
68
69 def other_supported_variants_of_locale(locale) do
70 cond do
71 supports_locale?(locale) ->
72 []
73
74 variant?(locale) ->
75 lang = language_for_variant(locale)
76 if supports_locale?(lang), do: [lang], else: []
77
78 true ->
79 Gettext.known_locales(Pleroma.Web.Gettext)
80 |> Enum.filter(fn l -> String.starts_with?(l, locale <> "_") end)
81 end
82 end
83
84 def get_locales do
85 Process.get({Pleroma.Web.Gettext, :locales}, [])
86 end
87
88 def is_locale_list(locales) do
89 Enum.all?(locales, &is_binary/1)
90 end
91
92 def put_locales(locales) do
93 if is_locale_list(locales) do
94 Process.put({Pleroma.Web.Gettext, :locales}, Enum.uniq(locales))
95 Gettext.put_locale(Enum.at(locales, 0, Gettext.get_locale()))
96 :ok
97 else
98 {:error, :not_locale_list}
99 end
100 end
101
102 def locale_or_default(locale) do
103 if supports_locale?(locale) do
104 locale
105 else
106 Gettext.get_locale()
107 end
108 end
109
110 def with_locales_func(locales, fun) do
111 prev_locales = Process.get({Pleroma.Web.Gettext, :locales})
112 put_locales(locales)
113
114 try do
115 fun.()
116 after
117 if prev_locales do
118 put_locales(prev_locales)
119 else
120 Process.delete({Pleroma.Web.Gettext, :locales})
121 Process.delete(Gettext)
122 end
123 end
124 end
125
126 defmacro with_locales(locales, do: fun) do
127 quote do
128 Pleroma.Web.Gettext.with_locales_func(unquote(locales), fn ->
129 unquote(fun)
130 end)
131 end
132 end
133
134 def to_locale_list(locale) when is_binary(locale) do
135 locale
136 |> String.split(",")
137 |> Enum.filter(&supports_locale?/1)
138 end
139
140 def to_locale_list(_), do: []
141
142 defmacro with_locale_or_default(locale, do: fun) do
143 quote do
144 Pleroma.Web.Gettext.with_locales_func(
145 Pleroma.Web.Gettext.to_locale_list(unquote(locale))
146 |> Enum.concat(Pleroma.Web.Gettext.get_locales()),
147 fn ->
148 unquote(fun)
149 end
150 )
151 end
152 end
153
154 defp next_locale(locale, list) do
155 index = Enum.find_index(list, fn item -> item == locale end)
156
157 if not is_nil(index) do
158 Enum.at(list, index + 1)
159 else
160 nil
161 end
162 end
163
164 def handle_missing_translation(locale, domain, msgctxt, msgid, bindings) do
165 next = next_locale(locale, get_locales())
166
167 if is_nil(next) do
168 super(locale, domain, msgctxt, msgid, bindings)
169 else
170 {:ok,
171 Gettext.with_locale(next, fn ->
172 Gettext.dpgettext(Pleroma.Web.Gettext, domain, msgctxt, msgid, bindings)
173 end)}
174 end
175 end
176
177 def handle_missing_plural_translation(
178 locale,
179 domain,
180 msgctxt,
181 msgid,
182 msgid_plural,
183 n,
184 bindings
185 ) do
186 next = next_locale(locale, get_locales())
187
188 if is_nil(next) do
189 super(locale, domain, msgctxt, msgid, msgid_plural, n, bindings)
190 else
191 {:ok,
192 Gettext.with_locale(next, fn ->
193 Gettext.dpngettext(
194 Pleroma.Web.Gettext,
195 domain,
196 msgctxt,
197 msgid,
198 msgid_plural,
199 n,
200 bindings
201 )
202 end)}
203 end
204 end
205 end