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.91k stars 389 forks source link

LC_ALL; (awk setlocale?); normal POSIX shell? #521

Open sdaoden opened 2 weeks ago

sdaoden commented 2 weeks ago

Hello! I just got pointed to this program by noone else but Nelson H. F. Beebe (after noting that dict.cc is so slow to access with the lynx text mode browser on the lynx list), who says he uses this for many purposes for many years, and so i tried it, and it is a really great tool to have. Thank you! (I have created a CRUX Linux package now, but i am no longer in "official" contrib, it is only in my private user repository .. that some people seem to access; anyhow).

So you can very well ignore the rest and close this "issue", i only stumbled upon it and thought i share that.

Iooked into the script, and i saw it looks for $LANG. Whereas that is ok, since many in the Linux world (GNU etc) document users should set that, $LANG is only a secondary switch, and i for example do not set it, but LC_ALL (this is also much much much faster). (C.UTF-8 is coming along, too, but checking for it would make installation more lengthy.)

It also seems bash is not really needed. Ie the following snippet should work just fine in any POSIX shell, and since the /usr/bin/trans thing is a cumulation generated via script the (possibly) necessary quoting in the manual page could very easily be done. This is just a quick hack, of course.

Ciao! And thanks for this tool!

y=en_US.UTF-8
if [ -n "$LC_ALL" ]; then x=$LC_ALL
elif [ -n "$LANG" ]; then x=$LANG
else x=
fi
if [ -n "$x" ] && [ "$x" != "${x%UTF*}" ]; then
        x=${x#*UTF}
        x=${x#*-}
        [ "$x" != 8 ] && x=$y || x=
else
        x=$y
fi
if [ -n "$x" ]; then
        LC_ALL=$x
        export LC_ALL
fi

TRANS_MANPAGE='
.TH "TRANS" "1" "2023\-02\-08" "0.9.7.1" ""
.hy
.SH NAME
.PP
trans \- yeah
.SH SYNOPSIS
dudia
'
export TRANS_MANPAGE

cat <<'EOT' | gawk -f - "$@"
function initPager() { Pager = "less" }
function showMan(    temp) {
PIPE=" | "
if (ENVIRON["TRANS_MANPAGE"]) {
initPager()
Groff = "/usr/bin/groff"
if (Pager && Groff) {
temp = "printf '%s' \"${TRANS_MANPAGE}\""
temp = temp PIPE\
Groff " -Wall -mtty-char -mandoc -Tutf8 "
temp = temp PIPE\
Pager " -s -P\"\\ \\Manual page " Command "(1) line %lt (press h for help or q to quit)\""
}
system(temp)
}
}
BEGIN{showMan()}
EOT
sdaoden commented 2 weeks ago

(And have you ever thought on asking Arnold Robbins of gawk whether he would be interested to support switching the locale? If he does not do that already. He is a very friendly and helpful person. Btw on some boxes there is "mandoc" as a replacement of groff, just say "mandoc -Tutf8" and it will do the rest.) But now really Ciao!

sdaoden commented 2 weeks ago

(Ah please wait, just one tiny addition: one can in fact "exec gawk" to pass execution over.)

sdaoden commented 2 weeks ago

(Oh, and one very very last final thing, even the cat is not needed, one can simply say

 ( <<'EOT' exec gawk -f -
> BEGIN{print "hi"}
> EOT
)
hi

so even that process is not necessary. translate-shell is cool :) Ciao!!)