mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
feat(clock): localization support
This commit is contained in:
parent
7c8d4668bc
commit
b310ea7636
4 changed files with 37 additions and 8 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -440,6 +440,7 @@ dependencies = [
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"pure-rust-locales",
|
||||||
"time 0.1.45",
|
"time 0.1.45",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"winapi",
|
"winapi",
|
||||||
|
@ -2246,6 +2247,12 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pure-rust-locales"
|
||||||
|
version = "0.5.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b45c49fc4f91f35bae654f85ebb3a44d60ac64f11b3166ffa609def390c732d8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quick-xml"
|
name = "quick-xml"
|
||||||
version = "0.23.1"
|
version = "0.23.1"
|
||||||
|
|
|
@ -110,7 +110,7 @@ reqwest = { version = "0.11.18", optional = true }
|
||||||
nix = { version = "0.26.2", optional = true, features = ["event"] }
|
nix = { version = "0.26.2", optional = true, features = ["event"] }
|
||||||
|
|
||||||
# clock
|
# clock
|
||||||
chrono = { version = "0.4.26", optional = true }
|
chrono = { version = "0.4.26", optional = true, features = ["unstable-locales"] }
|
||||||
|
|
||||||
# music
|
# music
|
||||||
mpd_client = { version = "1.2.0", optional = true }
|
mpd_client = { version = "1.2.0", optional = true }
|
||||||
|
|
|
@ -8,10 +8,11 @@ Clicking on the widget opens a popup with the time and a calendar.
|
||||||
|
|
||||||
> Type: `clock`
|
> Type: `clock`
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|----------------|----------|------------------|---------------------------------------------------------|
|
|----------------|----------|------------------------------------|-------------------------------------------------------------------------------------|
|
||||||
| `format` | `string` | `%d/%m/%Y %H:%M` | Date/time format string. |
|
| `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. |
|
| `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>
|
> Detail on available tokens can be found here: <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,13 @@ use crate::gtk_helpers::add_class;
|
||||||
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
||||||
use crate::popup::Popup;
|
use crate::popup::Popup;
|
||||||
use crate::{send_async, try_send};
|
use crate::{send_async, try_send};
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local, Locale};
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use glib::Continue;
|
use glib::Continue;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Align, Button, Calendar, Label, Orientation};
|
use gtk::{Align, Button, Calendar, Label, Orientation};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::env;
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
@ -26,6 +27,9 @@ pub struct ClockModule {
|
||||||
#[serde(default = "default_popup_format")]
|
#[serde(default = "default_popup_format")]
|
||||||
format_popup: String,
|
format_popup: String,
|
||||||
|
|
||||||
|
#[serde(default = "default_locale")]
|
||||||
|
locale: String,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub common: Option<CommonConfig>,
|
pub common: Option<CommonConfig>,
|
||||||
}
|
}
|
||||||
|
@ -38,6 +42,20 @@ fn default_popup_format() -> String {
|
||||||
String::from("%H:%M:%S")
|
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 {
|
impl Module<Button> for ClockModule {
|
||||||
type SendMessage = DateTime<Local>;
|
type SendMessage = DateTime<Local>;
|
||||||
type ReceiveMessage = ();
|
type ReceiveMessage = ();
|
||||||
|
@ -82,9 +100,10 @@ impl Module<Button> for ClockModule {
|
||||||
});
|
});
|
||||||
|
|
||||||
let format = self.format.clone();
|
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| {
|
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);
|
label.set_label(&date_string);
|
||||||
Continue(true)
|
Continue(true)
|
||||||
});
|
});
|
||||||
|
@ -115,8 +134,10 @@ impl Module<Button> for ClockModule {
|
||||||
container.add(&calendar);
|
container.add(&calendar);
|
||||||
|
|
||||||
let format = self.format_popup;
|
let format = self.format_popup;
|
||||||
|
let locale = Locale::try_from(self.locale.as_str()).unwrap_or(Locale::POSIX);
|
||||||
|
|
||||||
rx.attach(None, move |date| {
|
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);
|
clock.set_label(&date_string);
|
||||||
Continue(true)
|
Continue(true)
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue