#!/bin/bash #=============================================================================== # # FILE: potranslator # # USAGE: ./potranslator2 -i inputFile -o Outputfile -l lang [-p] [-h] # # DESCRIPTION: # # OPTIONS: --- # REQUIREMENTS: apertium-unicode, lttoolbox-unicode, apertium-??-?? # BUGS: --- # NOTES: --- # AUTHOR: Miguel Gea Milvaques (xerakko), xerakko@debian.org # COMPANY: Debian.org # VERSION: 2.0 # CREATED: 01/10/07 18:41:39 CEST # REVISION: ---- #=============================================================================== # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # ------------------------------------------------------------------------- file_exists() { if [ ! -f "$1" ] ; then echo "Error: File \"$1\" not found." return 1 fi return 0 } help () { echo "PO Translator" echo "$0 Usage: -i InputFile -o OutputFile -l LangPair [-p] [-h]" echo "" echo "-i InputFile > po file to translate " echo "-o OutputFile > translated output file" echo "-l LangPair > dictionary used (es-ca, en-ca fr-ca...)" echo "-p > translate the first msgstr string in po file" echo "-h > use html parser for translations" } normal () { echo "$1" >> "$2" } msgstr () { echo "$1" | apertium $HTMLOPT -u $3 >> "$2" CAD=$(($CAD+1)) [ $CAD -gt $(($CADENAS/10)) ] && echo -n '#' && CAD=0 } ######################################################################### # Main part ######################################################################### TRANS_FIRST_MSTR=0 CAD=0 if [ $# != 6 ] && [ $# != 7 ] && [ $# != 8 ]; then echo "ERROR: Incorrect parrameters count" help exit 1 fi while getopts ":i:o:l:p:h" opcio do case $opcio in i) echo "Input File: "${OPTARG} INPUTFILE=${OPTARG} file_exists "$INPUTFILE" || exit 1 INPTMP=`mktemp /tmp/potranslator.XXX` mv "$INPTMP" "$INPTMP".po INPTMP="$INPTMP".po msgattrib -w 20000 "$INPUTFILE" > "$INPTMP" sed -i 's/\\n/ NiUlYNe /g' "$INPTMP" INPUTFILE="$INPTMP" CADENAS=`pocount --csv "$INPUTFILE"|grep ^${INPUTFILE}|cut -d, -f9` ;; o) echo "Output File: "${OPTARG} OUTPUTFILE="${OPTARG}" if [ -f "$OUTPUTFILE" ]; then echo "Warning!: The output already file exists!" exit 1 fi ;; l) echo "Selected pair: "${OPTARG} LANGPAIR=${OPTARG} ;; p) echo "The first msgtr will be translated" TRANS_FIRST_MSTR=1 ;; h) echo "Using html parser in apertium" HTMLOPT="-f wxml" ;; *) echo "ERROR: Incorrect parrameters" exit 1 ;; esac # --- end of case --- done shift $(($OPTIND-1)) STRING=0 festat=normal cat $INPUTFILE | \ while read -r line ; do echo "$line" | grep -E "^#|^msgid|^['\ '|'\t']*$" > /dev/null && festat=normal echo "$line" | grep ^msgstr > /dev/null && festat=msgstr && STRING=$((STRING+1)) if [ "$STRING" == "1" ] && [ "$TRANS_FIRST_MSTR" == "0" ]; then festat=normal fi $festat "$line" "$OUTPUTFILE" "$LANGPAIR" done echo . rm "$INPTMP" OUTTMP=`mktemp potranslator.out.XXXX` msgattrib -w 80 "$OUTPUTFILE"| sed 's/ NiUlYNe /\\n/g' >"$OUTTMP" mv "$OUTTMP" "$OUTPUTFILE"