fix: add system language sup

This commit is contained in:
blagodaren
2024-12-28 08:12:46 +03:00
parent 48f9dede7b
commit 9c027b10b2
9 changed files with 554 additions and 55 deletions

View File

@@ -0,0 +1,59 @@
use std::{fs, path::Path};
use crate::config::Config;
use sys_locale;
pub fn get_supported_languages() -> Vec<String> {
let project_dir = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let i18n_path = project_dir.join("src/services/i18n.ts");
if let Ok(content) = fs::read_to_string(i18n_path) {
let mut languages = Vec::new();
for line in content.lines() {
if line.contains("resources = {") {
for line in content.lines() {
if let Some(lang) = line.trim().strip_suffix(": { translation:") {
let lang = lang.trim().trim_matches('"');
if !lang.is_empty() {
languages.push(lang.to_string());
}
}
if line.contains("};") {
break;
}
}
break;
}
}
if !languages.is_empty() {
return languages;
}
}
vec!["en".to_string(), "ru".to_string(), "zh".to_string(), "fa".to_string()]
}
pub fn t(text: &str) -> String {
let config = Config::verge();
let verge = config.latest();
let current_lang = verge.language.as_ref().map_or_else(
|| get_system_language(),
|lang| lang.to_string()
);
text.to_string()
}
fn get_system_language() -> String {
let sys_lang = sys_locale::get_locale()
.unwrap_or_else(|| String::from("en"))
.to_lowercase();
let lang_code = sys_lang.split(['_', '-']).next().unwrap_or("en");
let supported_languages = get_supported_languages();
if supported_languages.contains(&lang_code.to_string()) {
lang_code.to_string()
} else {
String::from("en")
}
}

View File

@@ -5,3 +5,4 @@ pub mod init;
pub mod resolve;
pub mod server;
pub mod tmpl;
pub mod i18n;