po2wml.sh
f0e1bafa
 #!/bin/bash
 #
 # Author: Runa Sandvik, <runa.sandvik@gmail.com>
 # Google Summer of Code 2009
 # 
 # This is Free Software (GPLv3)
 # http://www.gnu.org/licenses/gpl-3.0.txt
 #
 # This script will convert all the translated po files back to wml
 # files.
 #
 # For more information, see the HOWTO and README in
 # translation/tools/gsoc09.
 #
 
 ### start config ###
 
 # Location of the wml files
 wmldir="$PWD"
 
 # Location of the po files,
b3c56ddb
 podir="`dirname $wmldir`/translation/projects/website/po"
f0e1bafa
 
 # A lot of the wml files have custom tags. These tags have been defined
 # in website/include/versions.wmi. Tags that people usually forget to close,
 # as well as tags that are not defined in versions.wmi have been added.
 # See: https://svn.torproject.org/svn/website/trunk/include/versions.wmi
4e6637c7
 customtag=`echo $(cat "$wmldir/include/versions.wmi" | awk '{ printf "<%s> " , $2 }' | sed 's/<>//g') "<svnsandbox> <svnwebsite> <svnprojects> <input> <hr> <br> <img> <gitblob>"`
f0e1bafa
 
 # We also need to use the nodefault option of po4a; space separated list
 # of tags that the module should not try to set by default in any
 # category. For now, we only need the input tag.
 nodefault='<input>'
 
 ### end config ###
 
 # Create a lockfile to make sure that only one instance of the script
 # can run at any time.
 LOCKFILE=po2wml.lock
 
 if lockfile -! -l 60 -r 3 "$LOCKFILE"; 
 then
 	echo "unable to acquire lock" >2
 	exit 1
 fi
 
 trap "rm -f '$PWD/$LOCKFILE'" exit
 
 # Check if translation/projects/website exist, i.e. has been checked out
 if [ ! -d $podir ]
 then
 	echo "Have you remembered to check out translation/projects/website?"
 	exit 1
 fi
 
 # cd to the right directory so we can commit the files later
 cd "$wmldir"
 
 # We need to find the po files
 po=`find $podir -regex '^'$podir'/.*/.*\.po' -type f`
 
 # For every wml, update po
 for file in $po ; do
a54d1737
 
 	# Validate input and write results to a log file
 	validate_script="`dirname $wmldir`/translation/tools/validate.py"
5c4c2ee3
 	validate_log="`dirname $wmldir`/website-validate.log"
a54d1737
 	python "$validate_script" -i "$file" -l	"$validate_log"
f0e1bafa
 	
 	# Get the basename of the file we are dealing with
 	pofile=`basename $file`
 
 	# Strip the file for its original extension and the translation
 	# priority, and add .wml
 	wmlfile="`echo $pofile | cut -d . -f 2`.wml"	
 
 	# Find out what directory the file is in.
 	indir=`dirname $file`
 
 	# We also need to know what one directory up is
 	onedirup=`dirname $indir`
 
 	# We need to find out what subdirectory we are in
 	subdir=`dirname $file | sed "s#$onedirup/##"`
 
 	# And which language we are dealing with
 	lang=`dirname $indir | sed "s#$podir/##"`
 
 	# Time to write the translated wml file.
 	# The translated document is written if 80% or more of the po
b01071c7
 	# file has been translated. Example: Use '-k 21' to set this
 	# number down to 21%. Also, po4a-translate will only write the
 	# translated document if 80% or more has been translated.
 	# However, it will delete the wml if less than 80% has been
f0e1bafa
 	# translated. To avoid having our current, translated wml files
b01071c7
 	# deleted, convert the po to a temp wml first. If this file was
 	# actually written, rename it to wml.
f0e1bafa
 
b01071c7
 	# Convert translations to directories such as website/nb/.
 	function nosubdir {
f0e1bafa
 		# The location of the english wml file
 		english="$wmldir/en/$wmlfile"
 
5f016034
 		# Convert the translated file. Note that po4a will write the file and then delete it if less than 80% has been translated
 		po4a-translate -f wml -m "$english" -p "$file" -l "$wmldir/$subdir/$wmlfile" --master-charset utf-8 -L utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
b01071c7
 
 		# Check to see if the file was written
5f016034
                 if [ -e "$wmldir/$subdir/$wmlfile" ]
aeb6df64
 		then
b01071c7
                         # Remove last three lines in file
 			sed -i -e :a -e '$d;N;2,3ba' -e 'P;D' "$wmldir/$subdir/$wmlfile"
 
1336f98f
 			# If the file is mirrors.wml, include mirrors-table.wmi
 			if [ $wmlfile == "mirrors.wml" ]
 			then
 				sed -i 's/<!--PO4ASHARPBEGIN/#/' "$wmldir/$subdir/$wmlfile"
 				sed -i 's/PO4ASHARPEND-->//' "$wmldir/$subdir/$wmlfile"
 			fi
f9f0a80f
 
e4c80226
 			# If the file is an Arabic translation, include
 			# the right header, css, menu files and footer
f9f0a80f
 			if [ $subdir = "ar" ]
 			then
e4c80226
 				# Head
 				orig_head=`grep '#include "head.wmi"' "$wmldir/$subdir/$wmlfile"`
 				temp_head=`echo $orig_head | sed s@head.wmi@ar/head.wmi@`
 				new_head=`echo $temp_head 'STYLESHEET="css/master-rtl.css"'`
 				sed -i "s@$orig_head@$new_head@" "$wmldir/$subdir/$wmlfile"
 
 				# Side (not all files include this)
 				orig_side=`grep '#include "side.wmi"' "$wmldir/$subdir/$wmlfile"`
 				if [ -n "$orig_side" ]
 				then
 					new_side=`echo '#include "ar/side.wmi"'`
 					sed -i "s@$orig_side@$new_side@" "$wmldir/$subdir/$wmlfile"
 				fi
 
 				# Info (not all files include this)
 				orig_info=`grep '#include "info.wmi"' "$wmldir/$subdir/$wmlfile"`
 				if [ -n "$orig_info" ]
 				then
 					new_info=`echo '#include "ar/info.wmi"'`
 					sed -i "s@$orig_info@$new_info@" "$wmldir/$subdir/$wmlfile"
 				fi
 
 				# Footer
 				echo '#include "ar/foot.wmi"' >> "$wmldir/$subdir/$wmlfile"
 			else
 				# Include the English footer
 				echo '#include "foot.wmi"' >> "$wmldir/$subdir/$wmlfile"
f9f0a80f
 			fi
b01071c7
 		fi
 	}	
