1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Gettext do
7 A module providing Internationalization with a gettext-based API.
9 By using [Gettext](https://hexdocs.pm/gettext),
10 your module gains a set of macros for translations, for example:
12 import Pleroma.Web.Gettext
15 gettext "Here is the string to translate"
18 ngettext "Here is the string to translate",
19 "Here are the strings to translate",
22 # Domain-based translation
23 dgettext "errors", "Here is the error message to translate"
25 See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
27 use Gettext, otp_app: :pleroma
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
35 |> String.replace("_", "-", global: true)
38 def supports_locale?(locale) do
40 |> Gettext.known_locales()
41 |> Enum.member?(locale)
44 def locale_or_default(locale) do
45 if supports_locale?(locale) do
52 defmacro with_locale_or_default(locale, do: fun) do
54 Gettext.with_locale(Pleroma.Web.Gettext.locale_or_default(unquote(locale)), fn ->