Fixing locale detection in locales stage.

This commit is contained in:
2026-05-03 12:32:07 +02:00
parent 0de2f1ce10
commit a1263e7056
+39 -38
View File
@@ -24,7 +24,7 @@ use crate::{
};
use iced::{Alignment, widget};
use rust_i18n::t;
use std::collections::HashMap;
use std::{collections::HashMap, default};
const LOCALES_GEN_FILE_NAME: &str = "/etc/locale.gen";
const LOCALES_GEN_START_LINE: usize = 17;
@@ -45,6 +45,16 @@ impl std::fmt::Display for LocaleData {
}
}
impl LocaleData {
pub fn default() -> Self {
let def = default_locale();
Self {
code: def.0,
description: def.1,
}
}
}
fn get_locales_codes_list_blocking() -> std::io::Result<Vec<(String, String)>> {
use std::fs::File;
use std::io::{BufReader, Read};
@@ -143,21 +153,9 @@ impl LocaleStage {
let loc_codes = get_locales_codes_list_blocking().unwrap();
// if we get lang code from wellcome stage, try to fing match with system locales
let lang_match = if let Some(ConfigValue::String(lang)) = maybe_lang {
let lang = lang.replace("-", "_").to_lowercase();
loc_codes
.iter()
.find(|(code, _)| code.to_lowercase().starts_with(&lang))
.cloned()
.unwrap_or_else(default_locale)
} else {
default_locale()
};
let raw_loc_descr = get_locales_description_blocking().unwrap();
println!("{:?}", raw_loc_descr);
// println!("{:?}", raw_loc_descr);
let locales: Vec<LocaleData> = loc_codes
.iter()
@@ -176,33 +174,36 @@ impl LocaleStage {
})
.collect();
println!("{:?}", locales);
// if we get lang code from wellcome stage, try to fing match with system locales
let lang_match = if let Some(ConfigValue::String(lang)) = maybe_lang {
let lang = lang.replace("-", "_").to_lowercase();
locales
.iter()
.find(|l| l.code.to_lowercase().starts_with(&lang))
.cloned()
.unwrap_or_else(LocaleData::default)
} else {
LocaleData::default()
};
let locale = locales.iter().find(|l| l.code == lang_match.0).cloned();
println!("lang_match {:?}", lang_match);
println!("loc_codes {:?}", loc_codes);
let lang_locale_text = locale
.as_ref()
.and_then(|l| {
Some(format!(
"{}: {}",
t!("locale.select_language_locale"),
l.code
))
})
.unwrap_or_else(|| String::new());
let all_locale_text = locale
.as_ref()
.and_then(|l| {
Some(format!(
"{}: {}",
t!("locale.select_formats_locale"),
l.code
))
})
.unwrap_or_else(|| String::new());
// let locale = locales.iter().find(|l| l.code == lang_match.0).cloned();
let lang_locale_text = format!(
"{}: {}",
t!("locale.select_language_locale"),
lang_match.code
);
let all_locale_text = format!(
"{}: {}",
t!("locale.select_formats_locale"),
lang_match.code
);
Self {
lang_locale: locale.clone(),
all_locale: locale,
lang_locale: Some(lang_match.clone()),
all_locale: Some(lang_match),
locales: locales,
lang_locale_text: lang_locale_text,
all_locale_text: all_locale_text,