aeb6df64
 
5f016034
 	# Convert translations to directories such as website/torbrowser/nb/.
 	# Again, po4a will write the file and then delete it if less than 80% has been translated
b01071c7
 	function subdir {
 		# The location of the english wml file
                 english="$wmldir/$subdir/en/$wmlfile"
f849a6aa
 
b01071c7
 		# Convert the files
5f016034
 		po4a-translate -f wml -m "$english" -p "$file" -l "$wmldir/$subdir/$lang/$wmlfile" --master-charset utf-8 -L utf-8 -o customtag="$customtag" -o nodefault="$nodefault"
f849a6aa
 
b01071c7
 		# Check to see if the file was written
5f016034
 		if [ -e "$wmldir/$subdir/$lang/$wmlfile" ]
b01071c7
 		then
 			# Remove last three lines in file
 			sed -i -e :a -e '$d;N;2,3ba' -e 'P;D' "$wmldir/$subdir/$lang/$wmlfile"
 
e4c80226
 			# If the file is an Arabic translation, include the
 			# right header, css, menu files and footer
f9f0a80f
 			if [ $lang = "ar" ]
 			then
e4c80226
 				# Head
 				orig_head=`grep '#include "head.wmi"' "$wmldir/$subdir/$lang/$wmlfile"`
 				temp_head=`echo $orig_head | sed s@head.wmi@ar/head.wmi@`
 				new_head=`echo $temp_head 'STYLESHEET="css/master-rtl.css"'`
 				sed -i "s@$orig_head@$new_head@" "$wmldir/$subdir/$lang/$wmlfile"
 
 				# Side (not all files include this)
 				orig_side=`grep '#include "side.wmi"' "$wmldir/$subdir/$lang/$wmlfile"`
 				if [ -n "$orig_side" ]
 				then
 					new_side=`echo '#include "ar/side.wmi"'`
 					sed -i "s@$orig_side@$new_side@" "$wmldir/$subdir/$lang/$wmlfile"
 				fi
 
 				# Info (not all files include this)
 				orig_info=`grep '#include "info.wmi"' "$wmldir/$subdir/$lang/$wmlfile"`
 				if [ -n "$orig_info" ]
 				then
 					new_info=`echo '#include "ar/info.wmi"'`
 					sed -i "s@$orig_info@$new_info@" "$wmldir/$subdir/$lang/$wmlfile"
 				fi
 
 				# Footer
 				echo '#include "ar/foot.wmi"' >> "$wmldir/$subdir/$lang/$wmlfile"
 			else
 				# Include the English footer
 				echo '#include "foot.wmi"' >> "$wmldir/$subdir/$lang/$wmlfile"
f9f0a80f
 			fi
b01071c7
 		fi
 	}
 
 	# If $onedirup is equal to $lang, that means we do not have a
 	# subdirectory.
 	if [ $onedirup == $lang ]
 	then
 		# If the current subdirectory is "zh_CN" use "zh-cn" instead
 		if [ $subdir = "zh_CN" ]
 		then
 			subdir="zh-cn"
 			nosubdir
aeb6df64
 		fi
 		
b8c3ac64
 		# If the current directory is "nb" use "no" instead
 		if [ $subdir = "nb" ]
 		then
b01071c7
 			subdir="no"
 			nosubdir
b8c3ac64
 		fi
 
 		# If the current directory is "sv" use "se" instead
 		if [ $subdir = "sv" ]
 		then
b01071c7
 			subdir="se"
 			nosubdir
b8c3ac64
 		fi
 
aeb6df64
 		# Convert everything else
b8c3ac64
 		if [[ $subdir != "en" && $subdir != "zh_CN" && $subdir != "nb" && $subdir != "sv" ]]
f0e1bafa
 		then
b01071c7
 			nosubdir
f0e1bafa
 		fi
 	else
aeb6df64
 		# If the current language is "zh_CN" use "zh-cn" instead
 		if [ $lang = "zh_CN" ]
 		then
b01071c7
 			lang="zh-cn"
 			subdir
aeb6df64
 		fi
b8c3ac64
 
 		# If the current language is "nb" use "no" instead
 		if [ $lang = "nb" ]
 		then
b01071c7
 			lang="no"
 			subdir
b8c3ac64
 		fi
 
 		# If the current language is "sv" use "se" instead
 		if [ $lang = "sv" ]
 		then
b01071c7
 			lang="se"
 			subdir
b8c3ac64
 		fi
aeb6df64
 		
 		# Convert everything else
b8c3ac64
 		if [[ $lang != "en" && $lang != "zh_CN" && $lang != "nb" && $lang != "sv" ]]
f0e1bafa
 		then
b01071c7
 			subdir
f0e1bafa
 		fi
 	fi
 done