feat(clock): localization support

This commit is contained in:
Jake Stanger 2023-07-03 23:20:37 +01:00
parent 7c8d4668bc
commit b310ea7636
No known key found for this signature in database
GPG Key ID: C51FC8F9CB0BEA61
4 changed files with 37 additions and 8 deletions

7
Cargo.lock generated
View File

@ -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"

View File

@ -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 }

View File

@ -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: <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>

View File

@ -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<CommonConfig>,
}
@ -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<Button> for ClockModule {
type SendMessage = DateTime<Local>;
type ReceiveMessage = ();
@ -82,9 +100,10 @@ impl Module<Button> for ClockModule {
});
let format = self.format.clone();
let locale = Locale::try_from(self.locale.as_str()).unwrap_or(Locale::POSIX);
context.widget_rx.attach(None, move |date| {
let date_string = format!("{}", date.format(&format));
let date_string = format!("{}", date.format_localized(&format, locale));
label.set_label(&date_string);
Continue(true)
});
@ -115,8 +134,10 @@ impl Module<Button> for ClockModule {
container.add(&calendar);
let format = self.format_popup;
let locale = Locale::try_from(self.locale.as_str()).unwrap_or(Locale::POSIX);
rx.attach(None, move |date| {
let date_string = format!("{}", date.format(&format));
let date_string = format!("{}", date.format_localized(&format, locale));
clock.set_label(&date_string);
Continue(true)
});