use anyhow::Result;
use crate::client::DeepSeekClient;
const ENGLISH_LATIN_RATIO_THRESHOLD: f64 = 0.6;
const MIN_ALPHA_CHARS_FOR_DETECTION: usize = 10;
const CJK_CHAR_WEIGHT: usize = 3;
#[must_use]
pub fn needs_translation(text: &str) -> bool {
let mut latin_count = 0usize;
let mut cjk_count = 0usize;
for ch in text.chars() {
if ch.is_ascii_alphabetic() {
latin_count += 1;
} else if is_cjk(ch) {
cjk_count += 1;
}
}
let total_alpha = latin_count + (cjk_count * CJK_CHAR_WEIGHT);
if total_alpha < MIN_ALPHA_CHARS_FOR_DETECTION {
return false;
}
if (cjk_count * CJK_CHAR_WEIGHT) > latin_count {
return false;
}
let ratio = latin_count as f64 / total_alpha as f64;
ratio >= ENGLISH_LATIN_RATIO_THRESHOLD
}
fn is_cjk(ch: char) -> bool {
matches!(
ch,
'\u{4E00}'..='\u{9FFF}' | '\u{3400}'..='\u{4DBF}' | '\u{2E80}'..='\u{2EFF}' | '\u{3000}'..='\u{303F}' | '\u{FF00}'..='\u{FFEF}' | '\u{3040}'..='\u{309F}' | '\u{30A0}'..='\u{30FF}' )
}
pub async fn translate_text(
text: &str,
client: &DeepSeekClient,
model: &str,
target_language: &str,
) -> Result<String> {
client.translate(text, model, target_language).await
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)]
pub enum TranslationStatus {
NotNeeded,
Pending,
Done,
Failed,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn short_text_avoids_false_positive() {
assert!(!needs_translation("hi"));
assert!(!needs_translation("ok"));
}
#[test]
fn english_text_detected() {
assert!(needs_translation(
"This is a message from the assistant explaining how the code works."
));
}
#[test]
fn chinese_text_not_detected() {
assert!(!needs_translation(
"这是助手的一条中文回复,解释了代码的工作原理。"
));
}
#[test]
fn mixed_mostly_english_detected() {
assert!(needs_translation(
"The function handle_request takes a Request param and returns a Response."
));
}
#[test]
fn mixed_mostly_chinese_not_detected() {
assert!(!needs_translation(
"这个 handle_request 函数接收一个 Request 参数并返回 Response。"
));
}
#[test]
fn code_with_short_labels_not_falsely_detected() {
assert!(!needs_translation("let x = 1; let y = 2;"));
}
#[test]
fn long_english_code_is_detected() {
assert!(needs_translation(
"function calculateTotalRevenueForQuarterlyReport() { return; }"
));
}
#[test]
fn js_comments_in_english_detected() {
assert!(needs_translation(
"// This is a JavaScript function that handles user authentication\nfunction login() {}"
));
}
}