#!/bin/bash

# *****************************************************
# PROGRAM:  gxlat
# PURPOSE:  desktop gui for google translate (trans)
# AUTHOR:   vovchik (Puppy Linux forum)
# DEPENDS:  bash, gtkdialog4, trans, wget, gxmessage
# PLATFORM: Puppy Linux (actually, any *nix with GTK)
# DATE:     2010, updated: 19-06-2018  BK: version 0.4
# NOTE:     Added combobox for xlat engines and sound
# NOTE:     GUI adjusted: 25-03-2019 radky
# LICENCE:  GPL
# *****************************************************


# *********************
# INITIALIZATION
# *********************

icon="<svg width='128' height='128' viewBox='0 0 80 80'>
<linearGradient id='l1' gradientUnits='objectBoundingBox' 
 x1='1' x2='1' y1='0' y2='1'>
 <stop stop-color='blue' offset='1'/>
 <stop stop-color='#405DAA' offset='0'/>
</linearGradient>
 <g transform='translate(7 0)' stroke='none'>
 <text x='0' y='73' stroke='skyblue' stroke-width='5' 
  font-family='georgia' font-size='90px' font-weight='800'>
  <tspan x='000' fill='url(#l1)'>G</tspan>
 </text>
 </g>
</svg>"
# engines list
engines="<item>google</item>
<item>bing</item>
<item>yandex</item>
<item>deepl</item>
<item>apertium</item>
<item>aspell</item>
<item>spell</item>
<item>hunspell</item>"
# lang list
langs="<item>English [en]</item>
<item>Afrikaans [af]</item>
<item>Albanian [sq]</item>
<item>Arabic [ar]</item>
<item>Belarusian [be]</item>
<item>Bulgarian [bg]</item>
<item>Catalan [ca]</item>
<item>Chinese Simplfied [zh-CN]</item>
<item>Chinese Traditional [zh-TW]</item>
<item>Croatian [hr]</item>
<item>Czech [cs]</item>
<item>Danish [da]</item>
<item>Dutch [nl]</item>
<item>Estonian [et]</item>
<item>Filipino [tl]</item>
<item>Finnish [fi]</item>
<item>French [fr]</item>
<item>Galician [gl]</item>
<item>German [de]</item>
<item>Greek [el]</item>
<item>Hebrew [iw]</item>
<item>Hindi [hi]</item>
<item>Hungarian [hu]</item>
<item>Icelandic [is]</item>
<item>Indonesian [id]</item>
<item>Irish [ga]</item>
<item>Italian [it]</item>
<item>Japanese [ja]</item>
<item>Korean [ko]</item>
<item>Latin [la]</item>
<item>Latvian [lv]</item>
<item>Lithuanian [lt]</item>
<item>Macedonian [mk]</item>
<item>Malay [ms]</item>
<item>Maltese [mt]</item>
<item>Norwegian [no]</item>
<item>Persian [fa]</item>
<item>Polish [pl]</item>
<item>Portuguese [pt]</item>
<item>Portuguese Portugal [pt-PT] </item>
<item>Romanian [ro]</item>
<item>Russian [ru]</item>
<item>Serbian [sr]</item>
<item>Slovak [sk]</item>
<item>Sovenian [sl]</item>
<item>Spanish [es]</item>
<item>Swedish [sv]</item>
<item>Thai [th]</item>
<item>Turkish [tr]</item>
<item>Ukrainian [uk]</item>
<item>Vietnamese [vi]</item>
<item>Welsh [cy]</item>
<item>Yiddish [yi]</item>"
# readme text
readme="This program is a gtkdialog script wrapper for 'trans' and allows you to 
access google translate without a browser. It contains a function 'translate', 
which which consists of the following very useful bit of code:

function translate()
{
	TO_TEXT=\$(trans -brief -sl  '\$myFROM' -tl '\$myTO' '\$FROM_TEXT')
}

where:

	\$FROM_TEXT is the text to be translated
	\$myFROM is the source language
	\$myTO is the target language
	
Ergo, 'trans', when used in a script with proper arguments,
will fetch a translation from Google or another service (i.e. aspell, 
google, deepl, bing, spell, hunspell, apertium or yandex). If you have
mplayer, mpv, espeak or mpg123 installed, you can also hear the translated
text, provided the serivce has this option, by enabling 'Sound' via the
checkbox.

The CLI 'trans' program is available from:
https://www.soimort.org/translate-shell/

