Translator: Add romanizations setting in Translation settings#12829
Conversation
|
Can it be just a user patch of 2 lines? |
I'm sorry I don't understand what you mean by this. Are you suggesting I make some change? |
|
A niche setting. local Translator = require("ui/translator")
table.insert(Translator.trans_params.dt, "rm")Userpatches are: https://github.com/koreader/koreader/wiki/User-patches. |
|
@hius07 So, I have a few points to respond to that with:
|
Good point, agree. |
| rmIndex = i | ||
| break | ||
| end | ||
| end |
|
While we are in Translator. koreader/frontend/ui/translator.lua Line 296 in 8297d17 @Frenzie, isn't it more correct Translate into:?
|
|
I think it's a from/to, but into is also fine. |
| G_reader_settings:flipTrue("translator_from_romanizations") | ||
| end, | ||
| }, | ||
| { | ||
| text_func = function() | ||
| local lang = G_reader_settings:readSetting("translator_from_language") |
There was a problem hiding this comment.
(May be you could name the setting translator_with_romanizations or translator_show_romanizations to avoivd "same prefix but different purpose" with translator_from_language with using from?)
There was a problem hiding this comment.
Yeah, I re-used the _from_ segment to mean that the romanizations come from the "from" language (rather than the "to" language).
But maybe that's a little confusing.
There was a problem hiding this comment.
Renamed to translator_with_romanizations
| if G_reader_settings:isTrue("translator_from_romanizations") then | ||
| table.insert(self.trans_params.dt, "rm") | ||
| else | ||
| local rmIndex = nil | ||
| for i, v in pairs(self.trans_params.dt) do | ||
| if v == "rm" then | ||
| rmIndex = i |
There was a problem hiding this comment.
No other way avoid this and the ugly removal? Dunno (no time to check) how this appears in the query when dt is an array.
There was a problem hiding this comment.
Hmm, you're right.
Actually, I only had to do this to make the tests that I added pass since it reuses the Translator between tests. Actually, I'm pretty sure from testing it for real that this doesn't need to be done otherwise.
Do you have a suggestion how to better handle using this in tests?
There was a problem hiding this comment.
I moved the initialization of dt to this function so that it doesn't affect the tests. Now the tests still pass and it's not as ugly. Thanks for asking for me to look at this again.
There was a problem hiding this comment.
Depending on specifics you can also adjust the test code with for example before each and after each. (Mainly for future reference.)
| tsel = 0, -- ? | ||
| -- tk = "" -- auth token | ||
| dt = { -- what we want in result | ||
| "t", -- translation of source text | ||
| "at", -- alternate translations | ||
| -- Next options only give additional results when text is a single word |
There was a problem hiding this comment.
A bit sad to move this bit of documentation away from all the others :) Please let it here.
There was a problem hiding this comment.
That's probably why I ended up with the approach I had at first 😅
I can put the comments back here but I still think that dt should be added later on rather than here, so that it is clearer what's happening later on.
There was a problem hiding this comment.
We could put them all (with their documentation) where there are used, but that's a bit more than what this PR is about :)
Why there are here may not have been really thought (I may have just started translator.lua by copying wikipedia.lua), but the side effect of having it there is that people can user-patch it (like we suggested you to do :)) - so by hardcoded the replacement in the body of a function, the user patched stuff will be overridden.
| if G_reader_settings:isTrue("translator_with_romanizations") then | ||
| table.insert(self.trans_params.dt, "rm") | ||
| end | ||
| for k,v in pairs(self.trans_params) do | ||
| if type(v) == "table" then | ||
| for _, v2 in ipairs(v) do |
There was a problem hiding this comment.
I see that for things in the dt array, we just keep appending them with the same key: dt=aa&dt=bb&dt=cc.
So, may be just append yours after all the ones processed by this generic query construction.
ie. after this for loop:
if G_reader_settings:isTrue("translator_with_romanizations") then
query = query .. 'dt=rm&'
endThere was a problem hiding this comment.
But isn't it more consistent to have all the dt= parts be in the same structure first? Seems confusing to special-case this one.
There was a problem hiding this comment.
I don't mind the special case :)
But if you really don't want that, you could:
local dt = self.trans_params.dt
if G_reader_settings:isTrue("translator_with_romanizations") then
dt = util.tableDeepCopy(dt)
table.insert(dt, "rm"}
endWon't work as we just process the full self.trans_params...
I don't see another clean way.
There was a problem hiding this comment.
Alright, I went with appending to the query directly like you suggested.
| -- dt = { -- what we want in result | ||
| -- "t", -- translation of source text | ||
| -- "at", -- alternate translations | ||
| -- Next options only give additional results when text is a single word | ||
| -- "bd", -- dictionary (articles, reverse translations, etc) | ||
| -- "ex", -- examples | ||
| -- "ld", -- ? | ||
| "md", -- definitions of source text | ||
| -- "md", -- definitions of source text | ||
| -- "qca", -- ? |
There was a problem hiding this comment.
I also don't like you commenting these - while they are actually used.
This documents the whole API - commented out stuff means we won't need and use them (like you "rm"), so the ones we use should stick out from the ones we don't.
Again, can't think of something better than special casing and appending dt=rm to query.
There was a problem hiding this comment.
Ok, I put that back.
Co-authored-by: Frans de Jonge <fransdejonge@gmail.com>
…er#12829) This adds a new setting in "Translation settings" called "Show romanizations" which tells the translation popup to include in the query the parameter dt=rm and then extracts romanizations from the results to display.
This adds a new setting in "Translation settings" called "Show romanizations" which tells the translation popup to include in the query the parameter


dt=rmand then extracts romanizations from the results to display.This change is