Skip to content

Commit

Permalink
feat(clock): localization support
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Jul 3, 2023
1 parent 7c8d466 commit b310ea7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
9 changes: 5 additions & 4 deletions docs/modules/Clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
27 changes: 24 additions & 3 deletions src/modules/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>,
}
Expand All @@ -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 = ();
Expand Down Expand Up @@ -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)
});
Expand Down Expand Up @@ -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)
});
Expand Down

0 comments on commit b310ea7

Please sign in to comment.