Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Preferences: list the languages alphabetically by their own name

Nikolai Nyegaardca5627167818
diff --git a/crates/ui-egui/src/i18n/mod.rs b/crates/ui-egui/src/i18n/mod.rs
index 20c0fd046..563d33291 100644
--- a/crates/ui-egui/src/i18n/mod.rs
+++ b/crates/ui-egui/src/i18n/mod.rs
@@ -273,6 +273,16 @@ impl Lang {
LANGUAGES.iter().map(Lang)
}
+ /// Every registered language in the order a picker lists them: English first (the source
+ /// language and the fallback), then the rest by their own name with accents folded, so
+ /// Čeština sits under C rather than after Z. Names in other scripts follow the Latin ones in
+ /// code point order: Greek, Cyrillic, Arabic, then the CJK languages.
+ pub fn for_picker() -> Vec<Lang> {
+ let mut rest: Vec<Lang> = Lang::all().filter(|l| l.code() != Lang::EN.code()).collect();
+ rest.sort_by_cached_key(|l| name_sort_key(l.name()));
+ std::iter::once(Lang::EN).chain(rest).collect()
+ }
+
pub fn name(self) -> &'static str {
self.0.name
}
@@ -3426,3 +3436,40 @@ mod tests {
assert_eq!(dates, interface);
}
}
+
+/// `name` lowercased with the Latin accents that occur in language names folded away, so a
+/// byte-wise sort reads alphabetically. Other scripts are left as they are.
+fn name_sort_key(name: &str) -> String {
+ name.chars()
+ .flat_map(char::to_lowercase)
+ .map(|c| match c {
+ 'à'..='å' | 'ā' | 'ă' | 'ą' => 'a',
+ 'ç' | 'ć' | 'č' => 'c',
+ 'è'..='ë' | 'ē' | 'ě' | 'ę' | 'ế' | 'ệ' => 'e',
+ 'ì'..='ï' | 'ī' => 'i',
+ 'ñ' | 'ń' | 'ň' => 'n',
+ 'ò'..='ö' | 'ø' | 'ő' => 'o',
+ 'ś' | 'š' => 's',
+ 'ù'..='ü' | 'ū' | 'ů' | 'ű' => 'u',
+ 'ý' | 'ÿ' => 'y',
+ 'ź' | 'ż' | 'ž' => 'z',
+ c => c,
+ })
+ .collect()
+}
+
+#[cfg(test)]
+mod picker_order_tests {
+ use super::*;
+
+ #[test]
+ fn picker_lists_english_first_then_names_alphabetically() {
+ let order = Lang::for_picker();
+ assert_eq!(order.first().map(|l| l.code()), Some(Lang::EN.code()));
+ assert_eq!(order.len(), Lang::all().count());
+ let keys: Vec<String> = order[1..].iter().map(|l| name_sort_key(l.name())).collect();
+ assert!(keys.windows(2).all(|w| w[0] <= w[1]), "{keys:?}");
+ assert_eq!(name_sort_key("Čeština"), "cestina");
+ assert_eq!(name_sort_key("Türkçe"), "turkce");
+ }
+}
diff --git a/crates/ui-egui/src/js_ui.rs b/crates/ui-egui/src/js_ui.rs
index df461fc6e..38ab78ddf 100644
--- a/crates/ui-egui/src/js_ui.rs
+++ b/crates/ui-egui/src/js_ui.rs
@@ -271,7 +271,7 @@ pub(crate) fn preferences_body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tok
if ui.selectable_value(&mut app.language, crate::i18n::AUTO.to_string(), tl!("Auto")).clicked() {
ui.close();
}
- for language in crate::i18n::Lang::all() {
+ for language in crate::i18n::Lang::for_picker() {
if ui.selectable_value(&mut app.language, language.code().to_string(), language.name()).clicked() {
ui.close();
}