From 3c5b1ac6dd60e153899adf5eda03c0e0e21557ed Mon Sep 17 00:00:00 2001 From: Elara Date: Thu, 8 Feb 2024 01:53:03 -0800 Subject: [PATCH] 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 --- Cargo.lock | 7 +++++++ crates/api_crud/Cargo.toml | 1 + crates/api_crud/src/user/create.rs | 9 +++++++++ 3 files changed, 17 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 0f7695df5..abe673cd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/crates/api_crud/Cargo.toml b/crates/api_crud/Cargo.toml index f4f89c2e5..d72ed27c9 100644 --- a/crates/api_crud/Cargo.toml +++ b/crates/api_crud/Cargo.toml @@ -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"] diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 39e375150..5f11384f7 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -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();