mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-11 11:49:15 +00:00
fa95bc8725
elixir gettext current does not fully support fallback to another language [0]. But it might in the future. We adapt it so that all languages in Accept-Language headers are received by Pleroma.Web.Gettext. User.languages is now a comma-separated list. [0]: https://github.com/elixir-gettext/gettext/issues/303
136 lines
3.2 KiB
Elixir
136 lines
3.2 KiB
Elixir
# Pleroma: A lightweight social networking server
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
defmodule Pleroma.Web.Gettext do
|
|
@moduledoc """
|
|
A module providing Internationalization with a gettext-based API.
|
|
|
|
By using [Gettext](https://hexdocs.pm/gettext),
|
|
your module gains a set of macros for translations, for example:
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
# Simple translation
|
|
gettext "Here is the string to translate"
|
|
|
|
# Plural translation
|
|
ngettext "Here is the string to translate",
|
|
"Here are the strings to translate",
|
|
3
|
|
|
|
# Domain-based translation
|
|
dgettext "errors", "Here is the error message to translate"
|
|
|
|
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
|
|
"""
|
|
use Gettext, otp_app: :pleroma
|
|
|
|
def language_tag do
|
|
# Naive implementation: HTML lang attribute uses BCP 47, which
|
|
# uses - as a separator.
|
|
# https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang
|
|
|
|
Gettext.get_locale()
|
|
|> String.replace("_", "-", global: true)
|
|
end
|
|
|
|
def normalize_locale(locale) do
|
|
if is_binary(locale) do
|
|
String.replace(locale, "-", "_")
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
|
|
def supports_locale?(locale) do
|
|
Pleroma.Web.Gettext
|
|
|> Gettext.known_locales()
|
|
|> Enum.member?(locale)
|
|
end
|
|
|
|
def variant?(locale), do: String.contains?(locale, "_")
|
|
|
|
def supported_variants_of_locale(locale) do
|
|
cond do
|
|
variant?(locale) ->
|
|
[locale]
|
|
|
|
supports_locale?(locale) ->
|
|
[locale]
|
|
|
|
true ->
|
|
Gettext.known_locales(Pleroma.Web.Gettext)
|
|
|> Enum.filter(fn l -> String.starts_with?(l, locale <> "_") end)
|
|
end
|
|
end
|
|
|
|
def get_locales() do
|
|
Process.get({Pleroma.Web.Gettext, :locales}, [])
|
|
end
|
|
|
|
def is_locale_list(locales) do
|
|
Enum.all?(locales, &is_binary/1)
|
|
end
|
|
|
|
def put_locales(locales) do
|
|
if is_locale_list(locales) do
|
|
Process.put({Pleroma.Web.Gettext, :locales}, Enum.uniq(locales))
|
|
Gettext.put_locale(Enum.at(locales, 0, Gettext.get_locale()))
|
|
:ok
|
|
else
|
|
{:error, :not_locale_list}
|
|
end
|
|
end
|
|
|
|
def locale_or_default(locale) do
|
|
if supports_locale?(locale) do
|
|
locale
|
|
else
|
|
Gettext.get_locale()
|
|
end
|
|
end
|
|
|
|
def with_locales_func(locales, fun) do
|
|
prev_locales = Process.get({Pleroma.Web.Gettext, :locales})
|
|
put_locales(locales)
|
|
|
|
try do
|
|
fun.()
|
|
after
|
|
if prev_locales do
|
|
put_locales(prev_locales)
|
|
else
|
|
Process.delete({Pleroma.Web.Gettext, :locales})
|
|
end
|
|
end
|
|
end
|
|
|
|
defmacro with_locales(locales, do: fun) do
|
|
quote do
|
|
Pleroma.Web.Gettext.with_locales_func(unquote(locales), fn ->
|
|
unquote(fun)
|
|
end)
|
|
end
|
|
end
|
|
|
|
def to_locale_list(locale) when is_binary(locale) do
|
|
locale
|
|
|> String.split(",")
|
|
|> Enum.filter(&supports_locale?/1)
|
|
end
|
|
|
|
def to_locale_list(_), do: []
|
|
|
|
defmacro with_locale_or_default(locale, do: fun) do
|
|
quote do
|
|
Pleroma.Web.Gettext.with_locales_func(
|
|
Pleroma.Web.Gettext.to_locale_list(unquote(locale))
|
|
|> Enum.concat(Pleroma.Web.Gettext.get_locales()),
|
|
fn ->
|
|
unquote(fun)
|
|
end
|
|
)
|
|
end
|
|
end
|
|
end
|