Improve translator

This commit is contained in:
Kylmakalle 2025-07-09 00:02:41 +03:00
parent 2f4076d85f
commit fe28c9d96d
8 changed files with 45 additions and 12 deletions

View File

@ -193,14 +193,22 @@ private func SGControllerEntries(presentationData: PresentationData, callListSet
entries.append(.header(id: id.count, section: .translation, text: presentationData.strings.Localization_TranslateMessages.uppercased(), badge: nil))
entries.append(.oneFromManySelector(id: id.count, section: .translation, settingName: .translationBackend, text: i18n("Settings.Translation.Backend", lang), value: i18n("Settings.Translation.Backend.\(SGSimpleSettings.shared.translationBackend)", lang), enabled: true))
entries.append(.notice(id: id.count, section: .translation, text: i18n("Settings.Translation.Backend.Notice", lang)))
if SGSimpleSettings.shared.translationBackendEnum != .gtranslate {
entries.append(.notice(id: id.count, section: .translation, text: i18n("Settings.Translation.Backend.Notice", lang, "Settings.Translation.Backend.\(SGSimpleSettings.TranslationBackend.gtranslate.rawValue)".i18n(lang))))
} else {
id.increment(1)
}
entries.append(.toggle(id: id.count, section: .translation, settingName: .quickTranslateButton, value: SGSimpleSettings.shared.quickTranslateButton, text: i18n("Settings.Translation.QuickTranslateButton", lang), enabled: true))
entries.append(.disclosure(id: id.count, section: .translation, link: .languageSettings, text: presentationData.strings.Localization_TranslateEntireChat))
entries.append(.notice(id: id.count, section: .translation, text: i18n("Common.NoTelegramPremiumNeeded", lang, presentationData.strings.Settings_Premium)))
entries.append(.header(id: id.count, section: .voiceMessages, text: "Settings.Transcription.Header".i18n(lang), badge: nil))
entries.append(.oneFromManySelector(id: id.count, section: .voiceMessages, settingName: .transcriptionBackend, text: i18n("Settings.Transcription.Backend", lang), value: i18n("Settings.Transcription.Backend.\(SGSimpleSettings.shared.transcriptionBackend)", lang), enabled: true))
entries.append(.notice(id: id.count, section: .voiceMessages, text: i18n("Settings.Transcription.Backend.Notice", lang)))
if SGSimpleSettings.shared.transcriptionBackendEnum != .apple {
entries.append(.notice(id: id.count, section: .voiceMessages, text: i18n("Settings.Transcription.Backend.Notice", lang, "Settings.Transcription.Backend.\(SGSimpleSettings.TranscriptionBackend.apple.rawValue)".i18n(lang))))
} else {
id.increment(1)
}
entries.append(.header(id: id.count, section: .voiceMessages, text: presentationData.strings.Privacy_VoiceMessages.uppercased(), badge: nil))
entries.append(.toggle(id: id.count, section: .voiceMessages, settingName: .forceBuiltInMic, value: SGSimpleSettings.shared.forceBuiltInMic, text: i18n("Settings.forceBuiltInMic", lang), enabled: true))
entries.append(.notice(id: id.count, section: .voiceMessages, text: i18n("Settings.forceBuiltInMic.Notice", lang)))
@ -593,6 +601,12 @@ public func sgSettingsController(context: AccountContext/*, focusOnItemTag: Int?
}
for value in SGSimpleSettings.TranslationBackend.allCases {
if value == .system {
if #available(iOS 18.0, *) {
} else {
continue // System translation is not available on iOS 17 and below
}
}
items.append(ActionSheetButtonItem(title: i18n("Settings.Translation.Backend.\(value.rawValue)", presentationData.strings.baseLanguageCode), color: .accent, action: { [weak actionSheet] in
actionSheet?.dismissAnimated()
setAction(value.rawValue)

View File

@ -179,6 +179,8 @@ public class SGSimpleSettings {
public enum TranslationBackend: String, CaseIterable {
case `default`
case gtranslate
case system
// Make sure to update TranslationConfiguration
}
public enum PinnedMessageNotificationsSettings: String, CaseIterable {
@ -495,6 +497,16 @@ extension SGSimpleSettings {
}
}
extension SGSimpleSettings {
public var translationBackendEnum: SGSimpleSettings.TranslationBackend {
return TranslationBackend(rawValue: translationBackend) ?? .default
}
public var transcriptionBackendEnum: SGSimpleSettings.TranscriptionBackend {
return TranscriptionBackend(rawValue: transcriptionBackend) ?? .default
}
}
public func getSGDownloadPartSize(_ default: Int64, fileSize: Int64?) -> Int64 {
let currentDownloadSetting = SGSimpleSettings.shared.downloadSpeedBoost
// Increasing chunk size for small files make it worse in terms of overall download performance

View File

@ -40,7 +40,8 @@
"Settings.Translation.Backend.default" = "Telegram";
/* Do not translate */
"Settings.Translation.Backend.gtranslate" = "GTranslate";
"Settings.Translation.Backend.Notice" = "Swiftgram will fallback to other translation services in case Telegram is not available.";
"Settings.Translation.Backend.system" = "System";
"Settings.Translation.Backend.Notice" = "Swiftgram will fallback to %@ if selected translation service is not available.";
"Settings.Transcription.Header" = "VOICE-TO-TEXT";
"Settings.Transcription.Backend" = "Service";
@ -48,7 +49,7 @@
"Settings.Transcription.Backend.default" = "Telegram";
/* Do not translate */
"Settings.Transcription.Backend.apple" = "Apple";
"Settings.Transcription.Backend.Notice" = "Swiftgram will fallback to other transcription services in case Telegram is not available.";
"Settings.Transcription.Backend.Notice" = "Swiftgram will fallback to %@ if selected transcription service is not available.";
"Stories.Warning.Author" = "Author";
"Stories.Warning.ViewStory" = "View Story?";

View File

@ -457,7 +457,7 @@ final class LocalizationListControllerNode: ViewControllerTracingNode {
default:
break
}
chatTranslationAvailable = true; translateButtonAvailable = true // MARK: Swiftgram
let previousState = Atomic<LocalizationListState?>(value: nil)
let previousEntriesHolder = Atomic<([LanguageListEntry], PresentationTheme, PresentationStrings)?>(value: nil)
self.listDisposable = combineLatest(

View File

@ -394,7 +394,7 @@ public func parseTranslateResponse(_ data: String) -> String {
return ""
}
public func getGoogleLang(_ userLang: String) -> String {
public func getGTranslateLang(_ userLang: String) -> String {
var lang = userLang
let rawSuffix = "-raw"
if lang.hasSuffix(rawSuffix) {
@ -416,7 +416,11 @@ public func getGoogleLang(_ userLang: String) -> String {
// Fix for pt-br and other regional langs
// https://cloud.google.com/translate/docs/languages
// https://cloud.go
// ogle.com/tran
// slate/do
// cs/lang
// uages
lang = lang.components(separatedBy: "-")[0].components(separatedBy: "_")[0]
return lang
@ -477,7 +481,7 @@ public func requestTranslateUrl(url: URL) -> Signal<String, TranslateFetchError>
public func gtranslate(_ text: String, _ toLang: String) -> Signal<String, TranslateFetchError> {
return Signal { subscriber in
let urlString = getTranslateUrl(text, getGoogleLang(toLang))
let urlString = getTranslateUrl(text, getGTranslateLang(toLang))
let url = URL(string: urlString)!
let translateSignal = requestTranslateUrl(url: url)
var translateDisposable: Disposable? = nil

View File

@ -1,6 +1,7 @@
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
sgdeps = [
"//Swiftgram/SGSimpleSettings:SGSimpleSettings",
"//submodules/TextFormat:TextFormat"
]

View File

@ -185,7 +185,7 @@ public func canTranslateChats(context: AccountContext) -> Bool {
default:
break
}
return chatTranslationAvailable
return chatTranslationAvailable || true // MARK: Swiftgram
}
public func canTranslateText(context: AccountContext, text: String, showTranslate: Bool, showTranslateIfTopical: Bool = false, ignoredLanguages: [String]?) -> (canTranslate: Bool, language: String?) {
@ -205,7 +205,7 @@ public func canTranslateText(context: AccountContext, text: String, showTranslat
default:
break
}
translateButtonAvailable = true // MARK: Swiftgram
let showTranslate = showTranslate && translateButtonAvailable
if #available(iOS 12.0, *) {

View File

@ -1,3 +1,4 @@
import SGSimpleSettings
import Foundation
import UIKit
import Display
@ -134,7 +135,7 @@ private final class TranslateScreenComponent: CombinedComponent {
}
func translate(text: String, fromLang: String?, toLang: String) -> Signal<(String, [MessageTextEntity])?, TranslationError> {
if self.useAlternativeTranslation {
if self.useAlternativeTranslation && SGSimpleSettings.shared.translationBackendEnum == .default {
return alternativeTranslateText(text: text, fromLang: fromLang, toLang: toLang)
} else {
return self.context.engine.messages.translate(text: text, toLang: toLang)
@ -1200,7 +1201,7 @@ public func presentTranslateScreen(
display: (ViewController) -> Void
) {
let translationConfiguration = TranslationConfiguration.with(appConfiguration: context.currentAppConfiguration.with { $0 })
var useSystemTranslation = false
var useSystemTranslation = SGSimpleSettings.shared.translationBackendEnum == .system
switch translationConfiguration.manual {
case .system:
if #available(iOS 18.0, *) {