Merge pull request #116 from joshuamegnauth54/issue-103-localize-datetime-password-prompt

Localize date strings
This commit is contained in:
Jeremy Soller 2024-08-28 21:51:08 -06:00 committed by GitHub
commit cd9fc48c6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 31 additions and 10 deletions

View file

@ -912,14 +912,13 @@ impl cosmic::Application for App {
let mut column = widget::column::with_capacity(2).padding(16.0).spacing(12.0);
let dt = chrono::Local::now();
let locale = *crate::localize::LANGUAGE_CHRONO;
//TODO: localized format
let date = dt.format("%A, %B %-d");
let date = dt.format_localized("%A, %B %-d", locale);
column = column
.push(widget::text::title2(format!("{}", date)).style(style::Text::Accent));
//TODO: localized format
let time = dt.format("%R");
let time = dt.format_localized("%R", locale);
column = column.push(
widget::text(format!("{}", time))
.size(112.0)

View file

@ -1,6 +1,9 @@
// SPDX-License-Identifier: GPL-3.0-only
use std::sync::OnceLock;
use std::{
str::FromStr,
sync::{LazyLock, OnceLock},
};
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
@ -13,6 +16,19 @@ use rust_embed::RustEmbed;
struct Localizations;
pub static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new();
pub static LANGUAGE_CHRONO: LazyLock<chrono::Locale> = LazyLock::new(|| {
std::env::var("LC_TIME")
.ok()
.or_else(|| std::env::var("LANG").ok())
.and_then(|locale_full| {
// Split LANG because it may be set to a locale such as en_US.UTF8
locale_full
.split('.')
.next()
.and_then(|locale| chrono::Locale::from_str(locale).ok())
})
.unwrap_or(chrono::Locale::en_US)
});
#[macro_export]
macro_rules! fl {

View file

@ -564,14 +564,13 @@ impl cosmic::Application for App {
let mut column = widget::column::with_capacity(2).padding(16.0);
let dt = chrono::Local::now();
let locale = *crate::localize::LANGUAGE_CHRONO;
//TODO: localized format
let date = dt.format("%A, %B %-d");
let date = dt.format_localized("%A, %B %-d", locale);
column = column
.push(widget::text::title2(format!("{}", date)).style(style::Text::Accent));
//TODO: localized format
let time = dt.format("%R");
let time = dt.format_localized("%R", locale);
column = column.push(
widget::text(format!("{}", time))
.size(112.0)