You are not logged in.

#1 2010-04-26 02:31:55

ShadowKyogre
Member
From: Hell! XP No... I'm not telling
Registered: 2008-12-19
Posts: 476
Website

Zenity frontends for Google Translate and Dictd

I present to you two zenity frontends for Google translate via the commandline and dictd. So why'd I write this if the google translate cli function works already and that kupfer does it fine? For one, I don't want to have to memorize the ISO blahblahblah codes just to translate to different languages, and neither should you. Secondly, it doesn't do multi-line that well. Thirdly, I don't want to have to open a web page to translate multi-line text.

For the dictd front end, you also shouldn't have to remember the database name to look up a word in a certain dictionary, other than its common name.

So here they are:
Google Translate frontend (named as translate.sh on my machine):

#!/bin/sh

#just comment/delete the window line if you don't have the icons
txt="$(zenity --text-info --editable --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" --title="Your text?")"
fromlang=""
tolang=""
deftolang=$(locale|grep LANG|cut -c 6-7)

if [ "$txt" == "" ]; then
    zenity --error --text="I can't translate no text!"
else

function what(){
        zenity  --list --column="" --column=Language --column=Code --radiolist --print-column=3 \
        --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" \
        --text="What language is the text in? Pressing Cancel sets this to autotranslate it." \
        '' Autodetect "" \
        '' "Afrikaans" "af" \
        '' "Albanian" "sq" \
        '' "Arabic" "ar" \
        '' "Belarusian" "be" \
        '' "Bulgarian" "bg" \
        '' "Catalan; Valencian" "ca" \
        '' "Chinese" "zh" \
        '' "Croatian" "hr" \
        '' "Czech" "cs" \
        '' "Danish" "da" \
        '' "Dutch" "nl" \
        '' "English" "en" \
        '' "Estonian" "et" \
        '' "Finnish" "fi" \
        '' "French" "fr" \
        '' "Galician" "gl" \
        '' "German" "de" \
        '' "Greek, Modern" "el" \
        '' "Haitian; Haitian Creole" "ht" \
        '' "Hebrew (modern)" "he" \
        '' "Hindi" "hi" \
        '' "Hungarian" "hu" \
        '' "Icelandic" "is" \
        '' "Indonesian" "id" \
        '' "Italian" "it" \
        '' "Irish" "ga" \
        '' "Japanese" "ja" \
        '' "Korean" "ko" \
        '' "Latvian" "lv" \
        '' "Lithuanian" "lt" \
        '' "Macedonian" "mk" \
        '' "Malay" "ms" \
        '' "Maltese" "mt" \
        '' "Norwegian" "no" \
        '' "Persian" "fa" \
        '' "Polish" "pl" \
        '' "Portuguese" "pt" \
        '' "Romanian" "ro" \
        '' "Russian" "ru" \
        '' "Sanskrit" "sa" \
        '' "Sardinian" "sc" \
        '' "Sindhi" "sd" \
        '' "Northern Sami" "se" \
        '' "Samoan" "sm" \
        '' "Sango" "sg" \
        '' "Serbian" "sr" \
        '' "Slovak" "sk" \
        '' "Slovenian" "sl" \
        '' "Spanish; Castilian" "es" \
        '' "Swedish" "sv" \
        '' "Tagalog" "tl" \
        '' "Thai" "th" \
        '' "Turkish" "tr" \
        '' "Ukrainian" "uk" \
        '' "Vietnamese" "vi" \
        '' "Welsh" "cy" \
        '' "Yiddish" "yi"
    }
    
function lang(){
        eval $1=$(what)
        echo $1
    }
    
lang fromlang
    if [ "$fromlang" == "" ]; then
        tolang=""
    else
        lang tolang
    fi
    wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$txt&langpair=$fromlang|${tolang:-$deftolang}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/' > /tmp/lookup.txt
        if [ "$fromlang" == "" -a "$tolang" == "" ]; then
                    zenity --info --text="$txt in your language is: $(cat /tmp/lookup.txt)" --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" \
                    --title="$txt Auto-translate"
        else
                    zenity --info --text="$txt \($fromlang\) to $tolang is: $(cat /tmp/lookup.txt)" --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" \
                    --title="Translate $txt ($fromlang) to $tolang"
        fi
