From b3e1add178ffe7a13d91397d89cd1e642d32439e Mon Sep 17 00:00:00 2001 From: Kylmakalle Date: Fri, 11 Jul 2025 19:50:22 +0300 Subject: [PATCH] Translate one-by-one --- .../TelegramEngine/Messages/Translate.swift | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift index 43f0493c6e..cacd45b5ec 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift @@ -483,28 +483,32 @@ public func requestTranslateUrl(url: URL) -> Signal public func gtranslate(_ text: String, _ toLang: String) -> Signal { - let parts = gtranslateSplitTextBySentences(text) - - let translationSignals: [Signal] = parts.map { part in - return gtranslateSentence(part, toLang) - } - - return combineLatest(translationSignals) - |> map { results -> String in - var result: String = "" - - for translatedPart in results { - if !result.isEmpty { - result += " " + // 1) Preserve *all* line breaks, including empty ones + let lines = text.components(separatedBy: "\n") + + // 2) Map each line to either a passthrough signal or a translate signal + let translationSignals: [Signal] = lines.map { rawLine in + // Grab the "core" text and its leading whitespace + let leadingWhitespace = rawLine.prefix { $0.isWhitespace } + let core = rawLine.trimmingCharacters(in: .whitespacesAndNewlines) + + // If there’s nothing to translate, just emit the line back + if core.isEmpty { + return .single(rawLine) + } + + // Otherwise translate the core, then re-attach the indentation + return gtranslateSentence(core, toLang) + |> map { translatedCore in + return String(leadingWhitespace) + translatedCore } - result += translatedPart - } + } - if result.isEmpty { - return text // Fallback to original text if translation failed - } - - return result + // 3) Combine them and re-join with newlines + return combineLatest(translationSignals) + |> map { results in + let joined = results.joined(separator: "\n") + return joined.isEmpty ? text : joined } }