Development of 'trans_tray' (translation program)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

Development of 'trans_tray' (translation program)

#1 Post by fredx181 »

Development continuation of "trans_tray", which started in the "Yad - Tips" thread, initially here:
http://murga-linux.com/puppy/viewtopic. ... 87#1034587
Until here (at this time): http://murga-linux.com/puppy/viewtopic. ... 29#1035129

Started by Argolance who gave a hint about a script using 'xclip' and "trans" piped to yad, followed by contributions from vovchik (nice start concept), josejp2424 , stemsee and myself.

Further contributions and feedback are very welcome !

Fred
Last edited by fredx181 on Fri 23 Aug 2019, 19:23, edited 1 time in total.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#2 Post by fredx181 »

Last edited by fredx181 on Sat 14 Sep 2019, 18:36, edited 5 times in total.

stemsee

#3 Post by stemsee »

I added this to save all translation window texts in root

Code: Select all

for ex in $ENGINE
do
echo "icon:/usr/share/pixmaps/$ex.png" >$PIPE
for i in $TLANG
do
echo ---------------$ex $i-------------------
xclip -o | trans -e $ex -b -tl $i
done; done | yad \
--window-icon="$ICON" \
--text-align=center \
--title="$MYTITLE" \
--geometry=600x600+$SWIDTH+100 \
--fore="black" --back="#fef1e0" \
--margins=5 \
--button="$BUT" \
--text="$HELP" \
--fontname="$FONT" \
--text-info --wrap --tail --editable >$HOME/Trans-Text-$RANDOM
but this creates a file at function start and dumps data on exit, even if it's empty. It would be better to implement a save button. But I couldn't get it to work.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#4 Post by musher0 »

Remember to use DeepL for most Western /
Latin languages, plus Polish and Russian, rather than Google Translate.

