forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang.rs
232 lines (225 loc) · 5.86 KB
/
lang.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use hbb_common::regex::Regex;
use std::ops::Deref;
mod ar;
mod bg;
mod ca;
mod cn;
mod cs;
mod da;
mod de;
mod el;
mod en;
mod eo;
mod es;
mod et;
mod fa;
mod fr;
mod he;
mod hr;
mod hu;
mod id;
mod it;
mod ja;
mod ko;
mod kz;
mod lt;
mod lv;
mod nb;
mod nl;
mod pl;
mod ptbr;
mod ro;
mod ru;
mod sk;
mod sl;
mod sq;
mod sr;
mod sv;
mod th;
mod tr;
mod tw;
mod ua;
mod vn;
pub const LANGS: &[(&str, &str)] = &[
("en", "English"),
("it", "Italiano"),
("fr", "Français"),
("de", "Deutsch"),
("nl", "Nederlands"),
("nb", "Norsk bokmål"),
("zh-cn", "简体中文"),
("zh-tw", "繁體中文"),
("pt", "Português"),
("es", "Español"),
("et", "Eesti keel"),
("hu", "Magyar"),
("bg", "Български"),
("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", "Ελληνικά"),
("sv", "Svenska"),
("sq", "Shqip"),
("sr", "Srpski"),
("th", "ภาษาไทย"),
("sl", "Slovenščina"),
("ro", "Română"),
("lt", "Lietuvių"),
("lv", "Latviešu"),
("ar", "العربية"),
("he", "עברית"),
("hr", "Hrvatski"),
];
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn translate(name: String) -> String {
let locale = sys_locale::get_locale().unwrap_or_default();
translate_locale(name, &locale)
}
pub fn translate_locale(name: String, locale: &str) -> String {
let locale = locale.to_lowercase();
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") {
lang = (if locale.contains("tw") {
"zh-tw"
} else {
"zh-cn"
})
.to_owned();
}
}
if lang.is_empty() {
lang = locale
.split("-")
.next()
.map(|x| x.split("_").next().unwrap_or_default())
.unwrap_or_default()
.to_owned();
}
let lang = lang.to_lowercase();
let m = match lang.as_str() {
"fr" => fr::T.deref(),
"zh-cn" => cn::T.deref(),
"it" => it::T.deref(),
"zh-tw" => tw::T.deref(),
"de" => de::T.deref(),
"nb" => nb::T.deref(),
"nl" => nl::T.deref(),
"es" => es::T.deref(),
"et" => et::T.deref(),
"hu" => hu::T.deref(),
"ru" => ru::T.deref(),
"eo" => eo::T.deref(),
"id" => id::T.deref(),
"br" => ptbr::T.deref(),
"pt" => ptbr::T.deref(),
"tr" => tr::T.deref(),
"cs" => cs::T.deref(),
"da" => da::T.deref(),
"sk" => sk::T.deref(),
"vn" => vn::T.deref(),
"pl" => pl::T.deref(),
"ja" => ja::T.deref(),
"ko" => ko::T.deref(),
"kz" => kz::T.deref(),
"ua" => ua::T.deref(),
"fa" => fa::T.deref(),
"ca" => ca::T.deref(),
"el" => el::T.deref(),
"sv" => sv::T.deref(),
"sq" => sq::T.deref(),
"sr" => sr::T.deref(),
"th" => th::T.deref(),
"sl" => sl::T.deref(),
"ro" => ro::T.deref(),
"lt" => lt::T.deref(),
"lv" => lv::T.deref(),
"ar" => ar::T.deref(),
"bg" => bg::T.deref(),
"he" => he::T.deref(),
"hr" => hr::T.deref(),
_ => 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);
}
if !crate::is_rustdesk() {
if s.contains("RustDesk")
&& !name.starts_with("upgrade_rustdesk_server_pro")
&& name != "powered_by_me"
{
s = s.replace("RustDesk", &crate::get_app_name());
}
}
s
};
if let Some(v) = m.get(&name as &str) {
if !v.is_empty() {
return replace(v);
}
}
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()))
);
}
}