Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Preferences: list the languages alphabetically by their own name

Nikolai Nyegaard13809228bd34
diff --git a/crates/ui-egui/src/i18n/mod.rs b/crates/ui-egui/src/i18n/mod.rs
index c7a0d574b..76d394297 100644
--- a/crates/ui-egui/src/i18n/mod.rs
+++ b/crates/ui-egui/src/i18n/mod.rs
@@ -280,6 +280,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 and Türkçe under T rather than after Z. Names in other scripts follow
+ /// the Latin ones in code point order: Greek, Cyrillic, then the CJK languages.
+ pub fn for_picker() -> Vec<Lang> {
+ let mut rest: Vec<Lang> = Lang::all().filter(|l| l.code() != "en").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
}
@@ -299,6 +309,27 @@ impl Lang {
}
}
+/// `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()
+}
+
/// Candidate language codes for a locale tag, most specific first: `zh_TW.UTF-8` →
/// `zh-tw`, `zh-hant`, `zh`.
fn candidates(tag: &str) -> Vec<String> {
@@ -456,6 +487,22 @@ mod tests {
assert_eq!(Lang::from_pref("VI"), VI());
}
+ #[test]
+ fn picker_lists_english_first_then_names_alphabetically() {
+ let order = Lang::for_picker();
+ assert_eq!(order.first().map(|l| l.code()), Some("en"));
+ 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:?}");
+ let pos = |code: &str| order.iter().position(|l| l.code() == code).unwrap_or_else(|| panic!("{code} registered"));
+ assert!(pos("cs") < pos("de"), "Čeština sorts under C, not after Z");
+ assert!(pos("it") < pos("hu"), "Magyar sorts under M, not under its code");
+ assert!(pos("tr") < pos("el"), "Latin names come before Greek");
+ assert!(pos("uk") < pos("ja"), "Cyrillic names come before CJK");
+ assert_eq!(name_sort_key("Tiếng Việt"), "tieng viet");
+ assert_eq!(name_sort_key("Türkçe"), "turkce");
+ }
+
#[test]
fn tags_map_to_languages() {
assert_eq!(lang_from_tag("ja_JP.UTF-8"), Some(JA()));
diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs
index e76c20eec..59229a8c6 100644
--- a/crates/ui-egui/src/native_menu.rs
+++ b/crates/ui-egui/src/native_menu.rs
@@ -546,7 +546,7 @@ fn language_node(pref: &str, lang: Lang) -> Node {
role: Some(ItemRole::Language),
};
let mut children = vec![Node::Item(item("auto", tr(lang, "Auto"))), Node::Separator];
- children.extend(Lang::all().map(|l| Node::Item(item(l.code(), l.name()))));
+ children.extend(Lang::for_picker().into_iter().map(|l| Node::Item(item(l.code(), l.name()))));
Node::Submenu { label: tr(lang, "Language").into(), children, role: Some(ItemRole::Language) }
}
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs
index d808ae32f..a4962ceb2 100644
--- a/crates/ui-egui/src/prefs_ui.rs
+++ b/crates/ui-egui/src/prefs_ui.rs
@@ -1689,7 +1689,7 @@ fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map<String, Value>, ke
Value::String(s) if path == "interface.language" => {
ui.label(RichText::new(tl!(&label)).color(t.text_dim));
let mut pairs: Vec<(String, &str)> = vec![("auto".into(), tl!("Auto"))];
- pairs.extend(crate::i18n::Lang::all().map(|l| (l.code().to_string(), l.name())));
+ pairs.extend(crate::i18n::Lang::for_picker().into_iter().map(|l| (l.code().to_string(), l.name())));
let mut cur = s.clone();
crate::widgets::dropdown(ui, &format!("pref-{path}"), &mut cur, &pairs, 220.0);
obj.insert(k, json!(cur));