LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Looking for a bash/sh script to translate a word with Internet (no package to install (http://www.linuxquestions.org/questions/programming-9/looking-for-a-bash-sh-script-to-translate-a-word-with-internet-no-package-to-install-896423/)

Xeratul 08-09-2011 03:39 PM

Looking for a bash/sh script to translate a word with Internet (no package to install
 
Hello,

I would be glad if someone would have any ideas to translate a word with Internet (no package to install)?

Simple wget would be great :)

here some packages
http://ubuntuforums.org/archive/index.php/t-359574.html

Many thanks
Happy Tux

corp769 08-09-2011 03:55 PM

Hello,

From what language to what language are you looking to translate?

Cheers,

Josh

corp769 08-09-2011 04:02 PM

Never mind my last post, you just gave me a project ;)

-Josh

Xeratul 08-09-2011 04:09 PM

Quote:

Originally Posted by corp769 (Post 4438139)
Never mind my last post, you just gave me a project ;)

-Josh

I like coding too... I never stop kind of doing sh (for cross platform)
( I usually try to avoid bash, and prefer sh for wider compatibility)

corp769 08-09-2011 04:17 PM

Well before I decide to actually do it, I dug around a bit, and came across this - https://savannah.nongnu.org/projects/twandgtw/
What do you think of that?

MrCode 08-09-2011 04:22 PM

I found this on the Arch forums; it basically sends off $1 to Google Translate, GT translates the phrase/word/whatever (from the language you specify as $2; autodetect otherwise), then it snips out the actual translated word(s) from the wget'd page.

Code:

#!/bin/bash
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/';

The post I got it from

Hope this helps. :D

corp769 08-09-2011 04:31 PM

Quote:

Originally Posted by MrCode (Post 4438150)
I found this on the Arch forums; it basically sends off $1 to Google Translate, GT translates the phrase/word/whatever (from the language you specify as $2; autodetect otherwise), then it snips out the actual translated word(s) from the wget'd page.

Code:

#!/bin/bash
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/';

The post I got it from

Hope this helps. :D

So I'm going to be "that guy" and one up you - http://www.commandlinefu.com/command...-in-mp3-format
Text to speech! :D

Xeratul 08-09-2011 04:45 PM

Quote:

Originally Posted by corp769 (Post 4438155)
So I'm going to be "that guy" and one up you - http://www.commandlinefu.com/command...-in-mp3-format
Text to speech! :D

thanks. I remember i saw once that green page ...

We gotta make a $1, $2, ... for making our own script in an *.sh
Ipgeo.sh indeed rocks, and it says how to use it ;) nice job

corp769 08-09-2011 04:51 PM

Well what exactly do you want in this script?

MTK358 08-09-2011 05:48 PM

Quote:

Originally Posted by MrCode (Post 4438150)
I found this on the Arch forums; it basically sends off $1 to Google Translate, GT translates the phrase/word/whatever (from the language you specify as $2; autodetect otherwise), then it snips out the actual translated word(s) from the wget'd page.

Code:

#!/bin/bash
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/';


I think I read that Google is planning on removing the Google Translate API.

MrCode 08-09-2011 07:26 PM

I've improved the little "Google Translator script" thingy…it's in my LQ blog. :D

corp769 08-09-2011 07:30 PM

Quote:

Originally Posted by MrCode (Post 4438262)
I've improved the little "Google Translator script" thingy…it's in my LQ blog. :D

Hmmm... I'm going to check out how many languages google supports. Maybe I can add some to your code? :p

MrCode 08-09-2011 08:11 PM

Quote:

Maybe I can add some to your code?
Err…how exactly do you mean? Do you mean somehow adding language support on the server end, or do you just mean giving some hints as to what languages are available (and maybe forcibly restricting the options to those languages)?

Anyway, as to your "one-up": you can just pipe the output of my blog script to espeak and have it speak out the translation. :D :p

corp769 08-09-2011 08:25 PM

Actually, I thought of something else dude....
Code:

