Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###Set zenity cancel function
- function cancel {
- if [ $? -eq "1" ]; then
- zenity --error --title="Canceled" --text="Canceled" --timeout=1
- exit 0
- fi
- }
- ###Set zenity report success function
- report_trans (){
- zenity --info --title="Translation finished" \
- --text="Your file has been translated"
- }
- ###Set functions to store needed variables
- getsrclng () {
- SrcLng=`zenity --list --radiolist \
- --title "Select Source Language" --text "Translate from" \
- --column "" --column "Code" --column "Language" \
- "TRUE" "en" "English" \
- "" "ru" "Russian" \
- "" "uk" "Ukrainian"`
- cancel
- }
- gettarglng () {
- TargLng=`zenity --list --radiolist \
- --title "Select Target Language" --text "Translate to" \
- --column "" --column "Code" --column "Language" \
- "" "en" "English" "" \
- "ru" "Russian" "TRUE" \
- "uk" "Ukrainian"`
- cancel
- }
- getsourcefile () {
- sourcefile=`zenity --file-selection \
- --title="Select file to be translated" \
- --file-filter="*.txt *.TXT" \
- --file-filter="*.*"`
- cancel
- }
- gettargetfile () {
- #~ targetfile=`zenity --file-selection --save --confirm-overwrite \
- #~ --title="Select file to store the translation"`
- #~ cancel
- targetfile="${sourcefile%.*}"_"$mtengine"_\($SrcLng-$TargLng\)."${sourcefile##*.}"
- }
- getmtengine () {
- mtengine=`zenity --list --radiolist \
- --title="MT Service" \
- --text="Select MT Service" \
- --column "" --column "Service" \
- "" "Google" \
- "" "Yandex"`
- cancel
- if [ -z $mtengine ]; then
- zenity --title="Error" --error \
- --text="No MT service specified" --timeout=1
- getmtengine
- fi
- }
- ###Set function to ensure the number of lines is specified
- getlinesatime () {
- lines_a_time=`zenity \
- --entry --text="How many lines of text to tranlate at a time?\n \
- This number should be less then $lines_in_source.\n \
- 40 or 50 lines at a time is the optimal value." \
- --title="Lines of text"`
- cancel
- if [ $lines_a_time -ge $lines_in_source ]; then
- zenity --title="Error" --error \
- --text="Lower number required" --timeout=1
- getlinesatime
- fi
- if [ -z $lines_a_time ]; then
- zenity --title="Error" --error \
- --text="Number required" --timeout=1
- getlinesatime
- fi
- if [ "`echo $lines_a_time|grep ^[0-9]*$|wc -c`" -eq "0" ]; then
- zenity --title="Error" --error \
- --text="Digits only" --timeout=1
- getlinesatime
- fi
- }
- ###Set tmp file to store each chunk of translation
- tmpfile=/tmp/translation
- ###Set yandex translate function
- function yandex_translate {
- langpair="lang=$SrcLng-$TargLng"
- curl --silent -d $langpair \
- --data-urlencode "text=`cat "$sourcefile" |\
- head -$lines |\
- tail -$lines_a_time`" \
- http://translate.yandex.net/api/v1/tr/translate |\
- sed s/'<'/'<'/g |\
- sed s/'>'/'>'/g |\
- sed s/'''/'’'/g |\
- sed s/'"'/'"'/g |\
- sed '/<?xml version="1.0" encoding="utf-8"?>/d' |\
- sed s/'<\/text><\/Translation>'/''/g |\
- sed s/'<Translation code=\"200\" lang=\".*\"><text>'/''/g \
- > $tmpfile
- }
- ###Set google translate function
- google_translate () {
- result=$(curl -s -i --user-agent "lynx" \
- -d "sl=$SrcLng" -d "tl=$TargLng" \
- --data-urlencode "text=`cat "$sourcefile" |\
- head -$lines |\
- tail -$lines_a_time`" \
- http://translate.google.com)
- encoding=$(awk '/Content-Type: .* charset=/ \
- {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' \
- <<< "$result")
- #~ TransTxt=`iconv -f $encoding <<<"$result" |\
- #~ awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' |\
- #~ html2text -utf8 |sed s/'"'/'"'/g`
- iconv -f $encoding <<<"$result" |\
- awk -F"TRANSLATED_TEXT='" -v OFS="\nZZZ\n" ' $1=$1 ' |\
- awk '/ZZZ/{getline;print}' |\
- awk -F"';INPUT_TOOL_PATH='" -v OFS="\nZZZ\n" ' $1=$1 ' |\
- awk '/ZZZ/{getline;next}{print}' |\
- sed 's/\\x3cbr\\x3e/\n/g' |\
- sed 's/\\x26lt;/</g' |\
- sed 's/\\x26gt;/>/g' |\
- sed 's/\\x26#39;/"’"/g' |\
- sed 's/\\x26quot;/"\""/g' \
- > $tmpfile
- #~ echo $TransTxt > $tmpfile
- }
- ###Collect information about the translation
- getsourcefile
- getsrclng
- gettarglng
- getmtengine
- gettargetfile
- lines_in_source=`cat "$sourcefile"|wc -l`
- #Check if target file exists and create it, if not
- if [ ! -e "$targetfile" ]; then
- if [ ! -d `dirname "$targetfile"` ]; then
- mkdir `dirname "$targetfile"`
- else touch "$targetfile"
- fi
- fi
- cat /dev/null > "$targetfile"
- ###Check which MT engine was selected and behave accordingly
- case $mtengine in
- #############################
- ###Here's TRANSLATION COMMAND
- #############################
- "Yandex" | "Google" )
- getlinesatime
- ###Initially set number of lines to 0, this number will increment with
- #each loop
- lines=0
- ###Set loops
- loops=`expr $lines_in_source / $lines_a_time`
- lines_at_the_end=`expr $lines_in_source % $lines_a_time`
- ###Translate
- round=1
- (while [ $round -le $loops ]; do
- lines=$(expr $lines + $lines_a_time)
- ${mtengine,,}_translate
- cat $tmpfile >> "$targetfile"
- sleep 3
- echo -en "\n\n" >> "$targetfile"
- round=$(expr $round + 1)
- done
- ###Change values of $lines and $lines_a_time to translate the remainder
- #of the file
- lines=$lines_in_source
- lines_a_time=$lines_at_the_end
- ${mtengine,,}_translate
- cat $tmpfile >> "$targetfile")|\
- (if `zenity --progress --auto-close --text='Translation in progress...\nPlease do not close this window util it is finished' --title='Translating...'`;
- then
- echo 'Job completed'
- else
- killall `basename $0`
- exit
- fi)
- ###Report success
- report_trans
- rm $tmpfile
- ;;
- esac
- ####DONE WITH TRANSLATION!!!
- ###Now we move to aligning
- #For that LF_Align is needed
- zenity --question --title="Create TMX" \
- --text="Align files and create TMX?\n
- LF_Aligher should be installed"
- if [ $? -eq 1 ] ; then
- exit 23
- fi
- lfalingpath=$HOME/.config/tmxcreate/lfalingpath
- if [ $? -eq 0 ] ; then
- if [ ! -e $lfalingpath ]; then
- zenity --question --title="LF_Aligner installed?" \
- --text="Is LF_Aligner present in the system?"
- if [ $? -eq 1 ] ; then
- exit 25
- fi
- if [ ! -d `dirname "$lfalingpath"` ]; then
- mkdir `dirname "$lfalingpath"`
- touch "$lfalingpath"
- fi
- zenity --file-selection --directory \
- --title="Navigate to LF_Aligner directory" \
- --text="Navigate to the directory where LF_Aligner is installed.\n
- Once it is selected, this question will not be asked again" \
- > $lfalingpath
- fi
- lf_aling_path=`cat $lfalingpath`
- LFA_exec=`find $lf_aling_path -iname "LF_aligner_*.pl"`
- cd `dirname "$LFA_exec"`
- $LFA_exec -f="t" -l="${SrcLng^^}","${TargLng^^}" -i="$sourcefile","$targetfile" \
- -r="n" -s="n" -t="y"
- fi
- cd "`dirname "$sourcefile"`"
- xdg-open "`ls ./ -Art|tail -1`"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement