Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Preferences: list the languages alphabetically by their own name

Nikolai Nyegaard58791345029f
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 8b4043787..ebab15050 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -1984,8 +1984,8 @@ fn preferences(app: &crate::DesignApp, ui: &mut egui::Ui, d: &mut Dialog, max_he
egui::ComboBox::from_id_salt("pref_language").width(180.0).height(400.0).selected_text(crate::rtl::widget(ui, current)).show_ui(
ui,
|ui| {
- for (code, name) in crate::i18n::LANGUAGES {
- if ui.selectable_label(*code == app.ui.language, crate::rtl::widget(ui, *name)).clicked() {
+ for (code, name) in crate::i18n::picker_languages() {
+ if ui.selectable_label(code == app.ui.language, crate::rtl::widget(ui, name)).clicked() {
// Applied by `show` right after this frame, which has `&mut DesignApp`.
d.fields.insert("applyLanguage".into(), json!(code));
}
diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs
index efe089ef9..60b827dc3 100644
--- a/crates/ui-egui/src/i18n.rs
+++ b/crates/ui-egui/src/i18n.rs
@@ -34,6 +34,16 @@ pub const LANGUAGES: &[(&str, &str)] = &[
("ka", "ქართული"),
];
+/// The interface languages in the order the Preferences picker lists them: English first (the
+/// source language), then the rest by their own name with accents folded, so Čeština sits under
+/// C. Names in other scripts follow the Latin ones in code point order: Cyrillic, Arabic,
+/// Georgian, then the CJK languages.
+pub fn picker_languages() -> Vec<(&'static str, &'static str)> {
+ let mut rest: Vec<(&str, &str)> = LANGUAGES.iter().copied().filter(|(code, _)| !code.is_empty()).collect();
+ rest.sort_by_cached_key(|(_, name)| name_sort_key(name));
+ std::iter::once(LANGUAGES[0]).chain(rest).collect()
+}
+
/// English → [German, French, Spanish, Japanese, Simplified Chinese].
const TABLE: &[(&str, [&str; 5])] = &[
// Menus.
@@ -5133,3 +5143,42 @@ mod tests {
}
}
}
+
+/// `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 = picker_languages();
+ assert_eq!(order.first(), Some(&("", "English")));
+ assert_eq!(order.len(), LANGUAGES.len());
+ let keys: Vec<String> = order[1..].iter().map(|(_, name)| name_sort_key(name)).collect();
+ assert!(keys.windows(2).all(|w| w[0] <= w[1]), "{keys:?}");
+ let pos = |code: &str| order.iter().position(|(c, _)| *c == code).unwrap_or(usize::MAX);
+ assert!(pos("cs") < pos("de"), "Čeština sorts under C");
+ assert!(pos("pl") < pos("ru"), "Latin names come before Cyrillic");
+ assert!(pos("ru") < pos("ja"), "Cyrillic names come before CJK");
+ }
+}