LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-04-2021, 06:31 PM   #1
Alexcostariha
LQ Newbie
 
Registered: Jul 2021
Posts: 8

Rep: Reputation: Disabled
translate from any language with zenity window


I ruled a translator with zenity and this is what I ended up with:
Code:
#!/usr/bin/env bash

xsel -o | python3 -c "import sys,urllib.request,json; r = json.loads(urllib.request.urlopen(urllib.request.Request('http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=ru&dt=t&q=' + urllib.parse.quote(sys.stdin.read().replace('\n',' ')), None, {'User-Agent' : 'Mozilla/5.0' })).read().decode('utf-8')); [print(t[0], end='') for t in r[0]];" | zenity --text-info --font='STIX Bold Italic 16'  --width=600 --height=250 --window-icon=$HOME/bin/translate-icon.png  --title="(*_*)<< TRANSLATION >>(^_^)/"  2> >(grep -v 'GtkDialog'>&2)
I explain:
1) This is a script in ~/bin.
2) --font='STIX Bold Italic 16' # you can choose any font and any size of the text (but not the title) of the window
3) --window-icon=/home/pictures/someimage.jpg # image for window if you want
4) --width=600 --height=250 # window size
5) --title="TRANSLATE" # title of the window(how to change fonts and font size for it I could not find).
6) 2> >(grep -v 'GtkDialog'>&2) # suppress error message "Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged".
7) this is only window with translation without source text
8) auto&tl=ru&dt # change "ru" for your language.
9) Place the file translate-icon.png in your folder "~/bin".

Thanks for:
Tim Cooper
Dave Rove
Andrey Chizhegov
Attached Thumbnails
Click image for larger version

Name:	translate-icon.png
Views:	1
Size:	67.3 KB
ID:	36755  

Last edited by Alexcostariha; 07-08-2021 at 03:01 PM.
 
Old 07-05-2021, 05:19 PM   #2
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,620
Blog Entries: 40

Rep: Reputation: Disabled
I did something similar, first with a simple script, then with a rudimentary GUI, written for Yad.

One version of the script, which consults Merriam-Webster, is in my blog entry “Word-definitions on the command-line”.

It is very simple to do the same or to adapt the procedure for other online-dictionaries and purposes, like a search for synonyms, conjugations etc...

See also “Web-Scraping”.

Last edited by Michael Uplawski; 07-05-2021 at 05:21 PM.
 
Old 07-06-2021, 02:58 PM   #3
Alexcostariha
LQ Newbie
 
Registered: Jul 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
Michael Uplawski

This script is not my credit. I have looked at a lot of posts on the internet on this topic. The task was to make the conclusion simple - no source text. In addition, in this script, the entire text is translated, and not up to the first point, as usual.
My merit - I found how to choose a font and its size for zenity. The regular font is very small and hard to read for me.
I could not find it anywhere on the Internet. Usually they suggest using the "span" function, which I did not understand. Only the Gnome website has information that the font can be changed for "--text-info" function of ZENITY(in fact, they write about the "dialog" command, and zenity is written in its base). The syntax for the fonts was chosen at random. It turned out that all font data is written with a space, no commas.
I didn't use the "yad" command. Perhaps "yad" is better. However, the translation articles talked about ZENITY, so I used it even though the project not developing since 2013.
Thanks for the tip “Web-Scraping”, I'll keep it in mind.

Last edited by Alexcostariha; 07-07-2021 at 01:14 PM.
 
