rustdesk/src/lang.rs

233 lines
5.9 KiB
Rust
Raw Normal View History

use hbb_common::regex::Regex;
2021-12-25 16:45:22 +08:00
use std::ops::Deref;
mod ar;
mod bg;
2023-03-03 11:36:12 +08:00
mod ca;
2021-12-25 16:45:22 +08:00
mod cn;
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;
mod eo;
mod es;
mod et;
2023-03-03 11:36:12 +08:00
mod fa;
2022-08-05 20:33:07 +08:00
mod fr;
2024-03-21 11:18:48 +08:00
mod he;
mod hr;
mod hu;
mod id;
2021-12-29 00:48:58 +08:00
mod it;
mod ja;
mod ko;
2023-03-03 11:36:12 +08:00
mod kz;
mod lt;
2023-09-15 19:58:10 +08:00
mod lv;
mod nb;
mod nl;
mod pl;
mod ptbr;
2023-01-24 23:59:31 +08:00
mod ro;
mod ru;
mod sk;
2023-03-03 11:36:12 +08:00
mod sl;
mod sq;
2022-12-15 20:16:20 +08:00
mod sr;
2023-03-03 11:36:12 +08:00
mod sv;
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"),
("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"),
("et", "Eesti keel"),
2023-03-03 11:36:12 +08:00
("hu", "Magyar"),
("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à"),
("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", "العربية"),
("he", "עברית"),
("hr", "Hrvatski"),
2023-03-03 11:36:12 +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
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("-")
.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(),
"nb" => nb::T.deref(),
"nl" => nl::T.deref(),
2022-06-23 07:26:03 +08:00
"es" => es::T.deref(),
"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(),
"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(),
"ja" => ja::T.deref(),
"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(),
"ca" => ca::T.deref(),
"el" => el::T.deref(),
"sv" => sv::T.deref(),
"sq" => sq::T.deref(),
2022-12-15 20:16:20 +08:00
"sr" => sr::T.deref(),
"th" => th::T.deref(),
"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(),
"he" => he::T.deref(),
2024-04-10 00:23:45 +08:00
"hr" => hr::T.deref(),
2021-12-25 16:45:22 +08:00
_ => en::T.deref(),
};
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() {
2024-03-21 11:18:48 +08:00
if s.contains("RustDesk")
&& !name.starts_with("upgrade_rustdesk_server_pro")
&& name != "powered_by_me"
{
2024-02-25 15:06:55 +08:00
s = s.replace("RustDesk", &crate::get_app_name());
}
}
s
};
2021-12-25 16:45:22 +08:00
if let Some(v) = m.get(&name as &str) {
2024-03-21 11:18:48 +08:00
if !v.is_empty() {
return replace(v);
2022-01-17 12:05:06 +08:00
}
2021-12-25 16:45:22 +08:00
}
2024-03-21 11:18:48 +08:00
if lang != "en" {
if let Some(v) = en::T.get(&name as &str) {
if !v.is_empty() {
return replace(v);
}
}
}
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
}