Skip to content

Instantly share code, notes, and snippets.

@hrwgc
Created December 12, 2012 20:34
Show Gist options
  • Save hrwgc/4271358 to your computer and use it in GitHub Desktop.
Save hrwgc/4271358 to your computer and use it in GitHub Desktop.
Command line translator application using Bing's free Translator service.
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo 'Usage: translate.sh ["Original Text"] ["Source Language"] ["Target Language"]'
echo 'Example: translate.sh "Hello World" "en" "fr"'
declare -a LANG_NAMES=('Arabic' 'Czech' 'Danish' 'German' 'English' 'Estonian' 'Finnish' 'French' 'Dutch' 'Greek' 'Hebrew' 'Haitian Creole' 'Hungarian' 'Indonesian' 'Italian' 'Japanese' 'Korean' 'Lithuanian' 'Latvian' 'Norwegian' 'Polish' 'Portuguese' 'Romanian' 'Spanish' 'Russian' 'Slovak' 'Slovene' 'Swedish' 'Thai' 'Turkish' 'Ukrainian' 'Vietnamese' 'Simplified Chinese' 'Traditional Chinese');
declare -a LANG_CODES=('ar' 'cs' 'da' 'de' 'en' 'et' 'fi' 'fr' 'nl' 'el' 'he' 'ht' 'hu' 'id' 'it' 'ja' 'ko' 'lt' 'lv' 'no' 'pl' 'pt' 'ro' 'es' 'ru' 'sk' 'sl' 'sv' 'th' 'tr' 'uk' 'vi' 'zh-CHS' 'zh-CHT');
for IX in $(seq 0 $((${#LANG_CODES[@]} - 1))); do
echo ${LANG_CODES[$IX]}: ${LANG_NAMES[$IX]};
done
exit 1
fi
export LC_ALL=C
ORIGINAL_TEXT="$1"
SOURCE_LANG="$2"
DEST_LANG="$3"
# URL_Encode function from http://ethertubes.com/bash-snippet-url-encoding/
# future feature: Languages: http://www.emreakkas.com/internationalization/microsoft-translator-api-languages-list-language-codes-and-names
urlencode () {
tab="`echo -en "\x9"`"
i="$@"
i=${i//%/%25} ; i=${i//' '/%20} ; i=${i//$tab/%09}
i=${i//!/%21} ; i=${i//\"/%22} ; i=${i//#/%23}
i=${i//\$/%24} ; i=${i//\&/%26} ; i=${i//\'/%27}
i=${i//(/%28} ; i=${i//)/%29} ; i=${i//\*/%2a}
i=${i//+/%2b} ; i=${i//,/%2c} ; i=${i//-/%2d}
i=${i//\./%2e} ; i=${i//\//%2f} ; i=${i//:/%3a}
i=${i//;/%3b} ; i=${i//</%3c} ; i=${i//=/%3d}
i=${i//>/%3e} ; i=${i//\?/%3f} ; i=${i//@/%40}
i=${i//\[/%5b} ; i=${i//\\/%5c} ; i=${i//\]/%5d}
i=${i//\^/%5e} ; i=${i//_/%5f} ; i=${i//\`/%60}
i=${i//\{/%7b} ; i=${i//|/%7c} ; i=${i//\}/%7d}
i=${i//\~/%7e}
echo "$i"
i=""
}
curl -X POST --data "grant_type=client_credentials&client_id=${BING_CLIENT_ID}&client_secret=${BING_CLIENT_SECRET}&scope=http://api.microsofttranslator.com" https://datamarket.accesscontrol.windows.net/v2/OAuth2-13 > $SH/tmp/response.json
ACCESS_TOKEN=$(cat $SH/tmp/response.json | sed -E 's~^.*access_token":"([^"]+)".*$~\1~g');
ENCODED_TEXT=$(urlencode "$ORIGINAL_TEXT");
curl -H "Authorization:bearer $ACCESS_TOKEN" "http://api.microsofttranslator.com/V2/Http.svc/Translate?text=${ENCODED_TEXT}&from=${SOURCE_LANG}&to=${DEST_LANG}"> $SH/tmp/translation.xml &&
TRANSLATION_TEXT=$(cat $SH/tmp/translation.xml | sed -E 's~<[^>]+>~~g');
echo "----------------------------------------"
echo " Original "
echo " "
echo "$ORIGINAL_TEXT"
echo " "
echo "----------------------------------------"
echo " Translation "
echo " "
echo "$TRANSLATION_TEXT"
echo " "
@zw963
Copy link

zw963 commented Oct 10, 2018

This not work.

 ╰─ $ translate.sh 'hello' 'en' 'zh-CHS'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: datamarket.accesscontrol.windows.net
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   215  100   215    0     0    529      0 --:--:-- --:--:-- --:--:--   529
----------------------------------------
               Original

hello

----------------------------------------
               Translation

Argument ExceptionMethod: Translate()Parameter: Message: No bearer information found in the tokenmessage id=0759.V2_Rest.Translate.162F5E13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment