2023-09-11 19:51:58 +08:00
|
|
|
use hbb_common::regex::Regex;
|
2021-12-25 16:45:22 +08:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2023-09-11 19:51:58 +08:00
|
|
|
mod ar;
|
2024-02-15 16:52:32 +08:00
|
|
|
mod bg;
|
2023-03-03 11:36:12 +08:00
|
|
|
mod ca;
|
2021-12-25 16:45:22 +08:00
|
|
|
mod cn;
|
2022-06-05 18:02:54 +08:00
|
|
|
mod cs;
|
|
|
|
mod da;
|
|
|
|
mod de;
|
2023-03-31 14:18:55 +08:00
|
|
|
mod el;
|
2021-12-25 16:45:22 +08:00
|
|
|
mod en;
|
2022-06-05 18:02:54 +08:00
|
|
|
mod eo;
|
2022-06-30 01:19:38 +08:00
|
|
|
mod es;
|
2023-12-19 10:47:34 +08:00
|
|
|
mod et;
|
2023-03-03 11:36:12 +08:00
|
|
|
mod fa;
|
2022-08-05 20:33:07 +08:00
|
|
|
mod fr;
|
2022-08-05 23:32:51 +08:00
|
|
|
mod hu;
|
2022-06-05 18:02:54 +08:00
|
|
|
mod id;
|
2021-12-29 00:48:58 +08:00
|
|
|
mod it;
|
2022-08-05 23:32:51 +08:00
|
|
|
mod ja;
|
2022-08-16 09:55:24 +08:00
|
|
|
mod ko;
|
2023-03-03 11:36:12 +08:00
|
|
|
mod kz;
|
2023-09-11 19:51:58 +08:00
|
|
|
mod lt;
|
2023-09-15 19:58:10 +08:00
|
|
|
mod lv;
|
2023-11-20 03:32:11 +08:00
|
|
|
mod nb;
|
2023-02-14 17:05:47 +08:00
|
|
|
mod nl;
|
2022-08-05 23:32:51 +08:00
|
|
|
mod pl;
|
2022-04-06 06:04:41 +08:00
|
|
|
mod ptbr;
|
2023-01-24 23:59:31 +08:00
|
|
|
mod ro;
|
2022-06-05 18:02:54 +08:00
|
|
|
mod ru;
|
2022-06-30 01:19:38 +08:00
|
|
|
mod sk;
|
2023-03-03 11:36:12 +08:00
|
|
|
mod sl;
|
2022-12-01 00:37:52 +08:00
|
|
|
mod sq;
|
2022-12-15 20:16:20 +08:00
|
|
|
mod sr;
|
2023-03-03 11:36:12 +08:00
|
|
|
mod sv;
|
2022-12-29 03:05:10 +08:00
|
|
|
mod th;
|
2023-03-03 11:36:12 +08:00
|
|
|
mod tr;
|
|
|
|
mod tw;
|
|
|
|
mod ua;
|
|
|
|
mod vn;
|
2021-12-25 16:45:22 +08:00
|
|
|
|
2023-03-03 11:36:12 +08:00
|
|
|
pub const LANGS: &[(&str, &str)] = &[
|
|
|
|
("en", "English"),
|
|
|
|
("it", "Italiano"),
|
|
|
|
("fr", "Français"),
|
|
|
|
("de", "Deutsch"),
|
|
|
|
("nl", "Nederlands"),
|
2023-11-20 03:32:11 +08:00
|
|
|
("nb", "Norsk bokmål"),
|
2023-03-03 11:49:02 +08:00
|
|
|
("zh-cn", "简体中文"),
|
|
|
|
("zh-tw", "繁體中文"),
|
2023-03-03 11:36:12 +08:00
|
|
|
("pt", "Português"),
|
|
|
|
("es", "Español"),
|
2023-12-19 10:47:34 +08:00
|
|
|
("et", "Eesti keel"),
|
2023-03-03 11:36:12 +08:00
|
|
|
("hu", "Magyar"),
|
2024-02-15 16:52:32 +08:00
|
|
|
("bg", "Български"),
|
2023-03-03 11:36:12 +08:00
|
|
|
("ru", "Русский"),
|
|
|
|
("sk", "Slovenčina"),
|
|
|
|
("id", "Indonesia"),
|
|
|
|
("cs", "Čeština"),
|
|
|
|
("da", "Dansk"),
|
|
|
|
("eo", "Esperanto"),
|
|
|
|
("tr", "Türkçe"),
|
|
|
|
("vn", "Tiếng Việt"),
|
|
|
|
("pl", "Polski"),
|
|
|
|
("ja", "日本語"),
|
|
|
|
("ko", "한국어"),
|
|
|
|
("kz", "Қазақ"),
|
|
|
|
("ua", "Українська"),
|
|
|
|
("fa", "فارسی"),
|
|
|
|
("ca", "Català"),
|
2023-03-08 02:13:38 +08:00
|
|
|
("el", "Ελληνικά"),
|
2023-03-03 11:36:12 +08:00
|
|
|
("sv", "Svenska"),
|
|
|
|
("sq", "Shqip"),
|
|
|
|
("sr", "Srpski"),
|
|
|
|
("th", "ภาษาไทย"),
|
|
|
|
("sl", "Slovenščina"),
|
|
|
|
("ro", "Română"),
|
2023-04-04 01:21:50 +08:00
|
|
|
("lt", "Lietuvių"),
|
2023-09-15 19:58:10 +08:00
|
|
|
("lv", "Latviešu"),
|
2023-08-27 16:14:29 +08:00
|
|
|
("ar", "العربية"),
|
2023-03-03 11:36:12 +08:00
|
|
|
];
|
2022-06-30 01:19:38 +08:00
|
|
|
|
2021-12-25 16:45:22 +08:00
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
pub fn translate(name: String) -> String {
|
2024-03-06 17:38:33 +08:00
|
|
|
let locale = sys_locale::get_locale().unwrap_or_default();
|
2021-12-25 16:45:22 +08:00
|
|
|
translate_locale(name, &locale)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn translate_locale(name: String, locale: &str) -> String {
|
2024-03-06 17:38:33 +08:00
|
|
|
let locale = locale.to_lowercase();
|
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
|
2022-06-05 18:02:54 +08:00
|
|
|
if locale.starts_with("zh") {
|
2024-03-06 17:38:33 +08:00
|
|
|
lang = (if locale.contains("tw") {
|
2023-03-03 11:49:02 +08:00
|
|
|
"zh-tw"
|
|
|
|
} else {
|
|
|
|
"zh-cn"
|
|
|
|
})
|
|
|
|
.to_owned();
|
2022-05-12 17:35:25 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 16:45:22 +08:00
|
|
|
if lang.is_empty() {
|
|
|
|
lang = locale
|
|
|
|
.split("-")
|
2022-06-05 18:02:54 +08:00
|
|
|
.next()
|
2022-06-10 18:43:53 +08:00
|
|
|
.map(|x| x.split("_").next().unwrap_or_default())
|
2021-12-25 16:45:22 +08:00
|
|
|
.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(),
|
2023-03-03 11:49:02 +08:00
|
|
|
"zh-cn" => cn::T.deref(),
|
2021-12-29 00:48:58 +08:00
|
|
|
"it" => it::T.deref(),
|
2023-03-03 11:49:02 +08:00
|
|
|
"zh-tw" => tw::T.deref(),
|
2022-02-17 07:08:18 +08:00
|
|
|
"de" => de::T.deref(),
|
2023-11-20 03:32:11 +08:00
|
|
|
"nb" => nb::T.deref(),
|
2023-02-14 17:05:47 +08:00
|
|
|
"nl" => nl::T.deref(),
|
2022-06-23 07:26:03 +08:00
|
|
|
"es" => es::T.deref(),
|
2023-12-19 10:47:34 +08:00
|
|
|
"et" => et::T.deref(),
|
2022-07-13 01:08:05 +08:00
|
|
|
"hu" => hu::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(),
|
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(),
|
2022-06-05 18:02:54 +08:00
|
|
|
"da" => da::T.deref(),
|
2022-06-10 17:59:21 +08:00
|
|
|
"sk" => sk::T.deref(),
|
2022-07-28 16:54:17 +08:00
|
|
|
"vn" => vn::T.deref(),
|
2022-07-29 03:20:42 +08:00
|
|
|
"pl" => pl::T.deref(),
|
2022-08-05 23:32:51 +08:00
|
|
|
"ja" => ja::T.deref(),
|
2022-08-16 13:51:10 +08:00
|
|
|
"ko" => ko::T.deref(),
|
2022-09-13 17:00:59 +08:00
|
|
|
"kz" => kz::T.deref(),
|
2022-10-06 19:27:35 +08:00
|
|
|
"ua" => ua::T.deref(),
|
2022-11-08 15:52:41 +08:00
|
|
|
"fa" => fa::T.deref(),
|
2022-11-16 18:28:37 +08:00
|
|
|
"ca" => ca::T.deref(),
|
2023-03-08 02:13:38 +08:00
|
|
|
"el" => el::T.deref(),
|
2022-11-29 22:00:27 +08:00
|
|
|
"sv" => sv::T.deref(),
|
2022-12-01 00:37:52 +08:00
|
|
|
"sq" => sq::T.deref(),
|
2022-12-15 20:16:20 +08:00
|
|
|
"sr" => sr::T.deref(),
|
2022-12-29 03:05:10 +08:00
|
|
|
"th" => th::T.deref(),
|
2023-01-05 20:01:11 +08:00
|
|
|
"sl" => sl::T.deref(),
|
2023-01-24 23:59:31 +08:00
|
|
|
"ro" => ro::T.deref(),
|
2023-04-04 01:21:50 +08:00
|
|
|
"lt" => lt::T.deref(),
|
2023-09-15 19:58:10 +08:00
|
|
|
"lv" => lv::T.deref(),
|
2023-08-27 13:03:22 +08:00
|
|
|
"ar" => ar::T.deref(),
|
2024-02-15 16:54:52 +08:00
|
|
|
"bg" => bg::T.deref(),
|
2021-12-25 16:45:22 +08:00
|
|
|
_ => en::T.deref(),
|
|
|
|
};
|
2023-09-11 19:51:58 +08:00
|
|
|
let (name, placeholder_value) = extract_placeholder(&name);
|
|
|
|
let replace = |s: &&str| {
|
|
|
|
let mut s = s.to_string();
|
|
|
|
if let Some(value) = placeholder_value.as_ref() {
|
|
|
|
s = s.replace("{}", &value);
|
|
|
|
}
|
2024-02-25 15:06:55 +08:00
|
|
|
if !crate::is_rustdesk() {
|
|
|
|
if s.contains("RustDesk") && !name.starts_with("upgrade_rustdesk_server_pro") {
|
|
|
|
s = s.replace("RustDesk", &crate::get_app_name());
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 19:51:58 +08:00
|
|
|
s
|
|
|
|
};
|
2021-12-25 16:45:22 +08:00
|
|
|
if let Some(v) = m.get(&name as &str) {
|
2022-08-05 23:32:51 +08:00
|
|
|
if v.is_empty() {
|
|
|
|
if lang != "en" {
|
|
|
|
if let Some(v) = en::T.get(&name as &str) {
|
2023-09-11 19:51:58 +08:00
|
|
|
return replace(v);
|
2022-08-05 23:32:51 +08:00
|
|
|
}
|
2022-01-17 12:05:06 +08:00
|
|
|
}
|
2022-08-05 23:32:51 +08:00
|
|
|
} else {
|
2023-09-11 19:51:58 +08:00
|
|
|
return replace(v);
|
2022-01-17 12:05:06 +08:00
|
|
|
}
|
2021-12-25 16:45:22 +08:00
|
|
|
}
|
2023-09-11 19:51:58 +08:00
|
|
|
replace(&name.as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matching pattern is {}
|
|
|
|
// Write {value} in the UI and {} in the translation file
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// Write in the UI: translate("There are {24} hours in a day")
|
|
|
|
// Write in the translation file: ("There are {} hours in a day", "{} hours make up a day")
|
|
|
|
fn extract_placeholder(input: &str) -> (String, Option<String>) {
|
|
|
|
if let Ok(re) = Regex::new(r#"\{(.*?)\}"#) {
|
|
|
|
if let Some(captures) = re.captures(input) {
|
|
|
|
if let Some(inner_match) = captures.get(1) {
|
|
|
|
let name = re.replace(input, "{}").to_string();
|
|
|
|
let value = inner_match.as_str().to_string();
|
|
|
|
return (name, Some(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(input.to_string(), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
mod test {
|
|
|
|
#[test]
|
|
|
|
fn test_extract_placeholders() {
|
|
|
|
use super::extract_placeholder as f;
|
|
|
|
|
|
|
|
assert_eq!(f(""), ("".to_string(), None));
|
|
|
|
assert_eq!(
|
|
|
|
f("{3} sessions"),
|
|
|
|
("{} sessions".to_string(), Some("3".to_string()))
|
|
|
|
);
|
|
|
|
assert_eq!(f(" } { "), (" } { ".to_string(), None));
|
|
|
|
// Allow empty value
|
|
|
|
assert_eq!(
|
|
|
|
f("{} sessions"),
|
|
|
|
("{} sessions".to_string(), Some("".to_string()))
|
|
|
|
);
|
|
|
|
// Match only the first one
|
|
|
|
assert_eq!(
|
|
|
|
f("{2} times {4} makes {8}"),
|
|
|
|
("{} times {4} makes {8}".to_string(), Some("2".to_string()))
|
|
|
|
);
|
|
|
|
}
|
2021-12-25 16:45:22 +08:00
|
|
|
}
|