fi

dictd frontend (named as lookup.sh on my machine):

#!/bin/sh

#just comment/delete the window line if you don't have the icons
word=$(zenity --entry --title="Look up a word" --text="What word do you want to look up?" --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png")
dbs=""
if [ -z $word ]; then
    zenity --error --text="I can't define no text!"
else
    function what(){
        zenity  --list --column="" --column=Database --column=Code --radiolist --print-column=3 \
        --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
        --text="Where do you want to look?" --title="Select database" \
        '' "Everywhere" "everywhere" \
        '' "Wordnet" "wn" \
        '' "The Collaborative International Dictionary of English" "gcide" \
        '' "Moby Thesaurus II" "moby-thes" \
        '' "The Devil's Dictionary" "devils" \
        '' "Free On-line Dictionary of Computing" "foldoc"
    }
    
    function where(){
        eval $1=$(what)
    }
    
    where dbs

    if [ $dbs == "everywhere" ]; then
        dict $word > /tmp/lookup.txt
    else
        dict --database $dbs $word > /tmp/lookup.txt
    fi
zenity --text-info --filename=/tmp/lookup.txt \
--window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
--title="Definition lookup complete"
fi
TODO:
*Put the output of the GT box in a Zenity text-info box, while presenting the original text [Done]
*Be able to set default to language for auto-translate without editing the code itself [Done, now auto-detects based on your locale]
*Accept multiple dictionary databases for the dictd frontend [Done]
*Automatically see the available databases for dictd in the dictd frontend [Done, but the number of databases is hard-coded]
*Find icons for GT and dictd frontends that are available in packaged icons or just put them in with the pkg if it's going to be put in a PKGBUILD <-- purely asthetic
*Translate a file for GT frontend
*study up on exit codes and make sure the cancel button cancels
BUGS:
There's currently none that I'm aware of, but if there are any, please report them!
also, if you don't have the icons for the windows, remove the lines that define them
code cleanups should also be suggested here
*One code cleanup on zenity dict frondend

Last edited by ShadowKyogre (2010-04-28 02:07:28)


For every problem, there is a solution that is:
Clean
Simple and most of all...wrong!
Github page

Offline

#2 2010-04-26 20:57:47

VCoolio
Member
From: Netherlands
Registered: 2010-01-05
Posts: 120

Re: Zenity frontends for Google Translate and Dictd

Nice, but the google thingy asks twice what language the text is in, takes the first chosen option as source language and the second chosen as target language. Those should have different questions with them.

Offline

#3 2010-04-26 22:13:14

ShadowKyogre
Member
From: Hell! XP No... I'm not telling
Registered: 2008-12-19
Posts: 476
Website

Re: Zenity frontends for Google Translate and Dictd

VCoolio wrote:

Nice, but the google thingy asks twice what language the text is in, takes the first chosen option as source language and the second chosen as target language. Those should have different questions with them.

Fixed, also planning to add the ability to translate a plain text file and save the result as a file:
[EDIT: fixed a typo]
[EDIT]: added show queries in a textbox
[EDIT]: Added option to translate plain text files

#!/bin/sh

#just comment/delete the window line if you don't have the icons
if [ $1 == "-f" ]; then
    txt="$(cat $(zenity --file-selection --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" --title="Your text?" --file-filter=*.txt))"
else
    txt="$(zenity --text-info --editable --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" --title="Your text?")"
fi
fromlang=""
tolang=""
deftolang=$(locale|grep LANG|cut -c 6-7)
huh="What language is the text in? Pressing Cancel sets this to autotranslate it."

if [ "$txt" == "" ]; then
    zenity --error --text="I can't translate no text!"
else

