chore: update to icu 2.0.0

This commit is contained in:
Vukašin Vojinović 2025-09-15 15:13:03 +02:00 • committed by Jeremy Soller
parent 8424905134
commit 901bf3f564
4 changed files with 368 additions and 506 deletions

669
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,11 +9,7 @@ rust-version = "1.85"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
chrono = { version = "0.4", features = ["unstable-locales"] } chrono = { version = "0.4", features = ["unstable-locales"] }
icu = { version = "1.5.0", features = [ icu = { version = "2.0.0", features = ["compiled_data"] }
"experimental",
"compiled_data",
"icu_datetime_experimental",
] }
cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-client-toolkit", rev = "6254f50", optional = true } cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-client-toolkit", rev = "6254f50", optional = true }
cosmic-mime-apps = { git = "https://github.com/pop-os/cosmic-mime-apps.git", optional = true } cosmic-mime-apps = { git = "https://github.com/pop-os/cosmic-mime-apps.git", optional = true }
dirs = "6.0.0" dirs = "6.0.0"
@ -23,8 +19,6 @@ futures = "0.3.31"
gio = { version = "0.21", optional = true } gio = { version = "0.21", optional = true }
glib = { version = "0.21", optional = true } glib = { version = "0.21", optional = true }
glob = "0.3" glob = "0.3"
icu_collator = "1.5"
icu_provider = { version = "1.5", features = ["sync"] }
ignore = "0.4" ignore = "0.4"
image = "0.25" image = "0.25"
libc = "0.2" libc = "0.2"

View file

@ -4,11 +4,12 @@ use i18n_embed::{
DefaultLocalizer, LanguageLoader, Localizer, DefaultLocalizer, LanguageLoader, Localizer,
fluent::{FluentLanguageLoader, fluent_language_loader}, fluent::{FluentLanguageLoader, fluent_language_loader},
}; };
use icu::locid::Locale; use icu::collator::{
use icu_collator::{Collator, CollatorOptions, Numeric}; Collator, CollatorBorrowed, CollatorPreferences, options::CollatorOptions,
use icu_provider::DataLocale; preferences::CollationNumericOrdering,
};
use icu::locale::Locale;
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
use std::str::FromStr;
use std::sync::LazyLock; use std::sync::LazyLock;
#[derive(RustEmbed)] #[derive(RustEmbed)]
@ -25,44 +26,51 @@ pub static LANGUAGE_LOADER: LazyLock<FluentLanguageLoader> = LazyLock::new(|| {
loader loader
}); });
pub static LANGUAGE_SORTER: LazyLock<Collator> = LazyLock::new(|| { pub static LANGUAGE_SORTER: LazyLock<CollatorBorrowed> = LazyLock::new(|| {
let mut options = CollatorOptions::new(); let create_collator = |locale: Locale| {
options.numeric = Some(Numeric::On); let mut prefs = CollatorPreferences::from(locale);
prefs.numeric_ordering = Some(CollationNumericOrdering::True);
Collator::try_new(prefs, CollatorOptions::default()).ok()
};
DataLocale::from_str(&LANGUAGE_LOADER.current_language().to_string()) Locale::try_from_str(&LANGUAGE_LOADER.current_language().to_string())
.or_else(|_| DataLocale::from_str(&LANGUAGE_LOADER.fallback_language().to_string())) .ok()
.ok() .and_then(create_collator)
.and_then(|locale| Collator::try_new(&locale, options).ok()) .or_else(|| {
.or_else(|| { Locale::try_from_str(&LANGUAGE_LOADER.fallback_language().to_string())
let locale = DataLocale::from_str("en-US").expect("en-US is a valid BCP-47 tag"); .ok()
Collator::try_new(&locale, options).ok() .and_then(create_collator)
}) })
.expect("Creating a collator from the system's current language, the fallback language, or American English should succeed") .unwrap_or_else(|| {
let locale = Locale::try_from_str("en-US").expect("en-US is a valid BCP-47 tag");
create_collator(locale)
.expect("Creating a collator from the system's current language, the fallback language, or American English should succeed")
})
}); });
pub static LOCALE: LazyLock<Locale> = LazyLock::new(|| { pub static LOCALE: LazyLock<Locale> = LazyLock::new(|| {
fn get_local() -> Result<Locale, Box<dyn std::error::Error>> { for var in ["LC_TIME", "LC_ALL", "LANG"] {
let locale = std::env::var("LC_TIME").or_else(|_| std::env::var("LANG"))?; if let Ok(locale_str) = std::env::var(var) {
let cleaned_locale = locale_str
.split('.')
.next()
.unwrap_or(&locale_str)
.replace('_', "-");
let locale = locale if let Ok(locale) = Locale::try_from_str(&cleaned_locale) {
.split('.') return locale;
.next() }
.ok_or(format!("Can't split the locale {locale}"))?;
let locale = Locale::from_str(locale).map_err(|e| format!("{e:?}"))?; // Try language-only fallback (e.g., "en" from "en-US")
if let Some(lang) = cleaned_locale.split('-').next() {
Ok(locale) if let Ok(locale) = Locale::try_from_str(lang) {
} return locale;
}
match get_local() { }
Ok(locale) => locale,
Err(e) => {
log::error!("can't get locale {e}");
Locale::default()
} }
} }
log::warn!("No valid locale found in environment, using fallback");
Locale::try_from_str("en-US").expect("Failed to parse fallback locale 'en-US'")
}); });
#[macro_export] #[macro_export]
@ -86,6 +94,6 @@ pub fn localize() {
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages(); let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
if let Err(error) = localizer.select(&requested_languages) { if let Err(error) = localizer.select(&requested_languages) {
eprintln!("Error while loading language for App List {}", error); eprintln!("Error while loading language for COSMIC Files {}", error);
} }
} }

