I'm adding internationaliztion to a Django template site, and while doing it I made lots of little syntax errors. So I wrote a little bash script that'll do through all the .html template files and look for some simple mistakes. Just run it in the base of your django project and it'll print warning messages.
#! /bin/bash
# Simple Django i18n template syntax checker.
# Checks for some common mistakes
# - Missing {% load i18n %}
# - {% trans '....\n (trans spread across lines)
# - {% blocktrans with count ...%} (extra with)
# - {% blocktrans VARIABLE ... %} (missing with)
# - {% trans 'in your browser's address bar' %} (' inside a single quoted string)
# - {% blocktrans X as Y A as B %} (missing 'and')
find -path '*/templates/*' ! -path '*/.svn/*' -type f -name '*.html' ! -path '*/media/*' | while read TEMPLATE_FILE ; do
grep -q 'trans ' $TEMPLATE_FILE && ( grep -q 'load.*i18n' $TEMPLATE_FILE || echo -e "Missing {% load i18n %}\t$TEMPLATE_FILE" )
grep -o '{% trans.*$' $TEMPLATE_FILE | grep -qv '{% trans.*%}' && echo -e "{% trans %} spread across lines\t$TEMPLATE_FILE"
grep -q 'blocktrans with count' $TEMPLATE_FILE && echo -e "blocktrans with count\t$TEMPLATE_FILE"
pcregrep -q 'blocktrans (?!with)(?!%})(?!count)' $TEMPLATE_FILE && echo -e "'blocktrans VARIABLE' should be 'blocktrans with VARIABLE'\t$TEMPLATE_FILE"
pcregrep -q "{% ?trans '[^%]*?'[^%]*?' ?%}" $TEMPLATE_FILE && echo -e "Mismatched ' in {% trans %}\t$TEMPLATE_FILE"
if [[ $( pcregrep -o '{% ?blocktrans.*?%}' $TEMPLATE_FILE | grep with | perl -pe 's/".*?"//' | pcregrep -v '{% ?blocktrans +with +[\w\|\._:]+? +as +[\w_]+?( +and +[\w+\|\._:]+? +as +[\w_]+?)* ?%}') ]] ; then echo -e "Missing 'and' between blocktrans variables\t$TEMPLATE_FILE" ; fi
done
Download raw script