Files
rustdesk/src/lang.rs
T
RustDeskandClaude Opus 5.5 9997b7a3b8 test: remove tests that do not pin user-visible behavior (#16539)
* test: remove tests that do not pin user-visible behavior

Apply the AGENTS.md test rules to the existing Rust and Flutter suites.
A test is kept when it is the only coverage of user- or API-visible
behavior, the regression test of a reported or fixed bug, a security
boundary (one test per rejection class), or a check against an external
reference. Tests that assert private state or the order of internal
steps, pin review-round corner cases, or repeat what a kept test already
covers are removed.

Rust: 679 -> 298 tests. Dart: 118 -> 45 tests.

Test-only helpers left without callers go with them
(applyLastOnlineStateForTesting, terminalKeyboardRowWidth,
debugBufferedInputCount, tokio's test-util dev feature), and cfg gates
and visibility that existed only for the deleted tests are narrowed.
Runtime behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WS1HTjp6UQuG43KEcQPq1R

* test: keep tests that pin security and user-visible behavior

Restore six tests that review showed are the only coverage of their
behavior:

- update metadata rejects an artifact file name with path components;
- ICE candidates from another session are dropped;
- KCP delivers every multi-segment frame queued before a close;
- set_files rejects an empty name in a multi-file transfer;
- a lopsided elongated cursor keeps its centre hotspot (#16242);
- toggling Show hidden during a pending same-path read issues a new read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WS1HTjp6UQuG43KEcQPq1R

* test: keep offline update and key-exchange downgrade rejections

Restore three tests that are the only coverage of their rejection class:

- offline update metadata rejects a signed artifact with an http or
  credential-bearing URL;
- offline verification rejects a renamed local file and a version that
  does not match the release;
- a key-exchange pick lowered in transit leaves both sides unable to
  decrypt, so a v0 fallback after a failed v1 decrypt cannot pass
  unnoticed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WS1HTjp6UQuG43KEcQPq1R

* test: keep the decomposed-grapheme and reconnect-cancel tests

Each catches a plausible wrong change that passes every kept test:
mapping the first scalar under a locked Ctrl turns é into \x05,
and a reconnect that does not fail pending reads leaves them hanging
until the 30 s timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WS1HTjp6UQuG43KEcQPq1R

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:45:22 +08:00

369 lines
10 KiB
Rust

use hbb_common::regex::Regex;
use std::ops::Deref;
mod ar;
mod az;
mod be;
mod bg;
mod ca;
mod cn;
mod cs;
mod da;
mod de;
mod el;
mod en;
mod eo;
mod es;
mod et;
mod eu;
mod fa;
mod gu;
mod fr;
mod he;
mod hi;
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 pt_BR;
mod pt_PT;
mod ro;
mod ru;
mod sc;
mod sk;
mod sl;
mod sq;
mod sr;
mod sv;
mod th;
mod tr;
mod tw;
mod uk;
mod ur;
mod vi;
mod ta;
mod ge;
mod fi;
mod ml;
mod gl;
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-pt", "Português (Portugal)"),
("pt-br", "Português (Brasil)"),
("es", "Español"),
("et", "Eesti keel"),
("eu", "Euskara"),
("hu", "Magyar"),
("bg", "Български"),
("be", "Беларуская"),
("ru", "Русский"),
("sk", "Slovenčina"),
("id", "Indonesia"),
("cs", "Čeština"),
("da", "Dansk"),
("eo", "Esperanto"),
("tr", "Türkçe"),
("vi", "Tiếng Việt"),
("pl", "Polski"),
("ja", "日本語"),
("ko", "한국어"),
("kz", "Қазақ"),
("uk", "Українська"),
("ur", "اردو"),
("fa", "فارسی"),
("ca", "Català"),
("gl", "Galego"),
("el", "Ελληνικά"),
("sv", "Svenska"),
("sq", "Shqip"),
("sr", "Srpski"),
("th", "ภาษาไทย"),
("sl", "Slovenščina"),
("ro", "Română"),
("lt", "Lietuvių"),
("lv", "Latviešu"),
("ar", "العربية"),
("he", "עברית"),
("hr", "Hrvatski"),
("sc", "Sardu"),
("ta", "தமிழ்"),
("ge", "ქართული"),
("fi", "Suomi"),
("ml", "മലയാളം"),
("hi", "हिंदी"),
("gu", "ગુજરાતી"),
("az", "Azərbaycan dili"),
];
pub(crate) fn cjk_ui_unavailable() -> bool {
cfg!(all(
target_os = "linux",
target_arch = "aarch64",
feature = "flutter"
))
}
pub(crate) fn is_cjk_lang(lang_or_locale: &str) -> bool {
let lang = lang_or_locale
.split(|c| c == '-' || c == '_')
.next()
.unwrap_or_default()
.to_lowercase();
matches!(lang.as_str(), "zh" | "ja" | "ko")
}
fn resolve_lang(saved_lang: &str, locale: &str, cjk_fallback: bool) -> String {
let locale = locale.to_lowercase();
let mut lang = saved_lang.to_lowercase();
if cjk_fallback && is_cjk_lang(&lang) {
return "en".to_owned();
}
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() {
// pt_PT on Linux, pt-PT on mac, pt_PT on Android.
// Per CLDR locale inheritance, every Portuguese-speaking locale
// besides bare "pt" and Brazil's own variants (pt-BR/pt_BR) has
// pt-PT as its parent (Angola, Mozambique, Cape Verde, etc.),
// so it should resolve to European Portuguese.
if locale.starts_with("pt") {
lang = (if locale == "pt" || locale.starts_with("pt-br") || locale.starts_with("pt_br") {
"pt-br"
} else {
"pt-pt"
})
.to_owned();
}
}
if lang.is_empty() {
lang = locale
.split("-")
.next()
.map(|x| x.split("_").next().unwrap_or_default())
.unwrap_or_default()
.to_owned();
}
if cjk_fallback && is_cjk_lang(&lang) {
"en".to_owned()
} else {
lang
}
}
#[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 lang = resolve_lang(
&hbb_common::config::LocalConfig::get_option("lang"),
locale,
cjk_ui_unavailable(),
);
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(),
"eu" => eu::T.deref(),
"hu" => hu::T.deref(),
"ru" => ru::T.deref(),
"eo" => eo::T.deref(),
"id" => id::T.deref(),
"br" => pt_BR::T.deref(),
"pt" => pt_BR::T.deref(),
"pt-br" => pt_BR::T.deref(),
"pt-pt" => pt_PT::T.deref(),
"tr" => tr::T.deref(),
"cs" => cs::T.deref(),
"da" => da::T.deref(),
"sk" => sk::T.deref(),
"vi" => vi::T.deref(),
"pl" => pl::T.deref(),
"ja" => ja::T.deref(),
"ko" => ko::T.deref(),
"kz" => kz::T.deref(),
"uk" => uk::T.deref(),
"fa" => fa::T.deref(),
"fi" => fi::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(),
"be" => be::T.deref(),
"he" => he::T.deref(),
"hr" => hr::T.deref(),
"ur" => ur::T.deref(),
"sc" => sc::T.deref(),
"ta" => ta::T.deref(),
"ge" => ge::T.deref(),
"ml" => ml::T.deref(),
"hi" => hi::T.deref(),
"gu" => gu::T.deref(),
"gl" => gl::T.deref(),
"az" => az::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"
{
let app_name = crate::get_app_name();
if !app_name.contains("RustDesk") {
s = s.replace("RustDesk", &app_name);
} else {
// https://github.com/rustdesk/rustdesk-server-pro/issues/845
// If app_name contains "RustDesk" (e.g., "RustDesk-Admin"), we need to avoid
// replacing "RustDesk" within the already-substituted app_name, which would
// cause duplication like "RustDesk-Admin" -> "RustDesk-Admin-Admin".
//
// app_name only contains alphanumeric and hyphen.
const PLACEHOLDER: &str = "#A-P-P-N-A-M-E#";
if !s.contains(PLACEHOLDER) {
s = s.replace(&app_name, PLACEHOLDER);
s = s.replace("RustDesk", &app_name);
s = s.replace(PLACEHOLDER, &app_name);
} else {
// It's very unlikely to reach here.
// Skip replacement to avoid incorrect result.
}
}
}
}
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()))
);
}
#[test]
fn test_resolve_lang_forces_english_for_cjk_locale_when_target_disables_cjk() {
use super::resolve_lang as f;
assert_eq!(f("", "zh_CN", true), "en");
assert_eq!(f("", "ja-JP", true), "en");
assert_eq!(f("", "ko_KR", true), "en");
}
#[test]
fn test_resolve_lang_preserves_cjk_when_target_allows_cjk() {
use super::resolve_lang as f;
assert_eq!(f("zh-cn", "en-US", false), "zh-cn");
assert_eq!(f("", "zh_TW", false), "zh-tw");
assert_eq!(f("", "ja-JP", false), "ja");
}
#[test]
fn test_resolve_lang_detects_pt_pt_and_pt_br_from_locale() {
use super::resolve_lang as f;
assert_eq!(f("", "pt-PT", false), "pt-pt");
assert_eq!(f("", "pt_PT", false), "pt-pt");
assert_eq!(f("", "pt-BR", false), "pt-br");
assert_eq!(f("", "pt_BR", false), "pt-br");
assert_eq!(f("", "pt-AO", false), "pt-pt");
assert_eq!(f("", "pt-MZ", false), "pt-pt");
}
}