Translator: copy translation to clipboard or save to note (#8669)

reviewable/pr8673/r3
hius07 2 years ago committed by GitHub
parent 8b43811812
commit 2a244278e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -993,7 +993,7 @@ function ReaderBookmark:updateBookmark(item)
end
end
function ReaderBookmark:renameBookmark(item, from_highlight, is_new_note)
function ReaderBookmark:renameBookmark(item, from_highlight, is_new_note, new_text)
local bookmark
if from_highlight then
-- Called by ReaderHighlight:editHighlight, we need to find the bookmark
@ -1017,10 +1017,18 @@ function ReaderBookmark:renameBookmark(item, from_highlight, is_new_note)
else
bookmark = item
end
local input_text = bookmark.text_orig
if new_text then
if self:isBookmarkAutoText(bookmark) then
input_text = new_text
else
input_text = input_text .. "\n\n" .. new_text
end
end
self.input = InputDialog:new{
title = _("Edit note"),
description = " " .. T(_("Page: %1"), bookmark.mandatory) .. " " .. T(_("Time: %1"), bookmark.datetime),
input = bookmark.text_orig,
input = input_text,
allow_newline = true,
add_scroll_buttons = true,
use_available_height = true,

@ -118,11 +118,11 @@ function ReaderHighlight:init()
end,
}
end,
["07_translate"] = function(_self)
["07_translate"] = function(_self, page, index)
return {
text = _("Translate"),
callback = function()
_self:translate(_self.selected_text)
_self:translate(_self.selected_text, page, index)
-- We don't call _self:onClose(), so one can still see
-- the highlighted text when moving the translated
-- text window, and also if NetworkMgr:promptWifiOn()
@ -723,7 +723,7 @@ function ReaderHighlight:onShowHighlightDialog(page, index, is_auto_text)
text = _(""),
callback = function()
self.selected_text = self.view.highlight.saved[page][index]
self:onShowHighlightMenu()
self:onShowHighlightMenu(page, index)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
end,
@ -801,7 +801,7 @@ function ReaderHighlight:removeFromHighlightDialog(idx)
return button
end
function ReaderHighlight:onShowHighlightMenu()
function ReaderHighlight:onShowHighlightMenu(page, index)
if not self.selected_text then
return
end
@ -810,7 +810,7 @@ function ReaderHighlight:onShowHighlightMenu()
local columns = 2
for idx, fn_button in ffiUtil.orderedPairs(self._highlight_buttons) do
local button = fn_button(self)
local button = fn_button(self, page, index)
if not button.show_in_highlight_dialog_func or button.show_in_highlight_dialog_func() then
if #highlight_buttons[#highlight_buttons] >= columns then
table.insert(highlight_buttons, {})
@ -1300,9 +1300,9 @@ function ReaderHighlight:viewSelectionHTML(debug_view, no_css_files_buttons)
end
end
function ReaderHighlight:translate(selected_text)
function ReaderHighlight:translate(selected_text, page, index)
if selected_text.text ~= "" then
self:onTranslateText(selected_text.text)
self:onTranslateText(selected_text.text, page, index)
-- or we will do OCR
elseif self.hold_pos then
local text = self.ui.document:getOCRText(self.hold_pos.page, selected_text)
@ -1322,8 +1322,8 @@ dbg:guard(ReaderHighlight, "translate",
"translate must not be called with nil selected_text!")
end)
function ReaderHighlight:onTranslateText(text)
Translator:showTranslation(text)
function ReaderHighlight:onTranslateText(text, page, index)
Translator:showTranslation(text, false, false, true, page, index)
end
function ReaderHighlight:onHoldRelease()
@ -1629,9 +1629,10 @@ If you wish your highlights to be saved in the document, just move it to a writa
end
end
function ReaderHighlight:addNote()
function ReaderHighlight:addNote(text)
local page, index = self:saveHighlight()
self:editHighlight(page, index, true)
if text then self:clear() end
self:editHighlight(page, index, true, text)
UIManager:close(self.edit_highlight_dialog)
self.edit_highlight_dialog = nil
self.ui:handleEvent(Event:new("AddNote"))
@ -1687,15 +1688,16 @@ function ReaderHighlight:deleteHighlight(page, i, bookmark_item)
logger.dbg("delete highlight from document", removed)
self.ui.document:deleteHighlight(page, removed)
end
UIManager:setDirty(self.dialog, "ui")
end
function ReaderHighlight:editHighlight(page, i, is_new_note)
function ReaderHighlight:editHighlight(page, i, is_new_note, text)
local item = self.view.highlight.saved[page][i]
self.ui.bookmark:renameBookmark({
page = self.ui.document.info.has_pages and page or item.pos0,
datetime = item.datetime,
pboxes = item.pboxes
}, true, is_new_note)
}, true, is_new_note, text)
end
function ReaderHighlight:editHighlightStyle(page, i)

