Only convert locale one time

This commit is contained in:
Earthgames 2026-03-07 12:26:39 +01:00 committed by galister
parent ad78fdab24
commit 6b3f8a7b42
1 changed files with 16 additions and 10 deletions

View File

@ -130,6 +130,14 @@ pub(super) fn setup_custom_label<S: 'static>(
.ok() .ok()
}), }),
format: format.into(), format: format.into(),
locale: {
let i18n = app.wgui_globals.i18n();
let locale = i18n.get_locale();
match pure_rust_locales::Locale::try_from(locale.to_string().as_str()) {
Ok(loc) => loc,
Err(_) => pure_rust_locales::Locale::en_US,
}
},
}; };
Box::new(move |common, data, _, _| { Box::new(move |common, data, _, _| {
@ -213,6 +221,7 @@ fn battery_on_tick(
struct ClockLabelState { struct ClockLabelState {
timezone: Option<Tz>, timezone: Option<Tz>,
format: Rc<str>, format: Rc<str>,
locale: chrono::Locale,
} }
fn clock_on_tick( fn clock_on_tick(
@ -220,22 +229,19 @@ fn clock_on_tick(
common: &mut event::CallbackDataCommon, common: &mut event::CallbackDataCommon,
data: &mut event::CallbackData, data: &mut event::CallbackData,
) { ) {
let locale = {
let binding = common.i18n();
let locale = binding.get_locale();
match pure_rust_locales::Locale::try_from(locale.to_string().as_str()) {
Ok(loc) => loc,
Err(_) => pure_rust_locales::Locale::en_US,
}
};
let date_time = state.timezone.as_ref().map_or_else( let date_time = state.timezone.as_ref().map_or_else(
|| format!("{}", Local::now().format_localized(&state.format, locale)), || {
format!(
"{}",
Local::now().format_localized(&state.format, state.locale)
)
},
|tz| { |tz| {
format!( format!(
"{}", "{}",
Local::now() Local::now()
.with_timezone(tz) .with_timezone(tz)
.format_localized(&state.format, locale) .format_localized(&state.format, state.locale)
) )
}, },
); );