diff --git a/Swiftgram/SGSettingsUI/Sources/SGSettingsController.swift b/Swiftgram/SGSettingsUI/Sources/SGSettingsController.swift index 1ff826f27b..4ba0f5ebb9 100644 --- a/Swiftgram/SGSettingsUI/Sources/SGSettingsController.swift +++ b/Swiftgram/SGSettingsUI/Sources/SGSettingsController.swift @@ -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) diff --git a/Swiftgram/SGSimpleSettings/Sources/SimpleSettings.swift b/Swiftgram/SGSimpleSettings/Sources/SimpleSettings.swift index c9485751b1..cc3d1b4e4e 100644 --- a/Swiftgram/SGSimpleSettings/Sources/SimpleSettings.swift +++ b/Swiftgram/SGSimpleSettings/Sources/SimpleSettings.swift @@ -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 diff --git a/Swiftgram/SGStrings/Strings/en.lproj/SGLocalizable.strings b/Swiftgram/SGStrings/Strings/en.lproj/SGLocalizable.strings index 7491974f88..f63f7d2bb3 100644 --- a/Swiftgram/SGStrings/Strings/en.lproj/SGLocalizable.strings +++ b/Swiftgram/SGStrings/Strings/en.lproj/SGLocalizable.strings @@ -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?"; diff --git a/submodules/SettingsUI/Sources/Language Selection/LocalizationListControllerNode.swift b/submodules/SettingsUI/Sources/Language Selection/LocalizationListControllerNode.swift index 666d7721f9..4d55a03582 100644 --- a/submodules/SettingsUI/Sources/Language Selection/LocalizationListControllerNode.swift +++ b/submodules/SettingsUI/Sources/Language Selection/LocalizationListControllerNode.swift @@ -457,7 +457,7 @@ final class LocalizationListControllerNode: ViewControllerTracingNode { default: break } - + chatTranslationAvailable = true; translateButtonAvailable = true // MARK: Swiftgram let previousState = Atomic(value: nil) let previousEntriesHolder = Atomic<([LanguageListEntry], PresentationTheme, PresentationStrings)?>(value: nil) self.listDisposable = combineLatest( diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift index 92eeda6307..4cb06de0d5 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Translate.swift @@ -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 public func gtranslate(_ text: String, _ toLang: String) -> Signal { 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 diff --git a/submodules/TranslateUI/BUILD b/submodules/TranslateUI/BUILD index 3ab31455e4..7fb0cbca6e 100644 --- a/submodules/TranslateUI/BUILD +++ b/submodules/TranslateUI/BUILD @@ -1,6 +1,7 @@ load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") sgdeps = [ + "//Swiftgram/SGSimpleSettings:SGSimpleSettings", "//submodules/TextFormat:TextFormat" ] diff --git a/submodules/TranslateUI/Sources/Translate.swift b/submodules/TranslateUI/Sources/Translate.swift index f587da8b5d..b5d25432ac 100644 --- a/submodules/TranslateUI/Sources/Translate.swift +++ b/submodules/TranslateUI/Sources/Translate.swift @@ -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, *) { diff --git a/submodules/TranslateUI/Sources/TranslateScreen.swift b/submodules/TranslateUI/Sources/TranslateScreen.swift index d246a19ad3..072cabae61 100644 --- a/submodules/TranslateUI/Sources/TranslateScreen.swift +++ b/submodules/TranslateUI/Sources/TranslateScreen.swift @@ -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, *) {