Personal tools
You are here: Home Membres jordinas's Home Plone i18n zpt2pot
Document Actions

zpt2pot

Version 1.5 of my Bash script to drive the zpt2pot.xsl transformation.

Click here to get the file

Size 4.4 kB - File type text/plain

File contents

#!/bin/bash
# $Id: zpt2pot,v 1.5 2005/07/15 08:21:22 joan Exp joan $

########################################################################
# zpt2pot - A gettext extractor for Zope Page Templates
#
# Dependencies:
#	xsltproc	-- package libxslt
#	msgcat		-- package gettext
#	msguniq		-- package gettext
#	zpt2pot.xsl -- the XSLT transformation who does the real work
#
# Author:
#	Joan Ordinas <jordinas at gmail dot com>
#
# License:
#
# The source of this software is public domain and provided here "as is". I
# don't feel like commenting it more but if you have problems then feel free to
# ask me. If you derive your own software from the source or put a part of the
# source into your own software, please, give me a credit and send a copy to me.
#
# The author accepts no liability for any damage or data loss caused by this
# software.
########################################################################

########################################################################
# Environment and functions
########################################################################

PROGRAM=${0##*/}

shopt -s expand_aliases

alias integer='typeset -i'

function usage
{
	echo 1>&2 "Usage: $PROGRAM [OPTION]... template..."
	echo 1>&2 "Try '$PROGRAM -h' for more information."
	exit 2
}

function die
{
	echo 1>&2 "$PROGRAM: $@"
	exit 1
}

function help
{
	cat 1>&2 <<EOT
Usage: $PROGRAM [-c] [-d domain] [-e] [-h] [-o output-file] [-w] template...
Extract i18n translatable strings from given ZPT input template files.

Options:
  -c              Only check templates, and don't generate POT file.
  -d domain       Use <domain>.pot for output (instead of 'messages.pot').
  -e              Don't stop on errors.
  -h              Show this help.
  -o output-file  Write output to specified file.
  -w              Don't emit warnings.

Examples:
	$PROGRAM -d plone about.pt
	$PROGRAM -o about.pot about.pt
	$PROGRAM -d plone *.pt
	$PROGRAM -c about.pt

By default output goes to file 'messages.pot'. If this file exists it is
destroyed.

Report bugs to <jordinas at gmail dot com>.
EOT
	exit 1
}

########################################################################
# Parse arguments
########################################################################

[[ $PROGRAM == zpt2pot ]] || die "Script name must be 'zpt2pot'"

integer cflag=0 eflag=0 tflag=0 wflag=0
typeset dflag='' oflag='' output='messages.pot'

while getopts ':cd:eho:Tw' opt
do
	case $opt in
		c)	cflag=1; eflag=1;;
		d)	dflag=$OPTARG;;
		e)	eflag=1;;
		h)	help;;
		o)	oflag=$OPTARG;;
		T)	tflag=1;;	# internal and not documented flag
		w)	wflag=1;;
		\?)	usage;;
	esac
done

shift $(( OPTIND - 1 ))
(( $# > 0 )) || usage
[[ -n $dflag && -n $oflag ]] && die 'Options -d and -o are not compatibles'

TRANSFORM=${0}.xsl
[[ -r $TRANSFORM ]] || die "XSLT file '$TRANSFORM' not found"

[[ -n $oflag ]] && output=$oflag
[[ -n $dflag ]] && output=${dflag}.pot

########################################################################
# Build command
########################################################################

typeset -a PARAMETERS

if (( cflag )); then
	alias xp='xsltproc 1>/dev/null'
else
	alias xp='xsltproc'
fi
(( eflag )) && {
	PARAMETERS[${#PARAMETERS[@]}]='--stringparam'
	PARAMETERS[${#PARAMETERS[@]}]='on-errors-terminate'
	PARAMETERS[${#PARAMETERS[@]}]='no'
}
(( wflag )) && {
	PARAMETERS[${#PARAMETERS[@]}]='--stringparam'
	PARAMETERS[${#PARAMETERS[@]}]='emit-warnings'
	PARAMETERS[${#PARAMETERS[@]}]='no'
}
(( !tflag )) && {
	PARAMETERS[${#PARAMETERS[@]}]='--stringparam'
	PARAMETERS[${#PARAMETERS[@]}]='creation-date'
	PARAMETERS[${#PARAMETERS[@]}]=$(date -Iseconds)
}
[[ -n $dflag ]] && {
	PARAMETERS[${#PARAMETERS[@]}]='--stringparam'
	PARAMETERS[${#PARAMETERS[@]}]='user-domain'
	PARAMETERS[${#PARAMETERS[@]}]=$dflag
}

########################################################################
# Extract strings
########################################################################

trap 'rm -f *.pot-$$' EXIT

for zpt
do
	[[ -r $zpt ]] || die "ZPT file '$zpt' not found"

	pot=${zpt}.pot-$$

	xp --novalid \
		"${PARAMETERS[@]}" \
		--stringparam input-filename "$zpt" \
		$TRANSFORM $zpt > $pot

	if (( $? > 0 )); then
		die "Detected error processing template $zpt"
	fi

	msguniq --no-wrap --output-file $pot $pot
done

if (( !cflag )); then
	[[ -e $output ]] && rm -f $output
	msgcat --no-wrap --output-file $output *.pot-$$
fi

exit 0

# vim:syntax=sh:sw=4:ts=4:ai