From 7b39433074bdfab6208fbda8903fa3650f907602 Mon Sep 17 00:00:00 2001 From: Brandon T Date: Thu, 12 Sep 2024 18:52:55 -0400 Subject: [PATCH] Improve language detection. Allow translation of ReaderMode pages! --- .../BVC+ReaderMode.swift | 8 +++- .../BVC+ToolbarDelegate.swift | 2 +- .../BrowserViewController/BVC+Translate.swift | 19 +++++--- .../Browser/Translate/TranslateToast.swift | 44 ++++++++++--------- .../Sources/Brave/Frontend/Reader/Reader.html | 2 +- .../BraveTranslateScriptHandler.swift | 16 ++++--- 6 files changed, 55 insertions(+), 36 deletions(-) diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift index 117b7a7c0c12..5166079572c0 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ReaderMode.swift @@ -161,10 +161,12 @@ extension BrowserViewController { let playlistItem = tab.playlistItem webView.go(to: backList.last!) PlaylistScriptHandler.updatePlaylistTab(tab: tab, item: playlistItem) + self.updateTranslateURLBar(tab: tab, state: tab.translationState) } else if !forwardList.isEmpty && forwardList.first?.url == readerModeURL { let playlistItem = tab.playlistItem webView.go(to: forwardList.first!) PlaylistScriptHandler.updatePlaylistTab(tab: tab, item: playlistItem) + self.updateTranslateURLBar(tab: tab, state: tab.translationState) } else { // Store the readability result in the cache and load it. This will later move to the ReadabilityHelper. webView.evaluateSafeJavaScript( @@ -173,10 +175,11 @@ extension BrowserViewController { ) { (object, error) -> Void in if let readabilityResult = ReadabilityResult(object: object as AnyObject?) { let playlistItem = tab.playlistItem - Task { + Task { @MainActor in try? await self.readerModeCache.put(currentURL, readabilityResult) if webView.load(PrivilegedRequest(url: readerModeURL) as URLRequest) != nil { PlaylistScriptHandler.updatePlaylistTab(tab: tab, item: playlistItem) + self.updateTranslateURLBar(tab: tab, state: tab.translationState) } } } @@ -202,14 +205,17 @@ extension BrowserViewController { let playlistItem = tab.playlistItem webView.go(to: backList.last!) PlaylistScriptHandler.updatePlaylistTab(tab: tab, item: playlistItem) + self.updateTranslateURLBar(tab: tab, state: tab.translationState) } else if !forwardList.isEmpty && forwardList.first?.url == originalURL { let playlistItem = tab.playlistItem webView.go(to: forwardList.first!) PlaylistScriptHandler.updatePlaylistTab(tab: tab, item: playlistItem) + self.updateTranslateURLBar(tab: tab, state: tab.translationState) } else { let playlistItem = tab.playlistItem if webView.load(URLRequest(url: originalURL)) != nil { PlaylistScriptHandler.updatePlaylistTab(tab: tab, item: playlistItem) + self.updateTranslateURLBar(tab: tab, state: tab.translationState) } } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ToolbarDelegate.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ToolbarDelegate.swift index afb106a3c93d..da44edbcd3eb 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ToolbarDelegate.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ToolbarDelegate.swift @@ -270,7 +270,7 @@ extension BrowserViewController: TopToolbarDelegate { if tab.translationState == .active { scriptHandler.revertTranslation() } else if tab.translationState != .active { - scriptHandler.startTranslation() + scriptHandler.startTranslation(canShowToast: true) } } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Translate.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Translate.swift index ec61ecd94241..d6a49773ae2e 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Translate.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Translate.swift @@ -27,7 +27,7 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate { ) as? BraveTranslateScriptHandler { - scriptHandler.startTranslation() + scriptHandler.startTranslation(canShowToast: true) } } } @@ -110,15 +110,20 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate { } } - func presentToast(_ languageInfo: BraveTranslateLanguageInfo) { + func presentToast(tab: Tab?, languageInfo: BraveTranslateLanguageInfo) { let popover = PopoverController( - content: TranslateToast(languageInfo: languageInfo), + content: TranslateToast(languageInfo: languageInfo) { [weak tab] languageInfo in + + if let scriptHandler = tab?.getContentScript( + name: BraveTranslateScriptHandler.scriptName + ) + as? BraveTranslateScriptHandler + { + scriptHandler.startTranslation(canShowToast: false) + } + }, autoLayoutConfiguration: nil ) - - popover.popoverDidDismiss = { [weak self] _ in - - } popover.present(from: self.topToolbar.locationView.translateButton, on: self) } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/Translate/TranslateToast.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/Translate/TranslateToast.swift index 9f8e5eec9d44..2e7517109937 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/Translate/TranslateToast.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/Translate/TranslateToast.swift @@ -228,23 +228,7 @@ struct TranslateToast: View { @ObservedObject var languageInfo: BraveTranslateLanguageInfo - var currentLanguageName: String { - if let languageCode = languageInfo.currentLanguage.languageCode?.identifier, - let languageName = Locale.current.localizedString(forLanguageCode: languageCode) - { - return languageName - } - return "Unknown Language" - } - - var pageLanguageName: String { - if let languageCode = languageInfo.pageLanguage?.languageCode?.identifier, - let languageName = Locale.current.localizedString(forLanguageCode: languageCode) - { - return languageName - } - return "Unknown Language" - } + var onLanguageSelectionChanged: ((BraveTranslateLanguageInfo) -> Void)? var body: some View { HStack { @@ -305,7 +289,7 @@ struct TranslateToast: View { } .padding() .frame(alignment: .leading) - .bravePopup(isPresented: $showTargetLanguageSelection) { + .bravePopup(isPresented: $showSourceLanguageSelection) { TranslationOptionsView( language: Binding( get: { @@ -313,6 +297,7 @@ struct TranslateToast: View { }, set: { languageInfo.pageLanguage = $0 + onLanguageSelectionChanged?(languageInfo) } ) ) @@ -320,7 +305,7 @@ struct TranslateToast: View { showTargetLanguageSelection = false } } - .bravePopup(isPresented: $showSourceLanguageSelection) { + .bravePopup(isPresented: $showTargetLanguageSelection) { TranslationOptionsView( language: Binding( get: { @@ -328,6 +313,7 @@ struct TranslateToast: View { }, set: { languageInfo.currentLanguage = $0 ?? Locale.current.language + onLanguageSelectionChanged?(languageInfo) } ) ) @@ -336,6 +322,24 @@ struct TranslateToast: View { } } } + + private var currentLanguageName: String { + if let languageCode = languageInfo.currentLanguage.languageCode?.identifier, + let languageName = Locale.current.localizedString(forLanguageCode: languageCode) + { + return languageName + } + return "Unknown Language" + } + + private var pageLanguageName: String { + if let languageCode = languageInfo.pageLanguage?.languageCode?.identifier, + let languageName = Locale.current.localizedString(forLanguageCode: languageCode) + { + return languageName + } + return "Unknown Language" + } } extension TranslateToast: PopoverContentComponent { @@ -345,5 +349,5 @@ extension TranslateToast: PopoverContentComponent { } #Preview { - TranslateToast(languageInfo: .init()) + TranslateToast(languageInfo: .init(), onLanguageSelectionChanged: nil) } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Reader/Reader.html b/ios/brave-ios/Sources/Brave/Frontend/Reader/Reader.html index d8bf8bcff624..8c2cca1a7c8b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Reader/Reader.html +++ b/ios/brave-ios/Sources/Brave/Frontend/Reader/Reader.html @@ -2,7 +2,7 @@ - + diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift index 427d8d691180..32ce59abf4ab 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/BraveTranslateScriptHandler.swift @@ -15,7 +15,7 @@ import os.log protocol BraveTranslateScriptHandlerDelegate: NSObject { func updateTranslateURLBar(tab: Tab?, state: TranslateURLBarButton.TranslateState) func showTranslateOnboarding(tab: Tab?, completion: @escaping (_ translateEnabled: Bool) -> Void) - func presentToast(_ languageInfo: BraveTranslateLanguageInfo) + func presentToast(tab: Tab?, languageInfo: BraveTranslateLanguageInfo) } class BraveTranslateScriptLanguageDetectionHandler: NSObject, TabContentScript { @@ -70,7 +70,8 @@ class BraveTranslateScriptLanguageDetectionHandler: NSObject, TabContentScript { if message.hasNoTranslate { delegate.currentLanguageInfo.pageLanguage = delegate.currentLanguageInfo.currentLanguage } else { - delegate.currentLanguageInfo.pageLanguage = Locale.Language(identifier: message.htmlLang) + delegate.currentLanguageInfo.pageLanguage = + !message.htmlLang.isEmpty ? Locale.Language(identifier: message.htmlLang) : nil } replyHandler(nil, nil) @@ -244,7 +245,8 @@ class BraveTranslateScriptHandler: NSObject, TabContentScript { } // Language identified via our own Javascript - if let languageCode = await executePageFunction(name: "getPageLanguage") { + if let languageCode = await executePageFunction(name: "getPageLanguage"), !languageCode.isEmpty + { return Locale.Language(identifier: languageCode) } @@ -266,7 +268,7 @@ class BraveTranslateScriptHandler: NSObject, TabContentScript { return .init(currentLanguage: Locale.current.language, pageLanguage: pageLanguage) } - func startTranslation() { + func startTranslation(canShowToast: Bool) { Task { @MainActor [weak self] in guard let self = self, let currentLanguage = currentLanguageInfo.currentLanguage.languageCode?.identifier, @@ -288,8 +290,10 @@ class BraveTranslateScriptHandler: NSObject, TabContentScript { ] ) - self.delegate?.updateTranslateURLBar(tab: self.tab, state: .active) - self.delegate?.presentToast(currentLanguageInfo) + if canShowToast { + self.delegate?.updateTranslateURLBar(tab: self.tab, state: .active) + self.delegate?.presentToast(tab: self.tab, languageInfo: currentLanguageInfo) + } } }