Allow user to register with custom language

This commit is contained in:
Tusooa Zhu 2022-03-02 01:41:13 -05:00
commit e644f8dea5
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
7 changed files with 93 additions and 3 deletions

View file

@ -747,7 +747,8 @@ defmodule Pleroma.User do
:emoji,
:accepts_chat_messages,
:registration_reason,
:birthday
:birthday,
:language
])
|> validate_required([:name, :nickname, :password, :password_confirmation])
|> validate_confirmation(:password)

View file

@ -549,6 +549,11 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
nullable: true,
description: "User's birthday",
format: :date
},
language: %Schema{
type: :string,
nullable: true,
description: "User's preferred language for emails"
}
},
example: %{

View file

@ -35,6 +35,14 @@ defmodule Pleroma.Web.Gettext do
|> 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()

View file

@ -221,7 +221,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
# Note: param name is indeed :discoverable (not an error)
|> Maps.put_if_present(:is_discoverable, params[:discoverable])
|> Maps.put_if_present(:birthday, params[:birthday])
|> Maps.put_if_present(:language, params[:language])
|> Maps.put_if_present(:language, Pleroma.Web.Gettext.normalize_locale(params[:language]))
# What happens here:
#

View file

@ -25,7 +25,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlug do
defp normalize_language_codes(codes) do
codes
|> Enum.map(fn code -> String.replace(code, "-", "_") end)
|> Enum.map(fn code -> Pleroma.Web.Gettext.normalize_locale(code) end)
end
defp extract_preferred_language(conn) do

View file

@ -12,6 +12,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
alias Pleroma.UserInviteToken
def register_user(params, opts \\ []) do
fallback_language = Gettext.get_locale()
params =
params
|> Map.take([:email, :token, :password])
@ -21,6 +23,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|> Map.put(:password_confirmation, params[:password])
|> Map.put(:registration_reason, params[:reason])
|> Map.put(:birthday, params[:birthday])
|> Map.put(
:language,
Pleroma.Web.Gettext.normalize_locale(params[:language]) || fallback_language
)
if Pleroma.Config.get([:instance, :registrations_open]) do
create_user(params, opts)