Only use HTML mode when necessary

This commit is contained in:
2025-08-29 01:16:42 +00:00
parent b770ba83d6
commit c613af2b38

View File

@@ -27,7 +27,7 @@ class GenericmessageCommand extends SystemCommand
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
if(!in_array($chat_id, $allowed_chat_ids)) { if (!in_array($chat_id, $allowed_chat_ids)) {
return $this->replyToChat("Chat ID `$chat_id` not allowed"); return $this->replyToChat("Chat ID `$chat_id` not allowed");
} }
@@ -38,37 +38,45 @@ class GenericmessageCommand extends SystemCommand
$text_chara_count = count(preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY)); $text_chara_count = count(preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY));
$text_english_count = count(preg_split('~[^a-z 0-9\'\",.]*~i', $text, 0, PREG_SPLIT_NO_EMPTY)); $text_english_count = count(preg_split('~[^a-z 0-9\'\",.]*~i', $text, 0, PREG_SPLIT_NO_EMPTY));
if ($text_english_count >= ($text_chara_count*2 /3)) { if ($text_english_count >= ($text_chara_count * 2 / 3)) {
$sourceLang = 'en'; $sourceLang = 'en';
$targetLang = 'zh-hans'; $targetLang = 'zh-hans';
} else { } else {
$sourceLang = 'zh'; $sourceLang = 'zh';
$targetLang = 'en-US'; $targetLang = 'en-US';
} }
$htmlMode = $message->getEntities() && count($message->getEntities()) > 0;
$deeplOptions = [ $deeplOptions = [
TranslateTextOptions::FORMALITY => 'prefer_less', TranslateTextOptions::FORMALITY => 'prefer_less',
TranslateTextOptions::MODEL_TYPE => 'prefer_quality_optimized', TranslateTextOptions::MODEL_TYPE => 'prefer_quality_optimized',
TranslateTextOptions::TAG_HANDLING => 'html',
TranslateTextOptions::SPLIT_SENTENCES => 'on', TranslateTextOptions::SPLIT_SENTENCES => 'on',
]; ];
if ($htmlMode) {
$deeplOptions[TranslateTextOptions::TAG_HANDLING] = 'html';
}
if (isset($deepl_glossary_id)) { if (isset($deepl_glossary_id)) {
$deeplOptions[TranslateTextOptions::GLOSSARY] = $deepl_glossary_id; $deeplOptions[TranslateTextOptions::GLOSSARY] = $deepl_glossary_id;
} }
$this->translator = new Translator($deepl_api_key); $this->translator = new Translator($deepl_api_key);
try { try {
$decoded_text = (new EntityDecoder('HTML'))->decode(json_decode($message->toJson())); if ($htmlMode) {
$translated = $this->translator->translateText($decoded_text, $sourceLang, $targetLang, $deeplOptions); $text = (new EntityDecoder('HTML'))->decode(json_decode($message->toJson()));
// $text = str_replace("\n", '<br />', $text);
}
$translated = $this->translator->translateText($text, $sourceLang, $targetLang, $deeplOptions);
$translated = $translated->text; $translated = $translated->text;
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->replyToChat(get_class($e) . ': ' . $e->getMessage()); // ?? var_export($e, true) return $this->replyToChat(get_class($e) . ': ' . $e->getMessage()); // ?? var_export($e, true)
} }
$translated = str_replace('&lt;BR /&gt;', "\n", $translated);
// $translated = str_replace('<br />', "\n", $translated);
$message_forwarded_from = $message->getForwardFrom()?->getFirstName() ?? $message->getForwardSenderName(); $forwardedFrom = $message->getForwardFrom();
if ($message_forwarded_from !== null) { if ($forwardedFrom) {
$translated = $message_forwarded_from . ': ' . $translated; $translated = $forwardedFrom['first_name'] . ': ' . $translated;
} }
return $this->replyToChat($translated, ['disable_notification' => true, 'parse_mode' => 'HTML']); return $this->replyToChat($translated, ['disable_notification' => true, 'parse_mode' => $htmlMode ? 'HTML' : null]);
} }
} }