From 3c8f5b8c3187a8633e0137c8566294010fd97c6b Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 24 Mar 2024 16:16:53 +0100 Subject: [PATCH] Get user language from GLib --- Cargo.lock | 1 + Cargo.toml | 1 + src/db/mod.rs | 7 +++---- src/main.rs | 1 + src/util.rs | 18 ++++++++++++++++++ 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 src/util.rs diff --git a/Cargo.lock b/Cargo.lock index ee2e684..39e9203 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1050,6 +1050,7 @@ dependencies = [ "gettext-rs", "gstreamer-play", "gtk4", + "lazy_static", "libadwaita", "log", "mpris-player", diff --git a/Cargo.toml b/Cargo.toml index a43626d..f893416 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ fragile = "2" gettext-rs = { version = "0.7", features = ["gettext-system"] } gstreamer-play = "0.22" gtk = { package = "gtk4", version = "0.7", features = ["v4_12", "blueprint"] } +lazy_static = "1" log = "0.4" mpris-player = "0.6" once_cell = "1" diff --git a/src/db/mod.rs b/src/db/mod.rs index fa531b7..5de4e74 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -17,6 +17,8 @@ use diesel::{ use diesel_migrations::{EmbeddedMigrations, MigrationHarness}; use serde::{Deserialize, Serialize}; +use crate::util; + // This makes the SQL migration scripts accessible from the code. const MIGRATIONS: EmbeddedMigrations = diesel_migrations::embed_migrations!(); @@ -48,10 +50,7 @@ impl TranslatedString { /// generic translation exists (which is a bug in the data), an empty string is /// returned and a warning is logged. pub fn get(&self) -> &str { - // TODO: Get language from locale. - let lang = "generic"; - - match self.0.get(lang) { + match self.0.get(&*util::LANG) { Some(s) => s, None => match self.0.get("generic") { Some(s) => s, diff --git a/src/main.rs b/src/main.rs index 1da036b..906be97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ mod recording_tile; mod search_entry; mod search_tag; mod tag_tile; +mod util; mod welcome_page; mod window; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..cda86b5 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,18 @@ +use gtk::glib; +use lazy_static::lazy_static; + +lazy_static! { + /// The user's language code. + pub static ref LANG: String = { + let lang = match glib::language_names().first() { + Some(language_name) => match language_name.split('_').next() { + Some(lang) => lang.to_string(), + None => "generic".to_string(), + }, + None => "generic".to_string(), + }; + + log::info!("Intialized user language to '{lang}'."); + lang + }; +}