From 4e46e72b8c17f4c7574b73e2c197f136de3a1567 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 4 Jan 2024 21:09:36 +0000 Subject: [PATCH] Get the default language from the locale. Turns out Rust has a handy crate for that. --- Cargo.toml | 1 + src/posting.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2bbdf2e..6f1af20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ reqwest = { version = "0.11.23", features = ["blocking"] } serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" strum = { version = "0.25.0", features = ["derive"] } +sys-locale = "0.3.1" unicode-width = "0.1.5" [target.'cfg(unix)'.dependencies] diff --git a/src/posting.rs b/src/posting.rs index b872b50..67f1fee 100644 --- a/src/posting.rs +++ b/src/posting.rs @@ -2,6 +2,7 @@ use itertools::Itertools; use std::cmp::max; use std::iter::once; use strum::IntoEnumIterator; +use sys_locale::get_locale; use super::client::{Client, ClientError}; use super::coloured_string::ColouredString; @@ -26,6 +27,14 @@ pub struct Post { pub m: PostMetadata, } +fn default_language() -> String { + get_locale().as_deref() + .and_then(|s| s.split('-').next()) + .map(|s| if s.len() == 0 { "en" } else { s }) + .unwrap_or("en") + .to_owned() +} + impl Post { pub fn new() -> Self { Post { @@ -34,7 +43,7 @@ impl Post { in_reply_to_id: None, visibility: Visibility::Public, content_warning: None, - language: "en".to_owned(), // FIXME: better default + language: default_language(), }, } } @@ -58,7 +67,7 @@ impl Post { in_reply_to_id: Some(id.to_owned()), visibility: st.visibility, // match the existing vis content_warning: None, - language: "en".to_owned(), // FIXME: better default + language: default_language(), }, }) } -- 2.30.2