Merge branch 'from/upstream-develop/tusooa/translate-pages' into 'develop'

Translate backend-rendered pages

See merge request pleroma/pleroma!3634
This commit is contained in:
Haelwenn 2022-03-20 18:14:37 +00:00
commit d7c53da77a
58 changed files with 3319 additions and 446 deletions

View file

@ -53,7 +53,13 @@ defmodule Mix.Tasks.Pleroma.DigestTest do
assert_email_sent(
to: {user2.name, user2.email},
html_body: ~r/here is what you've missed!/i
html_body:
Regex.compile!(
"here is what you've missed!"
|> Phoenix.HTML.html_escape()
|> Phoenix.HTML.safe_to_string(),
"i"
)
)
end
end

View file

@ -56,4 +56,16 @@ defmodule Pleroma.Emails.UserEmailTest do
assert email.subject == "Your account is awaiting approval"
assert email.html_body =~ "Awaiting Approval"
end
test "email i18n" do
user = insert(:user, language: "en_test")
email = UserEmail.approval_pending_email(user)
assert email.subject == "xxYour account is awaiting approvalxx"
end
test "email i18n should fallback to default locale if user language is unsupported" do
user = insert(:user, language: "unsupported")
email = UserEmail.approval_pending_email(user)
assert email.subject == "Your account is awaiting approval"
end
end

View file

@ -0,0 +1,162 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.GettextTest do
use ExUnit.Case
require Pleroma.Web.Gettext
test "put_locales/1: set the first in the list to Gettext's locale" do
Pleroma.Web.Gettext.put_locales(["zh_Hans", "en_test"])
assert "zh_Hans" == Gettext.get_locale(Pleroma.Web.Gettext)
end
test "with_locales/2: reset locale on exit" do
old_first_locale = Gettext.get_locale(Pleroma.Web.Gettext)
old_locales = Pleroma.Web.Gettext.get_locales()
Pleroma.Web.Gettext.with_locales ["zh_Hans", "en_test"] do
assert "zh_Hans" == Gettext.get_locale(Pleroma.Web.Gettext)
assert ["zh_Hans", "en_test"] == Pleroma.Web.Gettext.get_locales()
end
assert old_first_locale == Gettext.get_locale(Pleroma.Web.Gettext)
assert old_locales == Pleroma.Web.Gettext.get_locales()
end
describe "handle_missing_translation/5" do
test "fallback to next locale if some translation is not available" do
Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
assert "xxYour account is awaiting approvalxx" ==
Pleroma.Web.Gettext.dpgettext(
"static_pages",
"approval pending email subject",
"Your account is awaiting approval"
)
end
end
test "duplicated locale in list should not result in infinite loops" do
Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do
assert "xxYour account is awaiting approvalxx" ==
Pleroma.Web.Gettext.dpgettext(
"static_pages",
"approval pending email subject",
"Your account is awaiting approval"
)
end
end
test "direct interpolation" do
Pleroma.Web.Gettext.with_locales ["en_test"] do
assert "xxYour digest from some instancexx" ==
Pleroma.Web.Gettext.dpgettext(
"static_pages",
"digest email subject",
"Your digest from %{instance_name}",
instance_name: "some instance"
)
end
end
test "fallback with interpolation" do
Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
assert "xxYour digest from some instancexx" ==
Pleroma.Web.Gettext.dpgettext(
"static_pages",
"digest email subject",
"Your digest from %{instance_name}",
instance_name: "some instance"
)
end
end
test "fallback to msgid" do
Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
assert "Your digest from some instance" ==
Pleroma.Web.Gettext.dpgettext(
"static_pages",
"digest email subject",
"Your digest from %{instance_name}",
instance_name: "some instance"
)
end
end
end
describe "handle_missing_plural_translation/7" do
test "direct interpolation" do
Pleroma.Web.Gettext.with_locales ["en_test"] do
assert "xx1 New Followerxx" ==
Pleroma.Web.Gettext.dpngettext(
"static_pages",
"new followers count header",
"%{count} New Follower",
"%{count} New Followers",
1,
count: 1
)
assert "xx5 New Followersxx" ==
Pleroma.Web.Gettext.dpngettext(
"static_pages",
"new followers count header",
"%{count} New Follower",
"%{count} New Followers",
5,
count: 5
)
end
end
test "fallback with interpolation" do
Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
assert "xx1 New Followerxx" ==
Pleroma.Web.Gettext.dpngettext(
"static_pages",
"new followers count header",
"%{count} New Follower",
"%{count} New Followers",
1,
count: 1
)
assert "xx5 New Followersxx" ==
Pleroma.Web.Gettext.dpngettext(
"static_pages",
"new followers count header",
"%{count} New Follower",
"%{count} New Followers",
5,
count: 5
)
end
end
test "fallback to msgid" do
Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
assert "1 New Follower" ==
Pleroma.Web.Gettext.dpngettext(
"static_pages",
"new followers count header",
"%{count} New Follower",
"%{count} New Followers",
1,
count: 1
)
assert "5 New Followers" ==
Pleroma.Web.Gettext.dpngettext(
"static_pages",
"new followers count header",
"%{count} New Follower",
"%{count} New Followers",
5,
count: 5
)
end
end
end
end

