Skip to content

Translator: translate current page #10438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 17, 2023
Merged

Conversation

hius07
Copy link
Member

@hius07 hius07 commented May 14, 2023

Available via the menu and with a gesture.
Forces book language (if available) as a source language.
Shows main translation only, without source text, alternative translations and definitions.
Just a page to read.
Closes #10199.

01

02


This change is Reviewable

@poire-z poire-z assigned poire-z and unassigned poire-z May 14, 2023
Comment on lines 487 to 528
if NetworkMgr:willRerunWhenOnline(function() self:showTranslation(text, target_lang, source_lang, from_highlight, page, index) end) then
if NetworkMgr:willRerunWhenOnline(function()
self:showTranslation(text, target_lang, source_lang, from_highlight, page, index, full_page) end) then
return
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's more readable than before :)
May be go a bit more proper:

    if NetworkMgr:willRerunWhenOnline(function()
                self:showTranslation(text, target_lang, source_lang, from_highlight, page, index, full_page)
            end) then
        return
    end

if not full_page then
table.insert(output, "▣ " .. table.concat(source, " "))
end
text_main = (full_page and "" or "● ") .. table.concat(translated, " ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(When naming this new parameter "full_page", did you think about translating the current full page - or showing the result translation full page ? I would have named it "no_alt_translation" or "simple_translation" as you're just ignoring all the other translations/definitions. But may be this ambivalent "full_page", because it is ambivalent, is fine.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this snippet would read better if you kept the 2 ways separate, rather than (full_page and "" or "● ") ..:

if full_page then
        text_main = table.concat(translated, " ")
else
        table.insert(output, "" .. table.concat(source, " "))
        text_main = "" .. table.concat(translated, " ")
end

Comment on lines 697 to 701
function Translator:onTranslateCurrentPage()
local ui = require("apps/reader/readerui").instance
if not ui or not ui.document then return end
local x0, y0, x1, y1, page
if ui.rolling then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels all this "reader" business should better be done in your added:

function ReaderHighlight:onTranslateCurrentPage()
    Translator:onTranslateCurrentPage()
end

It is still ReaderHighlight:onTranslateCurrentPage that will always be called, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it must be done in ReaderHighlight.

@@ -202,6 +202,7 @@ local order = {
"wikipedia_history",
"wikipedia_settings",
"----------------------------",
"translate_page",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will add a 11th item in that menu (10th for me with Vocabulary builder disabled), so making it 2 pages :/
No real better idea. In the 2nd menu with a page icon? so-so.
May be the dict & wiki lookup history should be moved into their respecting ... settings, or be in the own submenu?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

12 (incl. calibre search).
Maybe move dict, wiki and translation settings into Settings submenu?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, might be a good idea, now that "Translation" has one "action" item in there.

@hius07
Copy link
Member Author

hius07 commented May 15, 2023

03

04

Comment on lines 472 to 475
-- main menu Search
menu_items.search_settings = { -- submenu with Dict, Wiki, Translation settings
text = _("Settings"),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be this one should better be in readerdictionary, which is the one that brings into it the most subitems and the first in that search menu? (ReaderHighlight feels a bit unrelated).

Copy link
Member Author

@hius07 hius07 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about wordings inconsistency Dictionary lookup history vs Wikipedia history?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno when/where this difference comes from :/ @Frenzie will tell us if/how we should fix that.

Copy link
Member

@Frenzie Frenzie May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where it came from, but I think "Dictionary history" might be a bit ambiguous? So in that sense I'd prefer Wikipedia lookup history if you feel it lacks balance, but I think it's fine as is.

@@ -340,6 +342,8 @@ local dispatcher_menu_order = {
"book_description",
"book_cover",

"translate_page",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(OK about having it here, can't find a better place in the Reader> actions menu.)

@@ -526,6 +557,7 @@ function Translator:_showTranslation(text, target_lang, source_lang, from_highli
end
local output = {}
local text_main = ""
local not_full_page = not full_page
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels ugly and prone to typo :)
And it means "detailed".
So, may be name it everywhere "detailed", and if the default should be true:
if detailed == nil then detailed = true end
and pass false when you don't want it.

end)
end

function Translator:_showTranslation(text, target_lang, source_lang, from_highlight, page, index)
function Translator:_showTranslation(text, target_lang, source_lang, from_highlight, page, index, translate_page)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some thoughts (not insisting too much, but just in case you'd agree :):
translate_page still feels a bit odd.
Translator can be seen as some utility tool, and shouldn't really be that linked to the idea of translating a book page or a fragment of a book content. It could be used to translate a Book description, or some Wikipedia result content.
So, ideally, the params shouldn't indicate what's the source (with some inner logic to decide what's expected as the result), but directly what's expected. So, "detailed" vs "simple".
We do pass from_highlight/page/index, so yes, there is a bit of outer context, but that's just for adding buttons.
(Isn't from_highlight a synonym of translate_fragment, ie. it's not there when you translate a page? Or can we don't have it even with fragments?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"detailed" vs "simple"

Yes, I thought about these wordings, but translate_page mode also gets source lang from the book and shows bigger window.

Isn't from_highlight a synonym of translate_fragment, ie. it's not there when you translate a page? Or can we don't have it even with fragments?

We can translate_fragment from the pen_button in DictQuickLookup,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but translate_page mode also gets source lang from the book

Shouldn't it be readerhighlight that should get the source lang from the self.ui it knows and pass it to Translator:showTranslation(), instead of passing nil?

and shows bigger window

This could be implied when using "simple", or passed as another parameter (ie. large_window).

@hius07 hius07 merged commit 3dce412 into koreader:master May 17, 2023
@hius07 hius07 added this to the 2023.05 milestone May 17, 2023
@hius07 hius07 deleted the translate-page branch May 17, 2023 04:34
Frenzie pushed a commit that referenced this pull request May 18, 2023
Fix translation for books without "language" in properties (it is "", Translator expects nil).
Regression after #10438.
@poire-z
Copy link
Contributor

poire-z commented May 20, 2023

Looks like "[x] Auto detect source language" has no longer the intended effect.
On a French book, highlighting a german sentence, and translate'ing, I get:
Calling https://translate.googleapis.com/translate_a/single?sl=fr&oe=UTF-8&hl=en&ssel=0&tsel=0&dt=t&dt=at&dt=md&client=gtx&tl=fr&ie=UTF-8&otf=1&q=the-words-in-german
and I get the title "Translation from French".
Previously (on an older appimage), I got "Translation from German".

@Frenzie
Copy link
Member

Frenzie commented Jun 7, 2023

Sounds like Google autodetect got worse? ;-P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instant translation possible ?
3 participants