#!/bin/bash
if [ "$1" = "--languages" ]; then
        echo ""
        echo "Supported languages:"
        echo ""
        echo "English - EN              Afrikaans - AF"
        echo "Albanian - SQ            Arabic - AR"
        echo "Armenian - HY            Azerbaijani - AZ"
        echo "Basque - EU              Belarusian - BE"
        echo "Bengali - BN              Bulgarian - BG"
        echo "Catalan - CA              Chinese - ZH"
        echo "Croatian - HR            Czech - CS"
        echo "Danish - DA              Dutch - NL"
        echo "Estonian - ET            Filipino - FIL (NOT TESTED)"
        echo "Finnish - FI              French - FR"
        echo "Galician - GL            Georgian - KA"
        echo "German - DE              Greek - EL"
        echo "Gujarati - GU            Haitian Creole - HT"
        echo "Hebrew - IW              Hindi - HI"
        echo "Hungarian - HU            Icelandic - IS"
        echo "Indonesian - IN          Irish - GA"
        echo "Italian - IT              Japanese - JA"
        echo "Kannada - KN              Korean - KO"
        echo "Latin - LA                Latvian - LV"
        echo "Lithuanian - LT          Macedonian - MK"
        echo "Malay - MS                Maltese - MT"
        echo "Norwegian - NO            Persian - FA"
        echo "Polish - PL              Portuguese - PT"
        echo "Romanian - RO            Russian - RU"
        echo "Serbian - SR              Slovak - SK"
        echo "Slovenian - SL            Spanish - ES"
        echo "Swahili - SW              Swedish - SV"
        echo "Tamil - TA                Telugu - TE"
        echo "Thai - TH                Turkish - TR"
        echo "Ukrainian - UK            Urdu - UR"
        echo "Vietnamese - VI          Welsh - CY"
        echo "Yiddish - JI"
        echo ""
        exit
elif [ "$1" = "" -o "$2" = "" -o "$3" = "" ]; then
        echo "Usage: $0 \"<str>\" <lang1> <lang2>"
        echo "Translates string \"<str>\" from <lang1> to <lang2>"
        echo "(CLI frontend to Google Translate)"
        echo "Type $0 --languages for a list of supported languages."
        exit
else
        wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-$3}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/';
fi

Now you have the full list of supported languages on google's end.....

corp769 08-09-2011 08:36 PM

Damn you and your ideas....
Code:


#!/bin/bash
text1=""
if [ "$1" = "--languages" ]; then
        echo ""
        echo "Supported languages:"
        echo ""
        echo "English - EN                Afrikaans - AF"
        echo "Albanian - SQ                Arabic - AR"
        echo "Armenian - HY                Azerbaijani - AZ"
        echo "Basque - EU                Belarusian - BE"
        echo "Bengali - BN                Bulgarian - BG"
        echo "Catalan - CA                Chinese - ZH"
        echo "Croatian - HR                Czech - CS"
        echo "Danish - DA                Dutch - NL"
        echo "Estonian - ET                Filipino - FIL (NOT TESTED)"
        echo "Finnish - FI                French - FR"
        echo "Galician - GL                Georgian - KA"
        echo "German - DE                Greek - EL"
        echo "Gujarati - GU                Haitian Creole - HT"
        echo "Hebrew - IW                Hindi - HI"
        echo "Hungarian - HU                Icelandic - IS"
        echo "Indonesian - IN                Irish - GA"
        echo "Italian - IT                Japanese - JA"
        echo "Kannada - KN                Korean - KO"
        echo "Latin - LA                Latvian - LV"
        echo "Lithuanian - LT                Macedonian - MK"
        echo "Malay - MS                Maltese - MT"
        echo "Norwegian - NO                Persian - FA"
        echo "Polish - PL                Portuguese - PT"
        echo "Romanian - RO                Russian - RU"
        echo "Serbian - SR                Slovak - SK"
        echo "Slovenian - SL                Spanish - ES"
        echo "Swahili - SW                Swedish - SV"
        echo "Tamil - TA                Telugu - TE"
        echo "Thai - TH                Turkish - TR"
        echo "Ukrainian - UK                Urdu - UR"
        echo "Vietnamese - VI                Welsh - CY"
        echo "Yiddish - JI"
        echo ""
        exit
elif [ "$1" = "" -o "$2" = "" -o "$3" = "" ]; then
        echo "Usage: $0 \"<str>\" <lang1> <lang2>"
        echo "Translates string \"<str>\" from <lang1> to <lang2>"
        echo "(CLI frontend to Google Translate)"
        echo "Type $0 --languages for a list of supported languages."
        exit
else
        text1=`wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-$3}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/';`
        echo ""
        echo "Original text: $1"
        echo "Converted text: $text1"
        echo ""
        read -p "Would you like the converted text spoken to you? [Yy/Nn]" answer
        if [ $answer = "Y" -o $answer = "y" ]; then
                espeak -v $3+f2 "$text1"
        fi
fi

I'm done until you think of something else to add :p


All times are GMT -5. The time now is 01:59 AM.