Advertisement
KosIvantsov

mttranslate2tmx

May 13th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.56 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ###Set zenity cancel function
  4. function cancel {
  5.     if [ $? -eq "1" ]; then
  6.         zenity --error --title="Canceled" --text="Canceled" --timeout=1
  7.         exit 0
  8.     fi
  9.     }
  10.  
  11. ###Set zenity report success function
  12. report_trans (){
  13.     zenity --info --title="Translation finished" \
  14.     --text="Your file has been translated"
  15.     }
  16.  
  17. ###Set functions to store needed variables
  18. getsrclng () {
  19.     SrcLng=`zenity --list --radiolist \
  20.     --title "Select Source Language" --text "Translate from" \
  21.     --column "" --column "Code" --column "Language" \
  22.     "TRUE" "en" "English" \
  23.     "" "ru" "Russian" \
  24.     "" "uk" "Ukrainian"`
  25.     cancel
  26. }
  27.  
  28. gettarglng () {
  29.     TargLng=`zenity --list --radiolist \
  30.     --title "Select Target Language" --text "Translate to" \
  31.     --column "" --column "Code" --column "Language" \
  32.     "" "en" "English" "" \
  33.     "ru" "Russian" "TRUE" \
  34.     "uk" "Ukrainian"`
  35.     cancel
  36. }
  37.  
  38. getsourcefile () {
  39.     sourcefile=`zenity --file-selection \
  40.     --title="Select file to be translated" \
  41.     --file-filter="*.txt *.TXT" \
  42.     --file-filter="*.*"`
  43.     cancel
  44.     }
  45.  
  46. gettargetfile () {
  47.     #~ targetfile=`zenity --file-selection --save --confirm-overwrite \
  48.     #~ --title="Select file to store the translation"`
  49.     #~ cancel
  50.     targetfile="${sourcefile%.*}"_"$mtengine"_\($SrcLng-$TargLng\)."${sourcefile##*.}"
  51. }
  52.  
  53. getmtengine () {
  54.     mtengine=`zenity --list --radiolist \
  55.     --title="MT Service" \
  56.     --text="Select MT Service" \
  57.     --column "" --column "Service" \
  58.     "" "Google" \
  59.     "" "Yandex"`
  60.     cancel
  61.     if [ -z $mtengine ]; then
  62.         zenity --title="Error" --error \
  63.         --text="No MT service specified" --timeout=1
  64.         getmtengine
  65.     fi
  66. }
  67.  
  68. ###Set function to ensure the number of lines is specified
  69. getlinesatime () {
  70.     lines_a_time=`zenity \
  71.     --entry --text="How many lines of text to tranlate at a time?\n \
  72. This number should be less then $lines_in_source.\n \
  73. 40 or 50 lines at a time is the optimal value." \
  74.     --title="Lines of text"`
  75.     cancel
  76.     if [ $lines_a_time -ge $lines_in_source ]; then
  77.         zenity --title="Error" --error \
  78.         --text="Lower number required" --timeout=1
  79.         getlinesatime
  80.     fi
  81.     if [ -z $lines_a_time ]; then
  82.         zenity --title="Error" --error \
  83.         --text="Number required" --timeout=1
  84.         getlinesatime
  85.     fi
  86.     if [ "`echo $lines_a_time|grep ^[0-9]*$|wc -c`" -eq "0" ]; then
  87.         zenity --title="Error" --error \
  88.         --text="Digits only" --timeout=1
  89.         getlinesatime
  90.     fi
  91. }
  92.  
  93. ###Set tmp file to store each chunk of translation
  94. tmpfile=/tmp/translation
  95.  
  96. ###Set yandex translate function
  97. function yandex_translate {
  98.     langpair="lang=$SrcLng-$TargLng"
  99.     curl --silent -d $langpair \
  100.     --data-urlencode "text=`cat "$sourcefile" |\
  101.     head -$lines |\
  102.     tail -$lines_a_time`" \
  103.     http://translate.yandex.net/api/v1/tr/translate |\
  104.     sed s/'&lt;'/'<'/g |\
  105.     sed s/'&gt;'/'>'/g |\
  106.     sed s/'&apos;'/'’'/g |\
  107.     sed s/'&quot;'/'"'/g |\
  108.     sed '/<?xml version="1.0" encoding="utf-8"?>/d' |\
  109.     sed s/'<\/text><\/Translation>'/''/g |\
  110.     sed s/'<Translation code=\"200\" lang=\".*\"><text>'/''/g \
  111.     > $tmpfile
  112.     }
  113.  
  114. ###Set google translate function
  115. google_translate () {
  116.     result=$(curl -s -i --user-agent "lynx" \
  117.     -d "sl=$SrcLng" -d "tl=$TargLng" \
  118.     --data-urlencode "text=`cat "$sourcefile" |\
  119.     head -$lines |\
  120.     tail -$lines_a_time`" \
  121.     http://translate.google.com)
  122.     encoding=$(awk '/Content-Type: .* charset=/ \
  123.     {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' \
  124.     <<< "$result")
  125.     #~ TransTxt=`iconv -f $encoding <<<"$result" |\
  126.     #~ awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' |\
  127.     #~ html2text -utf8 |sed  s/'&quot;'/'"'/g`
  128.     iconv -f $encoding <<<"$result" |\
  129.     awk -F"TRANSLATED_TEXT='" -v OFS="\nZZZ\n" ' $1=$1 ' |\
  130.     awk '/ZZZ/{getline;print}' |\
  131.     awk -F"';INPUT_TOOL_PATH='" -v OFS="\nZZZ\n" ' $1=$1 ' |\
  132.     awk '/ZZZ/{getline;next}{print}' |\
  133.     sed 's/\\x3cbr\\x3e/\n/g' |\
  134.     sed 's/\\x26lt;/</g' |\
  135.     sed 's/\\x26gt;/>/g' |\
  136.     sed 's/\\x26#39;/"’"/g' |\
  137.     sed 's/\\x26quot;/"\""/g' \
  138.     > $tmpfile
  139.     #~ echo $TransTxt > $tmpfile
  140. }
  141.  
  142. ###Collect information about the translation
  143. getsourcefile
  144. getsrclng
  145. gettarglng
  146. getmtengine
  147. gettargetfile
  148.  
  149. lines_in_source=`cat "$sourcefile"|wc -l`
  150.  
  151. #Check if target file exists and create it, if not
  152. if [ ! -e "$targetfile" ]; then
  153.     if [ ! -d `dirname "$targetfile"` ]; then
  154.     mkdir `dirname "$targetfile"`
  155.     else touch "$targetfile"
  156.     fi
  157. fi
  158. cat /dev/null > "$targetfile"
  159.  
  160.  
  161. ###Check which MT engine was selected and behave accordingly
  162. case $mtengine in
  163.  
  164. #############################
  165. ###Here's TRANSLATION COMMAND
  166. #############################
  167.     "Yandex" | "Google" )
  168.     getlinesatime
  169.  
  170.     ###Initially set number of lines to 0, this number will increment with
  171.     #each loop
  172.     lines=0
  173.  
  174.     ###Set loops
  175.     loops=`expr $lines_in_source / $lines_a_time`
  176.     lines_at_the_end=`expr $lines_in_source % $lines_a_time`
  177.  
  178.     ###Translate
  179.     round=1
  180.     (while [ $round -le $loops ]; do
  181.     lines=$(expr $lines + $lines_a_time)
  182.     ${mtengine,,}_translate
  183.     cat $tmpfile >> "$targetfile"
  184.     sleep 3
  185.     echo -en "\n\n" >> "$targetfile"
  186.     round=$(expr $round + 1)
  187.     done
  188.  
  189.     ###Change values of $lines and $lines_a_time to translate the remainder
  190.     #of the file
  191.     lines=$lines_in_source
  192.     lines_a_time=$lines_at_the_end
  193.     ${mtengine,,}_translate
  194.     cat $tmpfile >> "$targetfile")|\
  195.     (if `zenity --progress --auto-close --text='Translation in progress...\nPlease do not close this window util it is finished' --title='Translating...'`;
  196.     then
  197.     echo 'Job completed'
  198.     else
  199.     killall `basename $0`
  200.     exit
  201.     fi)
  202.  
  203.     ###Report success
  204.     report_trans
  205.     rm $tmpfile
  206.     ;;
  207. esac
  208. ####DONE WITH TRANSLATION!!!
  209.  
  210.  
  211. ###Now we move to aligning
  212. #For that LF_Align is needed
  213. zenity --question --title="Create TMX" \
  214. --text="Align files and create TMX?\n
  215. LF_Aligher should be installed"
  216. if [ $? -eq 1 ] ; then
  217. exit 23
  218. fi
  219. lfalingpath=$HOME/.config/tmxcreate/lfalingpath
  220. if [ $? -eq 0 ] ; then
  221.     if [ ! -e $lfalingpath ]; then
  222.     zenity --question --title="LF_Aligner installed?" \
  223.     --text="Is LF_Aligner present in the system?"
  224.         if [ $? -eq 1 ] ; then
  225.         exit 25
  226.         fi
  227.  
  228.         if [ ! -d `dirname "$lfalingpath"` ]; then
  229.         mkdir `dirname "$lfalingpath"`
  230.         touch "$lfalingpath"
  231.         fi
  232.  
  233.         zenity --file-selection --directory \
  234.         --title="Navigate to LF_Aligner directory" \
  235.         --text="Navigate to the directory where LF_Aligner is installed.\n
  236. Once it is selected, this question will not be asked again" \
  237.         > $lfalingpath
  238.     fi
  239. lf_aling_path=`cat $lfalingpath`
  240.  
  241. LFA_exec=`find $lf_aling_path -iname "LF_aligner_*.pl"`
  242. cd `dirname "$LFA_exec"`
  243.  
  244. $LFA_exec -f="t" -l="${SrcLng^^}","${TargLng^^}" -i="$sourcefile","$targetfile" \
  245. -r="n" -s="n" -t="y"
  246. fi
  247.  
  248. cd "`dirname "$sourcefile"`"
  249. xdg-open "`ls ./ -Art|tail -1`"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement