Skip to content

Commit

Permalink
Improve language detection. Allow translation of ReaderMode pages!
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Sep 26, 2024
1 parent 9d59b02 commit 7b39433
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ extension BrowserViewController: TopToolbarDelegate {
if tab.translationState == .active {
scriptHandler.revertTranslation()
} else if tab.translationState != .active {
scriptHandler.startTranslation()
scriptHandler.startTranslation(canShowToast: true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {
)
as? BraveTranslateScriptHandler
{
scriptHandler.startTranslation()
scriptHandler.startTranslation(canShowToast: true)
}
}
}
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -305,29 +289,31 @@ struct TranslateToast: View {
}
.padding()
.frame(alignment: .leading)
.bravePopup(isPresented: $showTargetLanguageSelection) {
.bravePopup(isPresented: $showSourceLanguageSelection) {
TranslationOptionsView(
language: Binding(
get: {
languageInfo.pageLanguage
},
set: {
languageInfo.pageLanguage = $0
onLanguageSelectionChanged?(languageInfo)
}
)
)
.onDisappear {
showTargetLanguageSelection = false
}
}
.bravePopup(isPresented: $showSourceLanguageSelection) {
.bravePopup(isPresented: $showTargetLanguageSelection) {
TranslationOptionsView(
language: Binding(
get: {
languageInfo.currentLanguage
},
set: {
languageInfo.currentLanguage = $0 ?? Locale.current.language
onLanguageSelectionChanged?(languageInfo)
}
)
)
Expand All @@ -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 {
Expand All @@ -345,5 +349,5 @@ extension TranslateToast: PopoverContentComponent {
}

#Preview {
TranslateToast(languageInfo: .init())
TranslateToast(languageInfo: .init(), onLanguageSelectionChanged: nil)
}
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/Brave/Frontend/Reader/Reader.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
<html lang="en">
<html>

<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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,
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit 7b39433

Please sign in to comment.