View file

@ -36,11 +36,15 @@ use cosmic::{
}, },
}; };
use chrono::{DateTime, Datelike, Timelike, Utc}; use chrono::{Datelike, Timelike, Utc};
use i18n_embed::LanguageLoader; use i18n_embed::LanguageLoader;
use icu::datetime::{ use icu::{
DateTimeFormatter, DateTimeFormatterOptions, datetime::{
options::{components, preferences}, DateTimeFormatter, DateTimeFormatterPreferences, fieldsets,
input::{Date, DateTime, Time},
options::TimePrecision,
},
locale::preferences::extensions::unicode::keywords::HourCycle,
}; };
use image::ImageDecoder; use image::ImageDecoder;
use jxl_oxide::integration::JxlDecoder; use jxl_oxide::integration::JxlDecoder;
@ -399,49 +403,45 @@ fn set_mode_part(mode: u32, shift: u32, bits: u32) -> u32 {
(mode & !(0o7 << shift)) | (bits << shift) (mode & !(0o7 << shift)) | (bits << shift)
} }
fn date_time_formatter(military_time: bool) -> DateTimeFormatter { fn date_time_formatter(military_time: bool) -> DateTimeFormatter<fieldsets::YMDT> {
let mut bag = components::Bag::empty(); let mut prefs = DateTimeFormatterPreferences::from(LOCALE.clone());
bag.day = Some(components::Day::NumericDayOfMonth); prefs.hour_cycle = Some(if military_time {
bag.month = Some(components::Month::Short); HourCycle::H23
bag.year = Some(components::Year::Numeric);
bag = bag.merge(time_bag(military_time));
let options = DateTimeFormatterOptions::Components(bag);
DateTimeFormatter::try_new_experimental(&LOCALE.as_ref().into(), options)
.expect("failed to create DateTimeFormatter")
}
fn time_formatter(military_time: bool) -> DateTimeFormatter {
let options = DateTimeFormatterOptions::Components(time_bag(military_time));
DateTimeFormatter::try_new_experimental(&LOCALE.as_ref().into(), options)
.expect("failed to create DateTimeFormatter")
}
fn time_bag(military_time: bool) -> components::Bag {
let mut bag = components::Bag::empty();
bag.hour = Some(components::Numeric::Numeric);
bag.minute = Some(components::Numeric::Numeric);
let hour_cycle = if military_time {
preferences::HourCycle::H23
} else { } else {
preferences::HourCycle::H12 HourCycle::H12
}; });
bag.preferences = Some(preferences::Bag::from_hour_cycle(hour_cycle));
bag let mut fs = fieldsets::YMDT::medium();
fs = fs.with_time_precision(TimePrecision::Minute);
DateTimeFormatter::try_new(prefs, fs).expect("failed to create DateTimeFormatter")
}
fn time_formatter(military_time: bool) -> DateTimeFormatter<fieldsets::T> {
let mut prefs = DateTimeFormatterPreferences::from(LOCALE.clone());
prefs.hour_cycle = Some(if military_time {
HourCycle::H23
} else {
HourCycle::H12
});
let mut fs = fieldsets::T::medium();
fs = fs.with_time_precision(TimePrecision::Minute);
DateTimeFormatter::try_new(prefs, fs).expect("failed to create DateTimeFormatter")
} }
struct FormatTime<'a> { struct FormatTime<'a> {
pub time: SystemTime, pub time: SystemTime,
pub date_time_formatter: &'a DateTimeFormatter, pub date_time_formatter: &'a DateTimeFormatter<fieldsets::YMDT>,
pub time_formatter: &'a DateTimeFormatter, pub time_formatter: &'a DateTimeFormatter<fieldsets::T>,
} }
impl<'a> FormatTime<'a> { impl<'a> FormatTime<'a> {
fn from_secs( fn from_secs(
secs: i64, secs: i64,
date_time_formatter: &'a DateTimeFormatter, date_time_formatter: &'a DateTimeFormatter<fieldsets::YMDT>,
time_formatter: &'a DateTimeFormatter, time_formatter: &'a DateTimeFormatter<fieldsets::T>,
) -> Option<Self> { ) -> Option<Self> {
// This looks convoluted because we need to ensure the units match up // This looks convoluted because we need to ensure the units match up
let secs: u64 = secs.try_into().ok()?; let secs: u64 = secs.try_into().ok()?;
@ -464,33 +464,34 @@ impl Display for FormatTime<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let datetime = chrono::DateTime::<chrono::Local>::from(self.time); let datetime = chrono::DateTime::<chrono::Local>::from(self.time);
let now = chrono::Local::now(); let now = chrono::Local::now();
let icu_datetime = icu::calendar::DateTime::try_new_iso_datetime( let icu_datetime = DateTime {
datetime.year(), date: Date::try_new_gregorian(
datetime.month() as u8, datetime.year(),
datetime.day() as u8, datetime.month() as u8,
datetime.hour() as u8, datetime.day() as u8,
datetime.minute() as u8, )
datetime.second() as u8, .unwrap(),
) time: Time::try_new(
.expect("failed to construct DateTime") datetime.hour() as u8,
.to_any(); datetime.minute() as u8,
datetime.second() as u8,
0,
)
.unwrap(),
};
if datetime.date_naive() == now.date_naive() { if datetime.date_naive() == now.date_naive() {
write!( write!(
f, f,
"{}, {}", "{}, {}",
fl!("today"), fl!("today"),
self.time_formatter self.time_formatter.format(&icu_datetime).to_string()
.format(&icu_datetime)
.map_err(|_| fmt::Error)?
) )
} else { } else {
write!( write!(
f, f,
"{}", "{}",
self.date_time_formatter self.date_time_formatter.format(&icu_datetime).to_string()
.format(&icu_datetime)
.map_err(|_| fmt::Error)?
) )
} }
} }
@ -498,8 +499,8 @@ impl Display for FormatTime<'_> {
const fn format_time<'a>( const fn format_time<'a>(
time: SystemTime, time: SystemTime,
date_time_formatter: &'a DateTimeFormatter, date_time_formatter: &'a DateTimeFormatter<fieldsets::YMDT>,
time_formatter: &'a DateTimeFormatter, time_formatter: &'a DateTimeFormatter<fieldsets::T>,
) -> FormatTime<'a> { ) -> FormatTime<'a> {
FormatTime { FormatTime {
time, time,
@ -1161,11 +1162,11 @@ pub fn scan_recents(sizes: IconSizes) -> Vec<Item> {
None => continue, None => continue,
Some(path) => path, Some(path) => path,
}; };
let last_edit = match bookmark.modified.parse::<DateTime<Utc>>() { let last_edit = match bookmark.modified.parse::<chrono::DateTime<Utc>>() {
Ok(last_edit) => last_edit, Ok(last_edit) => last_edit,
Err(_) => continue, Err(_) => continue,
}; };
let last_visit = match bookmark.visited.parse::<DateTime<Utc>>() { let last_visit = match bookmark.visited.parse::<chrono::DateTime<Utc>>() {
Ok(last_visit) => last_visit, Ok(last_visit) => last_visit,
Err(_) => continue, Err(_) => continue,
}; };
@ -2460,8 +2461,8 @@ pub struct Tab {
modifiers: Modifiers, modifiers: Modifiers,
last_right_click: Option<usize>, last_right_click: Option<usize>,
search_context: Option<SearchContext>, search_context: Option<SearchContext>,
date_time_formatter: DateTimeFormatter, date_time_formatter: DateTimeFormatter<fieldsets::YMDT>,
time_formatter: DateTimeFormatter, time_formatter: DateTimeFormatter<fieldsets::T>,
watch_drag: bool, watch_drag: bool,
window_id: Option<window::Id>, window_id: Option<window::Id>,
} }