chiark / gitweb /
Get the default language from the locale.
authorSimon Tatham <anakin@pobox.com>
Thu, 4 Jan 2024 21:09:36 +0000 (21:09 +0000)
committerSimon Tatham <anakin@pobox.com>
Thu, 4 Jan 2024 21:10:35 +0000 (21:10 +0000)
Turns out Rust has a handy crate for that.

Cargo.toml
src/posting.rs

index 2bbdf2e3b65f4ae1dbb444e01acab57fb8dc56cb..6f1af20d976ab5008a0ef69ab140bc2939eae9f6 100644 (file)
@@ -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]
index b872b50a9e44f36f4af0f93fd929146654ceed7f..67f1fee538b8d60d037391debd11822ad838aaa8 100644 (file)
@@ -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(),
             },
         })
     }