mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-07-19 09:41:29 +00:00
Improve translator
This commit is contained in:
parent
2f4076d85f
commit
fe28c9d96d
@ -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(.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(.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(.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(.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(.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(.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(.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(.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(.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)))
|
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 {
|
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
|
items.append(ActionSheetButtonItem(title: i18n("Settings.Translation.Backend.\(value.rawValue)", presentationData.strings.baseLanguageCode), color: .accent, action: { [weak actionSheet] in
|
||||||
actionSheet?.dismissAnimated()
|
actionSheet?.dismissAnimated()
|
||||||
setAction(value.rawValue)
|
setAction(value.rawValue)
|
||||||
|
@ -179,6 +179,8 @@ public class SGSimpleSettings {
|
|||||||
public enum TranslationBackend: String, CaseIterable {
|
public enum TranslationBackend: String, CaseIterable {
|
||||||
case `default`
|
case `default`
|
||||||
case gtranslate
|
case gtranslate
|
||||||
|
case system
|
||||||
|
// Make sure to update TranslationConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum PinnedMessageNotificationsSettings: String, CaseIterable {
|
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 {
|
public func getSGDownloadPartSize(_ default: Int64, fileSize: Int64?) -> Int64 {
|
||||||
let currentDownloadSetting = SGSimpleSettings.shared.downloadSpeedBoost
|
let currentDownloadSetting = SGSimpleSettings.shared.downloadSpeedBoost
|
||||||
// Increasing chunk size for small files make it worse in terms of overall download performance
|
// Increasing chunk size for small files make it worse in terms of overall download performance
|
||||||
|
@ -40,7 +40,8 @@
|
|||||||
"Settings.Translation.Backend.default" = "Telegram";
|
"Settings.Translation.Backend.default" = "Telegram";
|
||||||
/* Do not translate */
|
/* Do not translate */
|
||||||
"Settings.Translation.Backend.gtranslate" = "GTranslate";
|
"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.Header" = "VOICE-TO-TEXT";
|
||||||
"Settings.Transcription.Backend" = "Service";
|
"Settings.Transcription.Backend" = "Service";
|
||||||
@ -48,7 +49,7 @@
|
|||||||
"Settings.Transcription.Backend.default" = "Telegram";
|
"Settings.Transcription.Backend.default" = "Telegram";
|
||||||
/* Do not translate */
|
/* Do not translate */
|
||||||
"Settings.Transcription.Backend.apple" = "Apple";
|
"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.Author" = "Author";
|
||||||
"Stories.Warning.ViewStory" = "View Story?";
|
"Stories.Warning.ViewStory" = "View Story?";
|
||||||
|
@ -457,7 +457,7 @@ final class LocalizationListControllerNode: ViewControllerTracingNode {
|
|||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
chatTranslationAvailable = true; translateButtonAvailable = true // MARK: Swiftgram
|
||||||
let previousState = Atomic<LocalizationListState?>(value: nil)
|
let previousState = Atomic<LocalizationListState?>(value: nil)
|
||||||
let previousEntriesHolder = Atomic<([LanguageListEntry], PresentationTheme, PresentationStrings)?>(value: nil)
|
let previousEntriesHolder = Atomic<([LanguageListEntry], PresentationTheme, PresentationStrings)?>(value: nil)
|
||||||
self.listDisposable = combineLatest(
|
self.listDisposable = combineLatest(
|
||||||
|
@ -394,7 +394,7 @@ public func parseTranslateResponse(_ data: String) -> String {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getGoogleLang(_ userLang: String) -> String {
|
public func getGTranslateLang(_ userLang: String) -> String {
|
||||||
var lang = userLang
|
var lang = userLang
|
||||||
let rawSuffix = "-raw"
|
let rawSuffix = "-raw"
|
||||||
if lang.hasSuffix(rawSuffix) {
|
if lang.hasSuffix(rawSuffix) {
|
||||||
@ -416,7 +416,11 @@ public func getGoogleLang(_ userLang: String) -> String {
|
|||||||
|
|
||||||
|
|
||||||
// Fix for pt-br and other regional langs
|
// 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]
|
lang = lang.components(separatedBy: "-")[0].components(separatedBy: "_")[0]
|
||||||
|
|
||||||
return lang
|
return lang
|
||||||
@ -477,7 +481,7 @@ public func requestTranslateUrl(url: URL) -> Signal<String, TranslateFetchError>
|
|||||||
|
|
||||||
public func gtranslate(_ text: String, _ toLang: String) -> Signal<String, TranslateFetchError> {
|
public func gtranslate(_ text: String, _ toLang: String) -> Signal<String, TranslateFetchError> {
|
||||||
return Signal { subscriber in
|
return Signal { subscriber in
|
||||||
let urlString = getTranslateUrl(text, getGoogleLang(toLang))
|
let urlString = getTranslateUrl(text, getGTranslateLang(toLang))
|
||||||
let url = URL(string: urlString)!
|
let url = URL(string: urlString)!
|
||||||
let translateSignal = requestTranslateUrl(url: url)
|
let translateSignal = requestTranslateUrl(url: url)
|
||||||
var translateDisposable: Disposable? = nil
|
var translateDisposable: Disposable? = nil
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
|
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
|
||||||
|
|
||||||
sgdeps = [
|
sgdeps = [
|
||||||
|
"//Swiftgram/SGSimpleSettings:SGSimpleSettings",
|
||||||
"//submodules/TextFormat:TextFormat"
|
"//submodules/TextFormat:TextFormat"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -185,7 +185,7 @@ public func canTranslateChats(context: AccountContext) -> Bool {
|
|||||||
default:
|
default:
|
||||||
break
|
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?) {
|
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:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
translateButtonAvailable = true // MARK: Swiftgram
|
||||||
let showTranslate = showTranslate && translateButtonAvailable
|
let showTranslate = showTranslate && translateButtonAvailable
|
||||||
|
|
||||||
if #available(iOS 12.0, *) {
|
if #available(iOS 12.0, *) {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import SGSimpleSettings
|
||||||
import Foundation
|
import Foundation
|
||||||
import UIKit
|
import UIKit
|
||||||
import Display
|
import Display
|
||||||
@ -134,7 +135,7 @@ private final class TranslateScreenComponent: CombinedComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func translate(text: String, fromLang: String?, toLang: String) -> Signal<(String, [MessageTextEntity])?, TranslationError> {
|
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)
|
return alternativeTranslateText(text: text, fromLang: fromLang, toLang: toLang)
|
||||||
} else {
|
} else {
|
||||||
return self.context.engine.messages.translate(text: text, toLang: toLang)
|
return self.context.engine.messages.translate(text: text, toLang: toLang)
|
||||||
@ -1200,7 +1201,7 @@ public func presentTranslateScreen(
|
|||||||
display: (ViewController) -> Void
|
display: (ViewController) -> Void
|
||||||
) {
|
) {
|
||||||
let translationConfiguration = TranslationConfiguration.with(appConfiguration: context.currentAppConfiguration.with { $0 })
|
let translationConfiguration = TranslationConfiguration.with(appConfiguration: context.currentAppConfiguration.with { $0 })
|
||||||
var useSystemTranslation = false
|
var useSystemTranslation = SGSimpleSettings.shared.translationBackendEnum == .system
|
||||||
switch translationConfiguration.manual {
|
switch translationConfiguration.manual {
|
||||||
case .system:
|
case .system:
|
||||||
if #available(iOS 18.0, *) {
|
if #available(iOS 18.0, *) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user