rustdesk/src/lang.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

2021-12-25 16:45:22 +08:00
use std::ops::Deref;
mod cn;
mod en;
mod fr;
2021-12-29 00:48:58 +08:00
mod it;
2022-02-12 11:50:39 +08:00
mod tw;
mod de;
2022-02-17 07:05:24 +08:00
mod ru;
2022-02-20 03:01:28 +08:00
mod eo;
mod ptbr;
2022-05-09 12:27:50 +08:00
mod id;
2022-05-13 21:38:49 +08:00
mod tr;
2022-05-26 21:07:39 +08:00
mod cs;
2021-12-25 16:45:22 +08:00
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn translate(name: String) -> String {
let locale = sys_locale::get_locale().unwrap_or_default().to_lowercase();
translate_locale(name, &locale)
}
pub fn translate_locale(name: String, locale: &str) -> String {
2022-05-12 17:35:25 +08:00
let mut lang = hbb_common::config::LocalConfig::get_option("lang").to_lowercase();
if lang.is_empty() {
// zh_CN on Linux, zh-Hans-CN on mac, zh_CN_#Hans on Android
if locale.starts_with("zh") && (locale.ends_with("CN") || locale.ends_with("SG") || locale.ends_with("Hans")) {
lang = "cn".to_owned();
}
}
2021-12-25 16:45:22 +08:00
if lang.is_empty() {
lang = locale
.split("-")
.last()
.map(|x| x.split("_").last().unwrap_or_default())
.unwrap_or_default()
.to_owned();
}
2022-01-17 12:05:06 +08:00
let lang = lang.to_lowercase();
let m = match lang.as_str() {
2021-12-25 16:45:22 +08:00
"fr" => fr::T.deref(),
"cn" => cn::T.deref(),
2021-12-29 00:48:58 +08:00
"it" => it::T.deref(),
2022-02-12 11:50:39 +08:00
"tw" => tw::T.deref(),
2022-02-17 07:08:18 +08:00
"de" => de::T.deref(),
2022-02-17 07:05:24 +08:00
"ru" => ru::T.deref(),
2022-02-20 03:01:28 +08:00
"eo" => eo::T.deref(),
2022-05-12 17:35:25 +08:00
"id" => id::T.deref(),
"ptbr" => ptbr::T.deref(),
2022-04-06 10:26:09 +08:00
"br" => ptbr::T.deref(),
"pt" => ptbr::T.deref(),
2022-05-13 21:38:49 +08:00
"tr" => tr::T.deref(),
2022-05-26 21:07:39 +08:00
"cs" => cs::T.deref(),
2021-12-25 16:45:22 +08:00
_ => en::T.deref(),
};
if let Some(v) = m.get(&name as &str) {
v.to_string()
} else {
2022-01-17 12:05:06 +08:00
if lang != "en" {
if let Some(v) = en::T.get(&name as &str) {
return v.to_string();
}
}
2021-12-25 16:45:22 +08:00
name
}
}