@ -477,13 +477,13 @@ Show translated text in TextViewer, with alternate translations
@string target_lang[opt] (`"en"`, `"fr"`, ``)
@string source_lang[opt="auto"] (`"en"`, `"fr"`, ``) or `"auto"` to auto-detect source language
--]]
function Translator:showTranslation(text, target_lang, source_lang)
function Translator:showTranslation(text, target_lang, source_lang, from_highlight, page, index)
if Device:hasClipboard() then
Device.input.setClipboardText(text)
end
local NetworkMgr = require("ui/network/manager")
if NetworkMgr:willRerunWhenOnline(function() self:showTranslation(text, target_lang, source_lang) end) then
if NetworkMgr:willRerunWhenOnline(function() self:showTranslation(text, target_lang, source_lang, from_highlight, page, index) end) then
return
end
@ -491,11 +491,11 @@ function Translator:showTranslation(text, target_lang, source_lang)
-- translation service query.
local Trapper = require("ui/trapper")
Trapper:wrap(function()
self:_showTranslation(text, target_lang, source_lang)
self:_showTranslation(text, target_lang, source_lang, from_highlight, page, index)
end)
end
function Translator:_showTranslation(text, target_lang, source_lang)
function Translator:_showTranslation(text, target_lang, source_lang, from_highlight, page, index)
if not target_lang then
target_lang = self:getTargetLanguage()
end
@ -524,6 +524,7 @@ function Translator:_showTranslation(text, target_lang, source_lang)
source_lang = result[3]
end
local output = {}
local text_main = ""
-- For both main and alternate translations, we may get multiple slices
-- of the original text and its translations.
@ -539,7 +540,8 @@ function Translator:_showTranslation(text, target_lang, source_lang)
table.insert(translated, t)
end
table.insert(output, "" .. table.concat(source, " "))
table.insert(output, "" .. table.concat(translated, " "))
text_main = "" .. table.concat(translated, " ")
table.insert(output, text_main)
end
if result[6] and type(result[6]) == "table" and #result[6] > 0 then
@ -574,14 +576,79 @@ function Translator:_showTranslation(text, target_lang, source_lang)
end
-- table.insert(output, require("dump")(result)) -- for debugging
UIManager:show(TextViewer:new{
local text_all = table.concat(output, "\n")
local textviewer
local buttons_table = {
{
{
text = _("Close"),
is_enter_default = true,
callback = function()
UIManager:close(textviewer)
end,
},
},
}
if Device:hasClipboard() then
table.insert(buttons_table, 1,
{
{
text = _("Copy main translation"),
callback = function()
Device.input.setClipboardText(text_main)
end,
},
{
text = _("Copy all"),
callback = function()
Device.input.setClipboardText(text_all)
end,
},
}
)
end
if from_highlight then
local ui = require("apps/reader/readerui").instance
table.insert(buttons_table, 1,
{
{
text = _("Save main translation to note"),
callback = function()
UIManager:close(textviewer)
UIManager:close(ui.highlight.highlight_dialog)
if page then
ui.highlight:editHighlight(page, index, false, text_main)
else
ui.highlight:addNote(text_main)
end
end,
},
{
text = _("Save all to note"),
callback = function()
UIManager:close(textviewer)
UIManager:close(ui.highlight.highlight_dialog)
if page then
ui.highlight:editHighlight(page, index, false, text_all)
else
ui.highlight:addNote(text_all)
end
end,
},
}
)
end
textviewer = TextViewer:new{
title = T(_("Translation from %1"), self:getLanguageName(source_lang, "?")),
title_multilines = true,
-- Showing the translation target language in this title may make
-- it quite long and wrapped, taking valuable vertical spacing
text = table.concat(output, "\n"),
text = text_all,
height = math.floor(Screen:getHeight() * 0.8),
justified = G_reader_settings:nilOrTrue("dict_justify"),
})
buttons_table = buttons_table,
}
UIManager:show(textviewer)
end
return Translator

Loading…
Cancel
Save