Old 07-07-2021, 03:15 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
I'm getting this error:
Code:
$> testy
  File "<string>", line 1
    import sys,urllib.request,json; r = json.loads(urllib.request.urlopen(urllib.request.Request('http://translate.googleapis.com/translate_a/single?client=gtx&
                                                                                                                                                                 ^
SyntaxError: EOL while scanning string literal
???

An empty zenity window pops up.

I tried escaping all ampersands, but the error is the same.

bash 5.1.8, Python 3.9.5
 
Old 07-07-2021, 07:07 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545

That error is because what's intended as a single line is broken across three lines, and the first break is inside a string. (Hence the End Of Line inside string error.)

(Afaik, LQ doesn't split long lines inside code blocks, so it seems Alexcostariha manually broke it, and perhaps needs to increase their knowledge of Bash/Python syntax.)


Though I'm not sure why it's such a convoluted script in the first place. It can be simplified to:
Code:
#!/bin/bash

from="$1"
to="$2"
phrase="$3"
firefox "https://www.deepl.com/translator#${from}/${to}/${phrase//$'\n'/ }"
Executed as "./translate.sh auto ru 'whatever you want'"


Or just slightly more complex (and flexible) would be...
Code:
#!/bin/bash

[[ ${#@} < 1 ]] && to="ru" || to="$1"
[[ ${#@} < 2 ]] && from="auto" || from="$2"
[[ ${#@} < 3 ]] && phrase="$(</dev/stdin)" || phrase="$3"
phrase=${phrase//$'\n'/ }
firefox "https://www.deepl.com/translator#${from}/${to}/${phrase//$'\n'/ }"
Which also allows "xsel -o | ./translate2.sh" or "cat kartoffel.txt | ./translate.sh de en" or whatever.


In both cases, the firefox command could be replaced with a lightweight Python webview/whatever with a customised interface, (if there's actually a reason to do that).


Last edited by boughtonp; 07-07-2021 at 07:09 AM.
 
Old 07-07-2021, 01:49 PM   #6
Alexcostariha
LQ Newbie
 
Registered: Jul 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
ondoho

Read this basic article about this theme, please. Maybe you must install "libnotify-bin" or "xclip". Then you must assign a keyboard shortcut to your script. I use this script on UBUNTU 14 and the latest LinuxMint 20.1 Cinnamon and it works great everywhere. My script is named "lingvatrans_zenity" (I have five translate scripts in total). Му shortcut for it is Alt+Shift+6. If you have LinuxMint or Ubuntu with Cinnamon - you can customize hot corners. Then you just touch for example the lower right corner with the mouse and receive your translate window.
To test the script, select some text you want to translate and then run the "./lingvatrans_zenity" command in the script folder(without using keyboard shortcut).

Last edited by Alexcostariha; 07-07-2021 at 03:20 PM.
 
Old 07-07-2021, 01:50 PM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
I can't tell what you are trying to do.
The python is a mess. Is this it, or part of it?

script.py
Code:
#!/usr/bin/python

from urllib import request
import json

agent = ('Mozilla/5.0 (Windows NT 10.0; x86_64; rv:88.0) '
            'Gecko/20100101 Firefox/88.0')
            
uagent = {'User-Agent': agent,
        'Accept': 'text/html,application/xhtml+xml,'
        'application/xml;q=0.9,*/*;q=0.8',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
        'Accept-Encoding': 'none',
        'Accept-Language': 'en-US,en;q=0.8',
        'Connection': 'keep-alive'}
            
u1 = ('http://translate.googleapis.com/translate_a/single?'
        'client=gtx&sl=auto&tl=ru&dt=t&q=')

u2 = input('Input something: ')

url = u1+u2

req = request.Request(url, data=None, headers=uagent)
get = request.urlopen(req)
html = get.read().decode('utf-8', 'ignore')

r = json.loads(html)

print(r)
Code:
python script.py
Input something: I+like+apples+,+they+are+good
[[['Я люблю яблоки они хорошие', 'I like apples , they are good', None, None, 3, None, None, [[]], [[['cce7c67b3f2439089dd6b428e0b83b88', 'en_ru_2020q2.md']]]]], None, 'en', None, None, None, 1, [], [['en'], None, [1], ['en']]]

Last edited by teckk; 07-07-2021 at 01:57 PM.
 
Old 07-07-2021, 01:57 PM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Code:
python script.py
Input something: I+ruled+a+translator+with+zenity+and+this+is+what+I+ended+up+with
[['Я с энтузиазмом правил переводчиком и вот что у меня получилось', 'I ruled a translator with zenity and this is what I ended up with', None, None, 3, None, None, [[]], [[['cce7c67b3f2439089dd6b428e0b83b88', 'en_ru_2020q2.md']]]]]
 
Old 07-07-2021, 03:36 PM   #9
Alexcostariha
LQ Newbie
 
Registered: Jul 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
teckk

Look at this video, please.

Last edited by Alexcostariha; 07-08-2021 at 08:21 AM.
 
Old 07-08-2021, 09:04 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by boughtonp View Post
That error is because what's intended as a single line is broken across three lines, and the first break is inside a string. (Hence the End Of Line inside string error.)

(Afaik, LQ doesn't split long lines inside code blocks, so it seems Alexcostariha manually broke it, and perhaps needs to increase their knowledge of Bash/Python syntax.)
oof, that was it.
Thanks, sorry, I should've seen that. I guess it was late.

TBF, I like these small yad scripts, and plan to assign something like it to a hotkey indeed.
The one I'm currently using is way too complex.
Quote:
Originally Posted by Alexcostariha View Post
Read this basic article about this theme, please. Maybe you must install "libnotify-bin" or "xclip".
Sorry but this is nonsense.
Please read what boughtonp wrote.

I would like to say "good effort for your first script", but somehow this feels like you copy-pasted most of it without understanding what it does.

Anyhow, thanks for sharing.
 
Old 07-08-2021, 09:20 AM   #11
Alexcostariha
LQ Newbie
 
Registered: Jul 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
ondoho

You are right! I made correction for my script, please test. There were two extra line feeds.
Thank You and boughtonp for comments.
Indeed, this is not my script. It is changed script from Andrey Chizhegov(as comment to basic article). You can compare it and mine:
Code:
#!/usr/bin/env bash

xsel -o | python3 -c "import sys,urllib.request,json; r = json.loads(urllib.request.urlopen(urllib.request.Request('http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=ru&dt=t&q=' + urllib.parse.quote(sys.stdin.read().replace('\n',' ')), None, { 'User-Agent' : 'Mozilla/5.0' })).read().decode('utf-8')); [print(t[0], end='') for t in r[0]]; print('\n--==<<>>==--'); [print(t[1], end='') for t in r[0]];" | zenity --text-info --width=500 --height=500 --title="Translation"

Last edited by Alexcostariha; 07-09-2021 at 08:52 AM.
 
Old 07-08-2021, 02:46 PM   #12
Alexcostariha
LQ Newbie
 
Registered: Jul 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
Additionally

What a translate one can receive from basic article's script(see image 01): translate only up to first point, little print, md5sum(what for?) and line breaks.
What a translate can one receive from "my" script(see image 2): full translate of selected text, large print, beautiful appearance and nothing more.
Attached Thumbnails
Click image for larger version

Name:	trlq-01.png
Views:	16
Size:	122.3 KB
ID:	36760   Click image for larger version

Name:	trlq-02.png
Views:	12
Size:	50.0 KB
ID:	36761  

Last edited by Alexcostariha; 07-09-2021 at 08:54 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Crow Translate: Desktop / CLI Text Translation App Using Google Translate, Yandex Translate and Bing Translator LXer Syndicated Linux News 0 05-11-2019 05:13 AM
LXer: Translate Shell – A Tool To Use Google Translate From Command Line In Linux LXer Syndicated Linux News 0 11-30-2017 07:05 PM
how to get zenity code if user close zenity window pedropt Programming 4 03-03-2017 12:13 PM
BASH/No X: Using google translate to convert TXT files (translate) frenchn00b Programming 10 09-13-2009 10:55 PM
to translate or not to translate HTML rblampain General 2 07-05-2007 09:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration