89feb0bb3f0e9fcfe6199f8573d1ffb0e0a2f30a
[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 end
122 end
123 end
124
125 defmacro with_locales(locales, do: fun) do
126 quote do
127 Pleroma.Web.Gettext.with_locales_func(unquote(locales), fn ->
128 unquote(fun)
129 end)
130 end
131 end
132
133 def to_locale_list(locale) when is_binary(locale) do
134 locale
135 |> String.split(",")
136 |> Enum.filter(&supports_locale?/1)
137 end
138
139 def to_locale_list(_), do: []
140
141 defmacro with_locale_or_default(locale, do: fun) do
142 quote do
143 Pleroma.Web.Gettext.with_locales_func(
144 Pleroma.Web.Gettext.to_locale_list(unquote(locale))
145 |> Enum.concat(Pleroma.Web.Gettext.get_locales()),
146 fn ->
147 unquote(fun)
148 end
149 )
150 end
151 end
152 end