View file

@ -13,6 +13,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
alias Pleroma.Web.ActivityPub.InternalFetchActor
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.Plugs.SetLocalePlug
import Pleroma.Factory
@ -1662,6 +1663,75 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
end
end
describe "create account with language" do
setup %{conn: conn} do
app_token = insert(:oauth_token, user: nil)
conn =
conn
|> put_req_header("authorization", "Bearer " <> app_token.token)
|> put_req_header("content-type", "multipart/form-data")
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
|> SetLocalePlug.call([])
[conn: conn]
end
test "creates an account with language parameter", %{conn: conn} do
params = %{
username: "foo",
email: "foo@example.org",
password: "dupa.8",
agreement: true,
language: "ru"
}
res =
conn
|> post("/api/v1/accounts", params)
assert json_response_and_validate_schema(res, 200)
assert %{language: "ru"} = Pleroma.User.get_by_nickname("foo")
end
test "language parameter should be normalized", %{conn: conn} do
params = %{
username: "foo",
email: "foo@example.org",
password: "dupa.8",
agreement: true,
language: "ru-RU"
}
res =
conn
|> post("/api/v1/accounts", params)
assert json_response_and_validate_schema(res, 200)
assert %{language: "ru_RU"} = Pleroma.User.get_by_nickname("foo")
end
test "createing an account without language parameter should fallback to cookie/header language",
%{conn: conn} do
params = %{
username: "foo2",
email: "foo2@example.org",
password: "dupa.8",
agreement: true
}
res =
conn
|> post("/api/v1/accounts", params)
assert json_response_and_validate_schema(res, 200)
assert %{language: "zh_Hans"} = Pleroma.User.get_by_nickname("foo2")
end
end
describe "GET /api/v1/accounts/:id/lists - account_lists" do
test "returns lists to which the account belongs" do
%{user: user, conn: conn} = oauth_access(["read:lists"])

View file

@ -16,7 +16,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|> SetLocalePlug.call([])
assert "en" == Gettext.get_locale()
assert %{locale: "en"} == conn.assigns
assert %{locale: "en"} = conn.assigns
end
test "use supported locale from `accept-language`" do
@ -30,7 +30,125 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|> SetLocalePlug.call([])
assert "ru" == Gettext.get_locale()
assert %{locale: "ru"} == conn.assigns
assert %{locale: "ru"} = conn.assigns
end
test "fallback to the general language if a variant is not supported" do
conn =
:get
|> conn("/cofe")
|> Conn.put_req_header(
"accept-language",
"ru-CA;q=0.9, en;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "ru" == Gettext.get_locale()
assert %{locale: "ru"} = conn.assigns
end
test "use supported locale with specifiers from `accept-language`" do
conn =
:get
|> conn("/cofe")
|> Conn.put_req_header(
"accept-language",
"zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "zh_Hans" == Gettext.get_locale()
assert %{locale: "zh_Hans"} = conn.assigns
end
test "it assigns all supported locales" do
conn =
:get
|> conn("/cofe")
|> Conn.put_req_header(
"accept-language",
"ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "ru" == Gettext.get_locale()
assert %{locale: "ru", locales: ["ru", "fr", "en"]} = conn.assigns
end
test "it assigns all supported locales in cookie" do
conn =
:get
|> conn("/cofe")
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans,uk,zh-Hant")
|> Conn.put_req_header(
"accept-language",
"ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "zh_Hans" == Gettext.get_locale()
assert %{locale: "zh_Hans", locales: ["zh_Hans", "uk", "zh_Hant", "ru", "fr", "en"]} =
conn.assigns
end
test "fallback to some variant of the language if the unqualified language is not supported" do
conn =
:get
|> conn("/cofe")
|> Conn.put_req_header(
"accept-language",
"zh;q=0.9, en;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "zh_" <> _ = Gettext.get_locale()
assert %{locale: "zh_" <> _} = conn.assigns
end
test "use supported locale from cookie" do
conn =
:get
|> conn("/cofe")
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
|> Conn.put_req_header(
"accept-language",
"ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "zh_Hans" == Gettext.get_locale()
assert %{locale: "zh_Hans"} = conn.assigns
end
test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
conn =
:get
|> conn("/cofe")
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
|> Conn.put_req_header(
"accept-language",
"ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
)
|> SetLocalePlug.call([])
assert "ru" == Gettext.get_locale()
assert %{locale: "ru"} = conn.assigns
end
test "fallback to default if nothing is supported" do
conn =
:get
|> conn("/cofe")
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
|> Conn.put_req_header(
"accept-language",
"x-nonexist"
)
|> SetLocalePlug.call([])
assert "en" == Gettext.get_locale()
assert %{locale: "en"} = conn.assigns
end
test "use default locale if locale from `accept-language` is not supported" do
@ -41,6 +159,6 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|> SetLocalePlug.call([])
assert "en" == Gettext.get_locale()
assert %{locale: "en"} == conn.assigns
assert %{locale: "en"} = conn.assigns
end
end