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]
anyhow = "1"
chrono = { version = "0.4", features = ["unstable-locales"] }
icu = { version = "1.5.0", features = [
"experimental",
"compiled_data",
"icu_datetime_experimental",
] }
icu = { version = "2.0.0", features = ["compiled_data"] }
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 }
dirs = "6.0.0"
@ -23,8 +19,6 @@ futures = "0.3.31"
gio = { version = "0.21", optional = true }
glib = { version = "0.21", optional = true }
glob = "0.3"
icu_collator = "1.5"
icu_provider = { version = "1.5", features = ["sync"] }
ignore = "0.4"
image = "0.25"
libc = "0.2"

View file

@ -4,11 +4,12 @@ use i18n_embed::{
DefaultLocalizer, LanguageLoader, Localizer,
fluent::{FluentLanguageLoader, fluent_language_loader},
};
use icu::locid::Locale;
use icu_collator::{Collator, CollatorOptions, Numeric};
use icu_provider::DataLocale;
use icu::collator::{
Collator, CollatorBorrowed, CollatorPreferences, options::CollatorOptions,
preferences::CollationNumericOrdering,
};
use icu::locale::Locale;
use rust_embed::RustEmbed;
use std::str::FromStr;
use std::sync::LazyLock;
#[derive(RustEmbed)]
@ -25,44 +26,51 @@ pub static LANGUAGE_LOADER: LazyLock<FluentLanguageLoader> = LazyLock::new(|| {
loader
});
pub static LANGUAGE_SORTER: LazyLock<Collator> = LazyLock::new(|| {
let mut options = CollatorOptions::new();
options.numeric = Some(Numeric::On);
pub static LANGUAGE_SORTER: LazyLock<CollatorBorrowed> = LazyLock::new(|| {
let create_collator = |locale: Locale| {
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())
.or_else(|_| DataLocale::from_str(&LANGUAGE_LOADER.fallback_language().to_string()))
.ok()
.and_then(|locale| Collator::try_new(&locale, options).ok())
.or_else(|| {
let locale = DataLocale::from_str("en-US").expect("en-US is a valid BCP-47 tag");
Collator::try_new(&locale, options).ok()
})
.expect("Creating a collator from the system's current language, the fallback language, or American English should succeed")
Locale::try_from_str(&LANGUAGE_LOADER.current_language().to_string())
.ok()
.and_then(create_collator)
.or_else(|| {
Locale::try_from_str(&LANGUAGE_LOADER.fallback_language().to_string())
.ok()
.and_then(create_collator)
})
.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(|| {
fn get_local() -> Result<Locale, Box<dyn std::error::Error>> {
let locale = std::env::var("LC_TIME").or_else(|_| std::env::var("LANG"))?;
for var in ["LC_TIME", "LC_ALL", "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
.split('.')
.next()
.ok_or(format!("Can't split the locale {locale}"))?;
if let Ok(locale) = Locale::try_from_str(&cleaned_locale) {
return locale;
}
let locale = Locale::from_str(locale).map_err(|e| format!("{e:?}"))?;
Ok(locale)
}
match get_local() {
Ok(locale) => locale,
Err(e) => {
log::error!("can't get locale {e}");
Locale::default()
// Try language-only fallback (e.g., "en" from "en-US")
if let Some(lang) = cleaned_locale.split('-').next() {
if let Ok(locale) = Locale::try_from_str(lang) {
return locale;
}
}
}
}
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]
@ -86,6 +94,6 @@ pub fn localize() {
let requested_languages = i18n_embed::DesktopLanguageRequester::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 icu::datetime::{
DateTimeFormatter, DateTimeFormatterOptions,
options::{components, preferences},
use icu::{
datetime::{
DateTimeFormatter, DateTimeFormatterPreferences, fieldsets,
input::{Date, DateTime, Time},
options::TimePrecision,
},
locale::preferences::extensions::unicode::keywords::HourCycle,
};
use image::ImageDecoder;
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)
}
fn date_time_formatter(military_time: bool) -> DateTimeFormatter {
let mut bag = components::Bag::empty();
bag.day = Some(components::Day::NumericDayOfMonth);
bag.month = Some(components::Month::Short);
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
fn date_time_formatter(military_time: bool) -> DateTimeFormatter<fieldsets::YMDT> {
let mut prefs = DateTimeFormatterPreferences::from(LOCALE.clone());
prefs.hour_cycle = Some(if military_time {
HourCycle::H23
} else {
preferences::HourCycle::H12
};
bag.preferences = Some(preferences::Bag::from_hour_cycle(hour_cycle));
bag
HourCycle::H12
});
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> {
pub time: SystemTime,
pub date_time_formatter: &'a DateTimeFormatter,
pub time_formatter: &'a DateTimeFormatter,
pub date_time_formatter: &'a DateTimeFormatter<fieldsets::YMDT>,
pub time_formatter: &'a DateTimeFormatter<fieldsets::T>,
}
impl<'a> FormatTime<'a> {
fn from_secs(
secs: i64,
date_time_formatter: &'a DateTimeFormatter,
time_formatter: &'a DateTimeFormatter,
date_time_formatter: &'a DateTimeFormatter<fieldsets::YMDT>,
time_formatter: &'a DateTimeFormatter<fieldsets::T>,
) -> Option<Self> {
// This looks convoluted because we need to ensure the units match up
let secs: u64 = secs.try_into().ok()?;
@ -464,33 +464,34 @@ impl Display for FormatTime<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let datetime = chrono::DateTime::<chrono::Local>::from(self.time);
let now = chrono::Local::now();
let icu_datetime = icu::calendar::DateTime::try_new_iso_datetime(
datetime.year(),
datetime.month() as u8,
datetime.day() as u8,
datetime.hour() as u8,
datetime.minute() as u8,
datetime.second() as u8,
)
.expect("failed to construct DateTime")
.to_any();
let icu_datetime = DateTime {
date: Date::try_new_gregorian(
datetime.year(),
datetime.month() as u8,
datetime.day() as u8,
)
.unwrap(),
time: Time::try_new(
datetime.hour() as u8,
datetime.minute() as u8,
datetime.second() as u8,
0,
)
.unwrap(),
};
if datetime.date_naive() == now.date_naive() {
write!(
f,
"{}, {}",
fl!("today"),
self.time_formatter
.format(&icu_datetime)
.map_err(|_| fmt::Error)?
self.time_formatter.format(&icu_datetime).to_string()
)
} else {
write!(
f,
"{}",
self.date_time_formatter
.format(&icu_datetime)
.map_err(|_| fmt::Error)?
self.date_time_formatter.format(&icu_datetime).to_string()
)
}
}
@ -498,8 +499,8 @@ impl Display for FormatTime<'_> {
const fn format_time<'a>(
time: SystemTime,
date_time_formatter: &'a DateTimeFormatter,
time_formatter: &'a DateTimeFormatter,
date_time_formatter: &'a DateTimeFormatter<fieldsets::YMDT>,
time_formatter: &'a DateTimeFormatter<fieldsets::T>,
) -> FormatTime<'a> {
FormatTime {
time,
@ -1161,11 +1162,11 @@ pub fn scan_recents(sizes: IconSizes) -> Vec<Item> {
None => continue,
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,
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,
Err(_) => continue,
};
@ -2460,8 +2461,8 @@ pub struct Tab {
modifiers: Modifiers,
last_right_click: Option<usize>,
search_context: Option<SearchContext>,
date_time_formatter: DateTimeFormatter,
time_formatter: DateTimeFormatter,
date_time_formatter: DateTimeFormatter<fieldsets::YMDT>,
time_formatter: DateTimeFormatter<fieldsets::T>,
watch_drag: bool,
window_id: Option<window::Id>,
}