diff --git a/Cargo.lock b/Cargo.lock index b648d03..81a1dc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -440,6 +440,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", + "pure-rust-locales", "time 0.1.45", "wasm-bindgen", "winapi", @@ -2246,6 +2247,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pure-rust-locales" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45c49fc4f91f35bae654f85ebb3a44d60ac64f11b3166ffa609def390c732d8" + [[package]] name = "quick-xml" version = "0.23.1" diff --git a/Cargo.toml b/Cargo.toml index ece484d..2b6d242 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,7 +110,7 @@ reqwest = { version = "0.11.18", optional = true } nix = { version = "0.26.2", optional = true, features = ["event"] } # clock -chrono = { version = "0.4.26", optional = true } +chrono = { version = "0.4.26", optional = true, features = ["unstable-locales"] } # music mpd_client = { version = "1.2.0", optional = true } diff --git a/docs/modules/Clock.md b/docs/modules/Clock.md index 13932c7..89cbbde 100644 --- a/docs/modules/Clock.md +++ b/docs/modules/Clock.md @@ -8,10 +8,11 @@ Clicking on the widget opens a popup with the time and a calendar. > Type: `clock` -| Name | Type | Default | Description | -|----------------|----------|------------------|---------------------------------------------------------| -| `format` | `string` | `%d/%m/%Y %H:%M` | Date/time format string. | -| `format_popup` | `string` | `%H:%M:%S` | Date/time format string to display in the popup header. | +| Name | Type | Default | Description | +|----------------|----------|------------------------------------|-------------------------------------------------------------------------------------| +| `format` | `string` | `%d/%m/%Y %H:%M` | Date/time format string. | +| `format_popup` | `string` | `%H:%M:%S` | Date/time format string to display in the popup header. | +| `locale` | `string` | `$LC_TIME` or `$LANG` or `'POSIX'` | Locale to use (eg `en_GB`). Defaults to the system language (reading from env var). | > Detail on available tokens can be found here: diff --git a/src/modules/clock.rs b/src/modules/clock.rs index e974466..77252f9 100644 --- a/src/modules/clock.rs +++ b/src/modules/clock.rs @@ -3,12 +3,13 @@ use crate::gtk_helpers::add_class; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::popup::Popup; use crate::{send_async, try_send}; -use chrono::{DateTime, Local}; +use chrono::{DateTime, Local, Locale}; use color_eyre::Result; use glib::Continue; use gtk::prelude::*; use gtk::{Align, Button, Calendar, Label, Orientation}; use serde::Deserialize; +use std::env; use tokio::spawn; use tokio::sync::mpsc; use tokio::time::sleep; @@ -26,6 +27,9 @@ pub struct ClockModule { #[serde(default = "default_popup_format")] format_popup: String, + #[serde(default = "default_locale")] + locale: String, + #[serde(flatten)] pub common: Option, } @@ -38,6 +42,20 @@ fn default_popup_format() -> String { String::from("%H:%M:%S") } +fn default_locale() -> String { + env::var("LC_TIME") + .or_else(|_| env::var("LANG")) + .map(strip_tail) + .unwrap_or_else(|_| "POSIX".to_string()) +} + +fn strip_tail(string: String) -> String { + string + .split_once('.') + .map(|(head, _)| head.to_string()) + .unwrap_or(string) +} + impl Module