function what(){
        zenity  --list --column="" --column=Language --column=Code --radiolist --print-column=3 \
        --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" \
        --text="$huh" \
        --hide-column=3 \
        '' "I just want to read it..." "" \
        '' "Afrikaans" "af" \
        '' "Albanian" "sq" \
        '' "Arabic" "ar" \
        '' "Belarusian" "be" \
        '' "Bulgarian" "bg" \
        '' "Catalan; Valencian" "ca" \
        '' "Chinese" "zh" \
        '' "Croatian" "hr" \
        '' "Czech" "cs" \
        '' "Danish" "da" \
        '' "Dutch" "nl" \
        '' "English" "en" \
        '' "Estonian" "et" \
        '' "Finnish" "fi" \
        '' "French" "fr" \
        '' "Galician" "gl" \
        '' "German" "de" \
        '' "Greek, Modern" "el" \
        '' "Haitian; Haitian Creole" "ht" \
        '' "Hebrew (modern)" "he" \
        '' "Hindi" "hi" \
        '' "Hungarian" "hu" \
        '' "Icelandic" "is" \
        '' "Indonesian" "id" \
        '' "Italian" "it" \
        '' "Irish" "ga" \
        '' "Japanese" "ja" \
        '' "Korean" "ko" \
        '' "Latvian" "lv" \
        '' "Lithuanian" "lt" \
        '' "Macedonian" "mk" \
        '' "Malay" "ms" \
        '' "Maltese" "mt" \
        '' "Norwegian" "no" \
        '' "Persian" "fa" \
        '' "Polish" "pl" \
        '' "Portuguese" "pt" \
        '' "Romanian" "ro" \
        '' "Russian" "ru" \
        '' "Sanskrit" "sa" \
        '' "Sardinian" "sc" \
        '' "Sindhi" "sd" \
        '' "Northern Sami" "se" \
        '' "Samoan" "sm" \
        '' "Sango" "sg" \
        '' "Serbian" "sr" \
        '' "Slovak" "sk" \
        '' "Slovenian" "sl" \
        '' "Spanish; Castilian" "es" \
        '' "Swedish" "sv" \
        '' "Tagalog" "tl" \
        '' "Thai" "th" \
        '' "Turkish" "tr" \
        '' "Ukrainian" "uk" \
        '' "Vietnamese" "vi" \
        '' "Welsh" "cy" \
        '' "Yiddish" "yi"
    }
    
function lang(){
        if [ $1 == tolang ]; then
            huh="What language do you want the text in?"
        fi
        eval $1=$(what)
    }
    
lang fromlang
    if [ "$fromlang" == "" ]; then
        tolang=""
    else
        lang tolang
    fi
    echo "Your original query in $fromlang :" > /tmp/lookup.txt
    echo "$txt" >> /tmp/lookup.txt
    echo "" >> /tmp/lookup.txt
    echo "Your new query in $tolang :" >> /tmp/lookup.txt
    wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$txt&langpair=$fromlang|${tolang:-$deftolang}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/' >> /tmp/lookup.txt
        if [ "$fromlang" == "" -a "$tolang" == "" ]; then
            zenity --text-info --filename="/tmp/lookup.txt" --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" \
                     --title="$txt Auto-translate" 
        else
            zenity --text-info --filename="/tmp/lookup.txt" --window-icon="$HOME/.icons/Breathless/48x48/apps/education_languages_section.png" \
                    --title="Translated from ($fromlang) to $tolang"
        fi
fi

[EDIT]: added more databases for the dict frontend. replace the previous lines for the databases with this:
[EDIT]: did a little cleanup and put in the ability to search multiple databases
[EDIT]: added matching features and getting information on databases

