soimort / translate-shell

:speech_balloon: Command-line translator using Google Translate, Bing Translator, Yandex.Translate, etc.
https://www.soimort.org/translate-shell
The Unlicense
6.83k stars 387 forks source link

Add compact option for compact dictionary output #484

Open txgk opened 1 year ago

txgk commented 1 year ago

Hi, first of all I would like to thank you for such wonderful project! It makes my translations really quick and allows me to return to reading straightaway without being distracted by overloaded translation services in the web browser.

In this issue I would like to resolve a little problem I ran into. For convenient reading of hieroglyphs, I increased the font size in my terminal and now it fits a slightly smaller number of lines than before and pretty often translate-shell output doesn't fit in one page, so I have to scroll terminal buffer within my tmux session which is quite tedious.

Let's take a look at output of trans -s zh-CN -t en 量:

量
(Liàng)

quantity

Definitions of 量
[ 简体中文 -> English ]

noun
    amount
        量, 额, 数量, 多少, 数目, 数字
    volume
        卷, 体积, 音量, 量, 额, 册
    quantity
        数量, 量, 份量, 额
    capacity
        容量, 量, 才能, 潜力, 器
    measure
        措施, 测度, 度量, 量, 度, 手段
    quantum
        量子, 量, 额, 限量
    estimate
        预算, 量, 揆
    mete
        量

verb
    amount
        量, 等于, 合计, 共计, 折合, 为数
    measure
        测量, 衡量, 测, 计量, 量, 衡
    estimate
        估计, 预计, 估, 估量, 量, 计量
    mete
        量

adjective
    quantitative
        量

量
    quantity, the amount, amount

As you can see this content takes a lot of terminal lines. It could be a significantly more compact if dictionary would place translation on one line with the definitions, e. g.:

    amount - 量, 额, 数量, 多少, 数目, 数字

instead of

    amount
        量, 额, 数量, 多少, 数目, 数字

So I decided to dig into the codebase of translate-shell and found a solution:

--- a/include/Translators/GoogleTranslate.awk
+++ b/include/Translators/GoogleTranslate.awk
@@ -430,9 +430,9 @@ function googleTranslate(text, sl, tl, hl,

                     r = r RS prettify("dictionary-word", ins(1, (article ? "(" article ") " : "") word, tl))
                     if (isRTL(il))
-                        r = r RS prettify("dictionary-explanation-item", ins(2, explanation, il))
+                        r = r " - " prettify("dictionary-explanation-item", ins(0, explanation, il))
                     else
-                        r = r RS ins(2, explanation)
+                        r = r " - " ins(0, explanation)
                 }
             }
         }

I tried to put this functionality into a command-line option to avoid disturbing users who are accustomed to the current broad output. I'm pretty sure I didn't do it 100% right, so I'll be happy to correct the patch of this MR to match the requirements of the project :)