Gxlat also makes use of wget to perform a fast check on
internet connectivity, since 'trans' requires access to the
Google translate web site.

Gxlat will continue work as long as 'trans' works. If Google
change its API, as they have have done in the past, the 
translation function will bomb and will have to be modified
accordingly in 'the light of prevailing circumstances'.

Fortunately, it works at the moment.

With kind regards,
vovchik

May 2018

PS. The gtkdialog code could be improved, I know, by
refreshing memvars, thereby obviating the need for a temporary
file and directories. Anybody interested?

PPS.

LANGUAGES (some don't work [that well] yet):

Afrikaans [af]
Albanian [sq]
Amharic [am]
Arabic [ar]
Armenian [hy]
Azerbaijani [az]
Basque [eu]
Belarusian [be]
Bengali [bn]
Bihari [bh]
Breton [br]
Bulgarian [bg]
Burmese [my]
Catalan [ca]
Cherokee [chr]
Chinese [zh]
Chinese_simplifieD [zh-CN]
Chinese_traditionAL [zh-TW]
Corsican [co]
Croatian [hr]
Czech [cs]
Danish [da]
Dhivehi [dv]
Dutch  [nl] 
English [en]
Esperanto [eo]
Estonian [et]
Faroese [fo]
Filipino [tl]
Finnish [fi]
French [fr]
Frisian [fy]
Galician [gl]
Georgian [ka]
German [de]
Greek [el]
Gujarati [gu]
Haitian_creole [ht]
Hebrew [iw]
Hindi [hi]
Hungarian [hu]
Icelandic [is]
Indonesian [id]
Inuktitut [iu]
Irish [ga]
Italian [it]
Japanese [ja]
Javanese [jw]
Kannada [kn]
Kazakh [kk]
Khmer [km]
Korean [ko]
Kurdish  [ku]
Kyrgyz  [ky]
Lao [lo]
Latin [la]
Latvian [lv]
Lithuanian [lt]
Luxembourgish [lb]
Macedonian [mk]
Malay [ms]
Malayalam [ml]
Maltese [mt]
Maori [mi]
Marathi [mr]
Mongolian [mn]
Nepali [ne]
Norwegian [no]
Occitan [oc]
Oriya [or]
Pashto [ps]
Persian [fa]
Polish [pl]
Portuguese [pt]
Portuguese_portugal [pt-PT]
Punjabi [pa]
Quechua [qu]
Romanian [ro]
Russian [ru]
Sanskrit [sa]
Scots_gaelic [gd]
Serbian [sr]
Sindhi [sd]
Sinhalese [si]
Slovak [sk]
Slovenian [sl]
Spanish [es]
Sundanese [su]
Swahili [sw]
Swedish [sv]
Syriac [syr]
Tajik [tg]
Tamil [ta]
Tatar [tt]
Telugu [te]
Thai [th]
Tibetan [bo]
Tonga [to]
Turkish [tr]
Ukrainian [uk]
Urdu [ur]
Uzbek [uz]
Uighur [ug]
Vietnamese [vi]
Welsh [cy]
Yiddish [yi]
Yoruba [yo]"

# *********************
# END INITIALIZATION
# *********************


# *********************
# FUNCTIONS
# *********************


# -------------
function init_vars()
# -------------
{
	#to_text.txt
	mkdir /tmp/gxlat
	mkdir /tmp/gxlat/tmp
	mkdir /tmp/gxlat/dat
	mkdir /tmp/gxlat/img
	mkdir /tmp/gxlat/doc
	VERSION="0.4"
	tmp_base="/tmp/gxlat"
	file_base="/tmp/gxlat"
	export file_base
	export tmp_base
	export readme
	file_last_engine="$tmp_base/tmp/last_engine.txt"
	if [ -e $file_last_engine ]; then
		engines="<item>"`cat "$file_last_engine"`"</item>""$engines"
	fi
	export engines
	echo "$icon" > "$tmp_base/img/icon.svg"
	echo "$langs" > "$tmp_base/dat/lang-list.txt"
	echo "$readme" > "$tmp_base/doc/readme.txt"
	file_lang="$tmp_base/dat/lang-list.txt"
	file_last_to="$tmp_base/dat/last_to.txt"
	file_last_from="$tmp_base/dat/last_from.txt"
	mylist=$(cat "$file_lang")
	mylast_to="$mylist"
	mylast_from="$mylist"
	if [ -e $file_last_to ]; then
		mylast_to="<item>"`cat "$file_last_to"`"</item>""$mylist"
	else
		mylist=$(cat "$file_lang")
	fi
	if [ -e $file_last_from ]; then
		mylast_from="<item>"`cat "$file_last_from"`"</item>""$mylist"
	else
		mylist=$(cat "$file_lang")
	fi
	echo " " > "$tmp_base"/tmp/to_text.txt
}

# -------------
function swap_langs()
# -------------
{
	tmp1="$TO"
	TO="$FROM"
	FROM="$tmp1"
	echo "$TO" > "$tmp_base"/dat/last_to.txt
	echo "$FROM" > "$tmp_base"/dat/last_from.txt
}

# --------------
function check_inet()
# --------------
{
	wget -q --spider http://google.com
	inet_check="$?"
	echo "inet: ""$inet_check"
	if [ "$inet_check" -eq 0 ]; then
		echo "Online"
	else
		  gxmessage -name "Warning!" -fn "DejaVu Sans Bold" -timeout 3 -center -bg red -fg white "  
    Your internet connection is down or google.com is not responding!"
		exit 1

		echo "Offline"
	fi
}

# --------------
function strip_lang_name()
# --------------
{
	long_from="$FROM"
	long_to="$TO"
	export long_from
	export long_to
	strip_start="${FROM#*[}"
	strip_end="${strip_start%]*}"
	myFROM=$strip_end
	strip_start="${TO#*[}"
	strip_end="${strip_start%]*}"
	myTO=$strip_end
}

# -------------
function translate()
# -------------
{	
	strip_lang_name
	if [ $myTO == $myFROM ] ; then
		echo "Bad language choice..."
		echo "Bad language choice. 'From' and 'To' can't be the same." > "$tmp_base"/tmp/to_text.txt
	else
		echo "From: $FROM_TEXT"
		echo "Engine: $ENGINE"
		if [ $CHECKBOX == "true" ] ; then
			MYSOUND="-p"
			TO_TEXT=$(trans -e "$ENGINE" "$MYSOUND" -brief -sl "$myFROM" -tl "$myTO" "$FROM_TEXT")
		else
			TO_TEXT=$(trans -e "$ENGINE" -brief -sl "$myFROM" -tl "$myTO" "$FROM_TEXT")
		fi
		echo "Sound: $CHECKBOX $MYSOUND"
		#TO_TEXT=$(trans -e "$ENGINE" "$MYSOUND" -brief -sl "$myFROM" -tl "$myTO" "$FROM_TEXT")
		echo -n "To:   $TO_TEXT"
		echo -n "$TO_TEXT" > "$tmp_base"/tmp/to_text.txt
		export TO_TEXT
	fi
}
#$tmp_base/img/icon.svg

# --------------
function make_gui()
# --------------
{
export MAIN='
	<window window_position="1" title="Gxlat - Google Translate" image-name="'"$tmp_base"'/img/icon.svg">
		<vbox>
			<frame Source text: >
				<edit editable="true"  wrap-mode="2">
					<variable>FROM_TEXT</variable>
					<default>Sample text</default>
					<height>160</height>
					<width>560</width>
				</edit>
			</frame>
			<frame Languages: >
			<vbox>
				<hbox>
					<text width-chars="5">
						<label>From:</label>
					</text>
					<combobox allow-empty="true" case-sensitive="true" value-in-list="true" tooltip-text="Source language">
						<variable>FROM</variable>
							'$mylast_from'
					</combobox>
					<text width-chars="5">
						<label>To:</label>
					</text>
					<combobox allow-empty="true" case-sensitive="true" value-in-list="true" tooltip-text="Target language">
						<variable>TO</variable>
							'$mylast_to'
					</combobox>
					<text width-chars="3">
						<label>"  "</label>
					</text>
					<button tooltip-text="Clear From and To text boxes">
						<label>"Clear"</label>
						<input file stock="gtk-clear"></input>
						<action>clear:FROM_TEXT</action>
						<action>clear:TO_TEXT</action>
					</button>
					<button tooltip-text="Swap languages">
						<label>"Swap"</label>
						<input file stock="gtk-refresh"></input>
						<action>swap_langs</action>
						<action>echo $ENGINE > '$tmp_base'/tmp/last_engine.txt</action>
						<action>gxlat &</action>
						<action type="exit">EXIT_NOW</action>
					</button>
					<button tooltip-text="Translate text">
						<label>"Translate"</label>
						<input file stock="gtk-network"></input>
						<action>translate $FROM_TEXT $FROM $TO</action>
						<action>refresh:TO_TEXT</action>
						<action type="refresh">TO_TEXT</action>
					</button>
				</hbox>
			</vbox>
			</frame>
			<frame Translated text: >
				<edit editable="false" wrap-mode="2">
					<variable>TO_TEXT</variable>
					<input file>'$tmp_base'/tmp/to_text.txt</input> 
					<action>refresh:TO_TEXT</action>
					<height>160</height>
					<width>560</width>
				</edit>
			</frame>
			<hbox>
			<text width-chars="19">
				<label>Translation engine:</label>
			</text>
			<combobox width-request="100" allow-empty="true" case-sensitive="true" value-in-list="true" tooltip-text="Select a translation engine">
				<variable>ENGINE</variable>
					'$engines'
			</combobox>
			<checkbox tooltip-text="Enable/disable sound">
				<label>Sound</label>
				<variable>CHECKBOX</variable>
				<action>echo $CHECKBOX</action>
				<action>if true enable:ENTRY</action>
				<action>if false disable:ENTRY</action>
			</checkbox>
				<button width-request="90" tooltip-text="Show help">
					<label>"Help"</label>
					<input file stock="gtk-help"></input>
					<action type="launch">show_xlat_help</action>
				</button>
				<button width-request="90" tooltip-text="Exit program">
					<label>Close</label>
					<input file stock="gtk-cancel"></input>
					<action>echo $FROM > '$tmp_base'/dat/last_from.txt</action>
					<action>echo $TO > '$tmp_base'/dat/last_to.txt</action>
					<action>echo $ENGINE > '$tmp_base'/tmp/last_engine.txt</action>
					<action type="exit">EXIT_NOW</action>
				</button>
			</hbox>
		</vbox>
		<action signal="hide">exit:Exit</action> 
	</window>'
}

# -------------
function make_help_gui()
# -------------
{
	# this is the help text
	myxlathelp="
<b>Gxlat</b> is intended to enable access to the Google translate facility from the desktop without the need to load a javascript-enabled browser. You will need a live Internet connection.

1. Select a text to translate (<i>typed in or pasted</i>).
2. Select the <b>From</b> language via the leftmost combo box.
3. Select the target language via the <b>To</b> combo box.
4. Click on the <b>Translate</b> button.
5. The Google translation will appear in the bottom window.
6. Click the <b>Close</b> button to exit the application.

Vovchik, Puppy Linux Forum, June 2010 (updated May 2018)
<i>       www.murga-linux.com/puppy</i>"
	
	show_xlat_help='
	<window title="Gxlat Help" skip-taskbar-hint="true">
		<vbox>
			<frame Help text>
				<text use-markup="true" wrap="true">
					<label>"'$myxlathelp'"</label>
				</text>
			</frame>
			<hbox>
				<button width-request="90" tooltip-text="Show README">
					<input file icon="gtk-info"></input>
					<label>ReadMe </label>
					<action>launch:show_readme</action>
				</button>
				<button width-request="90" tooltip-text="Close window">
					<input file stock="gtk-cancel"></input>
					<label>Close </label>
					<variable>show_xlat_help</variable>
					<action>closewindow:show_xlat_help</action>
				</button>
			</hbox>
		</vbox>
	</window>
	'
	export show_xlat_help
}

# -------------
make_readme_gui()
# -------------
{
show_readme='
	<window title="Gxlat Readme" skip-taskbar-hint="true">
		<vbox>
			<frame Readme text>
				<edit editable="false">
					<input file>'$file_base/doc/readme.txt'</input>
					<width>600</width><height>350</height>
				</edit>
			</frame>
			<hbox>
				<button width-request="90" tooltip-text="Close window">
					<input file stock="gtk-cancel"></input>
					<label>Close </label>
					<variable>show_readme</variable>
					<action>closewindow:show_readme</action>
				</button>
			</hbox>
		</vbox>
	</window>'

	export show_readme
}

# *********************
# END FUNCTIONS
# *********************


# *********************
# MAIN
# *********************

init_vars
check_inet
make_help_gui
make_readme_gui
make_gui
export -f swap_langs
export -f strip_lang_name
export -f translate
gtkdialog4 -p MAIN

# *********************
# END MAIN
# *********************