dbs=""
function where(){
    dbs=$(zenity  --list --column="" --column=Database --column=Code --checklist --print-column=3 \
        --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
        --text="What do are the selected databases about?" --title="Select database" \
        --hide-column=3 --separator=" -d " \
        '' "Everywhere" "" \
        '' "Wordnet" "wn" \
        '' "The Collaborative International Dictionary of English" "gcide" \
        '' "Moby Thesaurus II" "moby-thes" \
        '' "Webster's Revised Unabridged Dictionary" "web1913" \
        '' "The Devil's Dictionary" "devils" \
        '' "Publication Information for The World Factbook 1995" "world95" \
        '' "Easton's 1897 Bible Dictionary" "easton" \
        '' "Hitchcock's Bible Names Dictionary" "hitchcock" \
        '' "Bouvier's Law Dictionary" "bouvier" \
        '' "CIA World Factbook 2002" "world02" \
        '' "Free On-line Dictionary of Computing" "foldoc" \
        '' "Elements database 20001107" "elements" \
        '' "V.E.R.A. (list dealing with computational acronyms)" "vera" \
        '' "Jargon File (comprehensive compendium of hacker slang)" "jargon" \
        '' "U.S. Gazetteer" "gazetteer" \
        '' "U.S. Gazetteer Countries" "gaz-county" \
        '' "U.S. Gazetteer Places" "gaz-place" \
        '' "U.S. Gazetteer Zip Codes" "gaz-zip")
    }
function check(){
    if [ -z $word ]; then
    zenity --error --text="I can't look up no text!"
    exit 1
    fi
    }

case $1 in
"-i")
    where
    if [ "$dbs" == "" ]; then
        dict -i wn gcide moby-thes web1913 devils world95 easton hitchcock \
        bouvier world02 foldoc elements vera jargon gazetteer gaz-country gaz-place gaz-zip > /tmp/lookup.txt
    else
        dict -d $dbs > /tmp/lookup.txt
    fi
    zenity --text-info --filename=/tmp/lookup.txt \
    --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
    --title="Database info lookup complete"
    exit
    ;;
"-m")
    word=$(zenity --entry --title="Look up a word" --text="What do you want to match?" --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png")
    check
    strategy=$(zenity  --list --column="" --column=Code --column=Strategies --checklist --print-column=2 \
        --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
        --text="What do are the selected databases about?" --title="Select database" \
        --hide-column=2 --separator=" -s " \
        '' exact      "Match headwords exactly" \
        '' prefix     "Match prefixes" \
        '' substring "Match substring occurring anywhere in a headword" \
        '' suffix     "Match suffixes" \
        '' re        "POSIX 1003.2 (modern) regular expressions" \
        '' regexp    "Old (basic) regular expressions" \
        '' soundex   "Match using SOUNDEX algorithm" \
        '' lev       "Match headwords within Levenshtein distance one" \
        '' word      "Match separate words within headwords")
    where
    if [ "$dbs" == "" ]; then
        if [ $strategy == "" ];then
        dict -m $word > /tmp/lookup.txt    
        else
        dict -m $word -s $strategy > /tmp/lookup.txt
        fi
    else
        if [ $strategy == "" ];then
        dict -m --database $dbs $word > /tmp/lookup.txt
        else
        dict -m --database $dbs $word -s $strategy > /tmp/lookup.txt
        fi
    fi
    zenity --text-info --filename=/tmp/lookup.txt \
    --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
    --title="Match complete"
    exit
    ;;
"-h")
    echo "Usage:"
    echo "No arguments: Use the GUI to look up words."
    echo "-m: Use the GUI to match query."
    echo "-i: Use the GUI to get information on databases."
    echo "-h: This message."
    exit
    ;;
"")
    word=$(zenity --entry --title="Look up a word" --text="What word do you want to define?" --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png")
        check
        where
        if [ "$dbs" == "" ]; then
            dict $word > /tmp/lookup.txt
        else
            dict --database $dbs $word > /tmp/lookup.txt
        fi
        zenity --text-info --filename=/tmp/lookup.txt \
        --window-icon="$HOME/.icons/Breathless/48x48/apps/kdict.png" \
        --title="Definition lookup complete"
    exit
    ;;
esac

Last edited by ShadowKyogre (2010-04-29 16:18:24)


For every problem, there is a solution that is:
Clean
Simple and most of all...wrong!
Github page

Offline

Board footer

Powered by FluxBB