Skip to content

Commit

Permalink
Replace lexical-sort with icu
Browse files Browse the repository at this point in the history
`lexical-sort` is unmaintained. The author recommends switching to the
official set of ICU crates.
  • Loading branch information
joshuamegnauth54 authored and jackpot51 committed Jul 9, 2024
1 parent e64bdac commit d7e4693
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 3 deletions.
261 changes: 260 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dirs = "5"
env_logger = "0.10.0"
grep = "0.3.1"
ignore = "0.4.21"
lexical-sort = "0.3.1"
log = "0.4.20"
notify = "6.1.1"
open = "5.0.2"
Expand All @@ -26,6 +25,8 @@ tokio = { version = "1", features = ["process", "time"] }
syntect = "5.1.0"
two-face = "0.3.0"
# Internationalization
icu_collator = "1.5"
icu_provider = { version = "1.5", features = ["sync"] }
i18n-embed = { version = "0.14", features = ["fluent-system", "desktop-requester"] }
i18n-embed-fl = "0.7"
rust-embed = "8"
Expand Down
23 changes: 23 additions & 0 deletions src/localize.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// SPDX-License-Identifier: GPL-3.0-only

use std::str::FromStr;
use std::sync::OnceLock;

use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
};
use icu_collator::{Collator, CollatorOptions, Numeric};
use icu_provider::DataLocale;
use rust_embed::RustEmbed;

#[derive(RustEmbed)]
#[folder = "i18n/"]
struct Localizations;

pub static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new();
pub static LANGUAGE_SORTER: OnceLock<Collator> = OnceLock::new();

#[macro_export]
macro_rules! fl {
Expand Down Expand Up @@ -51,3 +55,22 @@ pub fn localize() {
eprintln!("Error while loading language for App List {}", error);
}
}

pub fn sorter() -> &'static Collator {
LANGUAGE_SORTER.get_or_init(|| {
let mut options = CollatorOptions::new();
options.numeric = Some(Numeric::On);
let localizer = localizer();
let language_loader = localizer.language_loader();

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")
})
}
Loading

0 comments on commit d7e4693

Please sign in to comment.