Use the Accept-Language header to set new users' language (#4435)

* Use the Accept-Language header to set new users' language

* Implement clippy suggestions

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Elara 2024-02-08 01:53:03 -08:00 committed by GitHub
parent f631f43024
commit 3c5b1ac6dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

7
Cargo.lock generated
View file

@ -8,6 +8,12 @@ version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
[[package]]
name = "accept-language"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f27d075294830fcab6f66e320dab524bc6d048f4a151698e153205559113772"
[[package]]
name = "activitypub_federation"
version = "0.5.1-beta.1"
@ -2608,6 +2614,7 @@ dependencies = [
name = "lemmy_api_crud"
version = "0.19.3"
dependencies = [
"accept-language",
"activitypub_federation",
"actix-web",
"anyhow",

View file

@ -29,6 +29,7 @@ moka.workspace = true
once_cell.workspace = true
anyhow.workspace = true
webmention = "0.5.0"
accept-language = "3.1.0"
[package.metadata.cargo-machete]
ignored = ["futures"]

View file

@ -126,6 +126,14 @@ pub async fn register(
// Also fixes a bug which allows users to log in when registrations are changed to closed.
let accepted_application = Some(!require_registration_application);
// Get the user's preferred language using the Accept-Language header
let language_tag = req.headers().get("Accept-Language").and_then(|hdr| {
accept_language::parse(hdr.to_str().unwrap_or_default())
.first()
// Remove the optional region code
.map(|lang_str| lang_str.split('-').next().unwrap_or_default().to_string())
});
// Create the local user
let local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_person.id)
@ -134,6 +142,7 @@ pub async fn register(
.show_nsfw(Some(data.show_nsfw))
.accepted_application(accepted_application)
.default_listing_type(Some(local_site.default_post_listing_type))
.interface_language(language_tag)
// If its the initial site setup, they are an admin
.admin(Some(!local_site.site_setup))
.build();