DeepL does a better job: its translations sound more natural in the target language.
(And it's not just me saying so.)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
josejp2424
Posts: 556
Joined: Sun 01 Aug 2010, 22:35
Contact:

button exit

#5 Post by josejp2424 »

you should remove the middle mouse button click on the system tray icon.
since we have exit button in the menu.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#6 Post by Argolance »

Bonjour,

@musher0
musher0 wrote:Remember to use DeepL for most Western/Latin languages, plus Polish and Russian, rather than Google Translate.
As I pointed out on my :arrow: 2Pot (2P Online Translator) thread, for me, DeepL has not worked at all for a long time. I think I probably overused the engine while I was working on my scripts and I'm now probably on a blacklist, which can happen to any user. Google, Bing, Yandex do the same thing but only temporarily and not permanently like DeepL, which strategy is unfortunate: pettiness has only a time (it seems that Google has improved significantly since the creation of DeepL).

@josejp2424
:wink:

Cordialement.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#7 Post by fredx181 »

stemsee wrote:I added this to save all translation window texts in root

Code: Select all

for ex in $ENGINE
do
echo "icon:/usr/share/pixmaps/$ex.png" >$PIPE
for i in $TLANG
do
echo ---------------$ex $i-------------------
xclip -o | trans -e $ex -b -tl $i
done; done | yad \
--window-icon="$ICON" \
--text-align=center \
--title="$MYTITLE" \
--geometry=600x600+$SWIDTH+100 \
--fore="black" --back="#fef1e0" \
--margins=5 \
--button="$BUT" \
--text="$HELP" \
--fontname="$FONT" \
--text-info --wrap --tail --editable >$HOME/Trans-Text-$RANDOM
but this creates a file at function start and dumps data on exit, even if it's empty. It would be better to implement a save button. But I couldn't get it to work.
Works for me this way with Save button, trans_paste function:

Code: Select all

trans_paste () {
#set -x
export TLANG=$(cat $HOME/.trans_target_lang2 2> /dev/null)
export _TLANG=$(cat $HOME/.trans_target_lang2 2> /dev/null | awk '{print $1}' | tr '\n' '+' | sed 's/.$//')
[ -z "$TLANG" ] && export TLANG="English - en"
export ENGINE=$(cat /tmp/_trans_engine_2 2> /dev/null)
[ -z "$ENGINE" ] && ENGINE=google
echo "Using $ENGINE for translating"
ICON="/tmp/trans-paste.svg"
if [ ! -e "$ICON" ]; then
   SVG="<""svg height='100' width='100'>
     <path style='fill:red;stroke:darkred;stroke-width:3;'
     d='M 93,62 C 83,82 65,96 48,96 32,96 19,89 15,79 L 5,90 5,53
     40,53 29,63 c 0,0 5,14 26,14 16,0 38,-15 38,-15 z'/>
     <path style='fill:red;stroke:darkred;stroke-width:3;'
     d='M 5,38 C 11,18 32,4 49,4 65,4 78,11 85,21 L 95,10 95,47
     57,47 68,37 C 68,37 63,23 42,23 26,23 5,38 5,38 z'/>
   </svg>"
   echo "$SVG" > /tmp/trans-paste.svg
fi
HELP=" Translating to $_TLANG... \n Press <b>ESC</b> key or click <b>Exit</b> to dismiss, or click <b>Save</b> (if desired, edit text first)"
MYTITLE="Trans"
BUT="<span color='darkblue'><b>Exit</b></span>"
BUTSAVE="<span color='darkgreen'><b>Save</b></span>"
FONT="Mono 12"
TWIDTH=$(xdpyinfo  | grep 'dimensions:' | cut -d' ' -f7 | cut -d'x' -f1)
SWIDTH=$(( $TWIDTH - 600 ))
#echo $SWIDTH
GUI=$(for ex in $ENGINE
do
echo "icon:/tmp/$ex.svg" >$PIPE
while read i; do
echo " ----- $ex translate to $(echo $i | awk '{print $1}') -----"
xclip -o | trans -e $ex -b -tl $(echo $i | awk '{print $NF}')
echo
done <<< "$TLANG"; done | yad \
--window-icon="$ICON" \
--text-align=center \
--title="$MYTITLE" \
--geometry=600x600+$SWIDTH+100 \
--fore="black" --back="#fef1e0" \
--margins=5 \
--button="$BUTSAVE:0" \
--button="$BUT:1" \
--text="$HELP" \
--fontname="$FONT" \
--text-info  --wrap --tail --editable)
if [ $? -eq 0 ]; then
DATE=$(date +"%Y%m%d_%H%M%S")
echo "$GUI" >  $HOME/trans-$DATE
yad --title="Translation saved" --borders=6 --window-icon=$ICON --text="  <b>Saved to $HOME/trans-$DATE </b>" --geometry=350x40-40-70 --undecorated --timeout 5 --no-buttons
fi
[ "$ENGINE" = "google bing" ] && echo "icon:/tmp/all.svg" >$PIPE
}
export -f trans_paste
@josejp2424
you should remove the middle mouse button click on the system tray icon.
since we have exit button in the menu.
Yes, better, will do that.

Fred

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#8 Post by fredx181 »

Hi everyone. Attached new trans_tray2.

- Added 'Save' button to the window with translated text (possible to edit first)
- Disabled middle click to Exit the tray-icon (since there's Exit in the Menu, as josejp24 and Argolance already stated)

Fred
Attachments
trans_tray2.gz
Remove fake .gz and make executable
(110.09 KiB) Downloaded 153 times
2019-08-24-094204_470x196_scrot.png
(106.55 KiB) Downloaded 429 times
2019-08-24-093810_600x629_scrot_500x524.png
Added Save button
(131.08 KiB) Downloaded 435 times

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#9 Post by fredx181 »

Hi All, just discovered this brilliant code from stemsee (many thanks, also to misko for the help)
http://murga-linux.com/puppy/viewtopic. ... 50#1035150
modified a bit and added it to the right-click menu options (View saved translations).
Hope you don't mind Stemsee ? (you can always change how you prefer of course)

Fred
Attachments
trans_tray2.gz
New trans_tray2, remove fake .gz and make executable
(110.82 KiB) Downloaded 147 times
2019-08-24-110305_728x704_scrot_528x511.jpg
View saved translations
(82.7 KiB) Downloaded 403 times

stemsee

#10 Post by stemsee »

:D

Fred, your enthusiasm is contagious! You usually take the initiative and remain a step ahead of the pack!(a lot of the time anyway! :? )

Is a progress indicator during translation 'easily' implemented....from your googledrive and other apps; you have far more successful experience withit (taking in to account Mochi's observations and hints about progress indicators cutting out before reaching the end.)

EDIT:
Also just to clarify why I prefer the locale code $i (en es de) etc .... it is educational....lazyish learners will learn the international country codes.

Also if only Bing and Google re to be used then 'All' is inappropriate, change 'All' to 'Both' .... I'm keeping yandex so 'All' is ok. But later when you gettext could make a difference.

Also when previewing saved texts --editable for tabnum=2 might be useful, requiring additional save button, and also a refresh button to update list of texts, without closing and opening the viewer again. Or just a button to open selected in geany/leafpad etc.

As for adding DeepL, I'm all for it, but the 'trans' script is in python which I know nothing about, and that's where it needs to be added, unless someone learns DeepL API and implements basic functions in trans_tray2...(maybe you can musher).

stemsee
Last edited by stemsee on Sat 24 Aug 2019, 14:44, edited 3 times in total.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#11 Post by fredx181 »

stemsee wrote:Fred, your enthusiasm is contagious! You usually take the initiative and remain a step ahead of the pack!(a lot of the time anyway! Confused )
Yeah yeah, I will try to calm down now :lol: :lol:
Is a progress indicator during translation 'easily' implemented...
Not saying it's impossible, but to be honest I have no idea even where to start making progress bar for this... :roll:

Fred

User avatar
666philb
Posts: 3615
Joined: Sun 07 Feb 2010, 12:27
Location: wales ... by the sea

#12 Post by 666philb »

i'm liking this! thanks fredx181

some feature requests...

a button to clear the contents

it would also be good to remember window position and size, or at least always open at the bottom directly above the tray icon. remembering the position & size would be better though.

cheers

phil
Bionicpup64 built with bionic beaver packages http://murga-linux.com/puppy/viewtopic.php?t=114311
Xenialpup64, built with xenial xerus packages http://murga-linux.com/puppy/viewtopic.php?t=107331

stemsee

#13 Post by stemsee »

Hi 666philb

Are you talking about all opening windows or one specific one?

Anyway, in savedtranstext function notice the WINDOWTITLE variable, with the title in spacedless text, that makes it suitable for geometry recorder which has geometry recording code from Mochimoppel, in a function by myself.

Code: Select all

function sayonara {     #MochiMoppel/stemsee geometry recorder
export track=/tmp/trans_tray2
export camino=$HOME/.trans_tray2
[[ ! -f "$track"/geom ]] && exit
   yad --text="$WINDOWTITLE: WT Geometry-Recorder" --window-icon="$pics/wireless.png" \
   --geometry=200x10+100-100 --no-focus --no-buttons --undecorated \
   --on-top --form --skip-taskbar --field="MoreTime:FBTN" "bash -c sayonara" \
   --timeout-indicator=top --timeout=6
   nought=`xwininfo -stats -name "$WINDOWTITLE" | grep "$WINDOWTITLE"`
   XWININFO=`xwininfo -stats -name "$WINDOWTITLE"`
   if [[ ! -z "$nought" ]]; then
	   ARRAY=(${XWININFO#* X: })
	   AX=${ARRAY[0]}
	   AY=${ARRAY[4]}
	   RX=${ARRAY[8]}
	   RY=${ARRAY[12]}
	   W=${ARRAY[14]}
	   H=${ARRAY[16]}
	   X=$((AX-RX))
	   Y=$((AY-RY))
	   echo "$W"x"$H"+"$X"+"$Y" > $camino/geometry/$WINDOWTITLE
   else
		exit
   fi
}; export -f sayonara

function switchgr (){ #toggle geometry recorder on or off
[[ -f "$track"/geom ]] && rm -f "$track"/geom || touch "$track"/geom
}; export -f switchgr

#code misko_2083
function savedtranstexts (){
   rm -f /tmp/TP
   stxtp=/tmp/TP
   mkfifo -m 755 $stxtp
   exec 4<> $stxtp
yad --plug=$$ --tabnum=1 --list --item-separator=" " --column="Saved Trans" \
    $(for f in $HOME/trans-*_*; do [[ -f "${f}" ]] && echo "${f##*/}"; done) \
    --select-action="bash -c \"echo -e '\f' >/tmp/TP ; cd "$HOME" ; cat  "%s" >/tmp/TP ; echo -e '\n' >/tmp/TP \""  &
yad --plug=$$ --tabnum=2 --editable --text-info --wrap --listen <&4 &

export WINDOWTITLE="Saved-Texts"
yad --window-icon=gtk-save --geometry=$camino/geometry/$WINDOWTITLE --no-buttons --on-top --paned --splitter=180 --key=$$ --title="$WINDOWTITLE" --tab="Selector" --tab="Preview" --orient=horizontal
	4<&-
	5<&-
	kill -9 $MON_PID
}; export -f savedtranstexts
from my wifi app, edit accordingly...
Last edited by stemsee on Sat 24 Aug 2019, 15:21, edited 2 times in total.

User avatar
666philb
Posts: 3615
Joined: Sun 07 Feb 2010, 12:27
Location: wales ... by the sea

#14 Post by 666philb »

hi stemsee,

i just meant the main 'trans' window. i have a 4k screen and it's opening right up in the top right of the screen. i've edited the script now for me personally so that it opens bottom right.

thanks for posting the code snippet, although it's way above my pay grade :) .. hopefully it will make sense to fredx181

cheers
Bionicpup64 built with bionic beaver packages http://murga-linux.com/puppy/viewtopic.php?t=114311
Xenialpup64, built with xenial xerus packages http://murga-linux.com/puppy/viewtopic.php?t=107331

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#15 Post by fredx181 »

666philb wrote:i'm liking this! thanks fredx181
....
....
Thanks phil,
a button to clear the contents

I assume you mean the contents of translated text window, what is it good for to clear the contents ?
it would also be good to remember window position and size, or at least always open at the bottom directly above the tray icon. remembering the position & size would be better though.
Yes, that would be good, I think stemsee can make that work properly.

@stemsee
EDIT:
Also just to clarify why I prefer the locale code $i (en es de) etc .... it is educational....lazyish learners will learn the international country codes.

I disagree !! (if you don't mind :wink: ) this program is not for educational purpose.
Also if only Bing and Google re to be used then 'All' is inappropriate, change 'All' to 'Both' .... I'm keeping yandex so 'All' is ok. But later when you gettext could make a difference.

Also when previewing saved texts --editable for tabnum=2 might be useful, requiring additional save button, and also a refresh button to update list of texts, without closing and opening the viewer again. Or just a button to open selected in geany/leafpad etc.

As for adding DeepL, I'm all for it, but the 'trans' script is in python which I know nothing about, and that's where it needs to be added, unless someone learns DeepL API and implements basic functions in trans_tray2...(maybe you can musher).
Yes, 'Both" will be better then, and yes additional Save button might be nice for saved-text, DeepL unfortunately isn't supported by trans anymore, I've read good reviews about it. Trans script in Python ? Just bash AFAIK.

Fred

stemsee

#16 Post by stemsee »

fredx181 wrote:Trans script in Python ? Just bash AFAIK.
Fred
Which proves I really do know nothing about bash too .... :lol:

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#17 Post by fredx181 »

stemsee wrote:
fredx181 wrote:
Trans script in Python ? Just bash AFAIK.
Fred
Which proves I really do know nothing about bash too .... Laughing
Well... for me too the trans script doesn't look like understandable bash code... :roll:

Tried the audio option of trans (works for me only with mpv), it seems to me there's a limit of characters (or words) that can be used, this example works for me (Dutch+French+Spanish):

Code: Select all

echo "Better to reinforce that baffle with, for example, the 2x2 bars that you have, instead of what I said earlier, otherwise I think it will be very wobbly and vulnerable don't you think?" | trans -e google -b -tl nl+fr+es -p -player mpv
But when adding more words to the text it fails for me. (around 190 chars limit maybe ? pity !)

P.S. The voice is nice IMHO ! (same btw as playing audio from google translate in browser)

Fred

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#18 Post by fredx181 »

Hi All, new trans_tray2 with some improvements, changes.

- The position and size of the "Trans" window will now be remembered when changed (request from 666philb, thanks for suggestion, it's a nice feature !)
Used the code for that from here: http://murga-linux.com/puppy/viewtopic. ... 285#989285
Thanks to MochiMoppel and stemsee !
This needed a change for the info "Translating to ...." , moved now from header text to top of the text-info, otherwise the header text could become too long which makes the window not properly resizable, see issue here: http://murga-linux.com/puppy/viewtopic. ... 21#1035321

- All (edit: most) dialogs will show now in the bottom right corner, which is more convenient IMO.

EDIT 2019-08-26: Made a mistake by testing with yad v0.41, which allows using --geometry --width and --height together, earlier versions don't allow that.
Fixed now and re-attached new trans_tray2 that should work well with e.g. yad v0.38

Fred
Attachments
trans_tray2.gz
Updated 2019-08-26 Remove fake .gz and make executable
(111.72 KiB) Downloaded 149 times
2019-08-25-185336_1183x635_scrot_783x420.jpg
Enlarged 'Trans' window, size and position will be remembered
(100.4 KiB) Downloaded 562 times
Last edited by fredx181 on Mon 26 Aug 2019, 08:14, edited 2 times in total.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#19 Post by MochiMoppel »

The trans script.does't work in Slacko 5.6.
Plenty of syntax errors from gawk

Code: Select all

gawk: /dev/fd/63:2024: LocaleAlias[Locale[i]["iso"]] = i
gawk: /dev/fd/63:2024:                      ^ syntax error
gawk: /dev/fd/63:2024: fatal: invalid subscript expression
Using gawk 3.1.8

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#20 Post by fredx181 »

MochiMoppel wrote:Using gawk 3.1.8
Required is 4.0 or later https://github.com/soimort/translate-shell#dependencies

Fred

Post Reply