gettext management
Anybody remember gettext?
A couple of years ago I investigated the localisation system for a project and actually came to like it pretty much.
Managing your .po and .mo files can be a hassle though.
That’s why I created these two scripts to help me handle them:
#!/usr/local/bin/bash
function createpot {
# create new .po templates (.pot)
local POT=$1
touch ./locale/${locale}.UTF-8/LC_MESSAGES/${POT}.pot
xgettext --default-domain=${DOMAIN} \
--output-dir="./locale/${locale}.UTF-8/LC_MESSAGES/" --language PHP \
--force-po --indent --strict --no-wrap \
--copyright-holder='inGen cvba' \
--msgid-bugs-address='bart@inGen.be' --output=${POT}.pot \
--add-location ${phpfile}
chmod -f g+rw ./locale/${locale}.UTF-8/LC_MESSAGES/${POT}.pot
}
function updatepo {
# update the original with the new potfile
local PO=$1
touch ./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.po
touch ./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.pot
msgmerge --update --backup=simple --force-po --indent --strict \
--no-wrap --quiet --add-location \
./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.po \
./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.pot
chmod -f g+rw ./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.po
}
function mergepot {
# merge the original po with the new pot
local PO=$1
local POT=$2
touch ./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.po
touch ./locale/${locale}.UTF-8/LC_MESSAGES/${POT}.pot
msgcat --output-file=./locale/${locale}.UTF-8/LC_MESSAGES/${POT}.pot \
--to-code=UTF-8 --use-first --force-po --indent --add-location \
--strict --sort-output \
./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.po \
./locale/${locale}.UTF-8/LC_MESSAGES/${POT}.pot
rm -f ./locale/${locale}.UTF-8/LC_MESSAGES/${PO}.po
chmod -f g+rw ./locale/${locale}.UTF-8/LC_MESSAGES/${POT}.pot
}
function backuppo {
# do a real backup of the existing pofiles
cp ./locale/${locale}.UTF-8/LC_MESSAGES/${DOMAIN}.po \
./locale/${locale}.UTF-8/LC_MESSAGES/pobackup/
}
for locale in nl_BE fr_BE en_GB
do
# generate .po messages
for phpfile in index.php
do
# the domain is the basename of the file
DOMAIN=`basename $phpfile .php`
backuppo
createpot ${DOMAIN}
updatepo ${DOMAIN}
done
done
and
#!/usr/local/bin/bash
# generate .mo messages of al .po files
for lang in nl_BE fr_BE en_GB
do
for pofile in ./locale/${lang}.UTF-8/LC_MESSAGES/*.po
do
rm -f ${profile/po/mo}
msgfmt ${pofile} -o ${pofile/po/mo} --strict --check --use-fuzzy
chmod -f g+r ${pofile/po/mo}
done
done
These scripts will not setup the working environment nor generate full headers for new .po files.
You will have to do this manually.
A corresponding working environment for the scripts in their posted configuration looks like this:
.
|-- images
| `-- favicon.ico
|-- includes
| `-- accept-to-gettext.inc
|-- index.php
|-- locale
| |-- en_GB.UTF-8
| | `-- LC_MESSAGES
| | |-- index.mo
| | |-- index.po
| | |-- index.pot
| | |-- index.po~
| | `-- pobackup
| | `-- index.po
| |-- fr_BE.UTF-8
| | `-- LC_MESSAGES
| | |-- index.mo
| | |-- index.po
| | |-- index.pot
| | |-- index.po~
| | `-- pobackup
| | `-- index.po
| `-- nl_BE.UTF-8
| `-- LC_MESSAGES
| |-- index.mo
| |-- index.po
| |-- index.pot
| |-- index.po~
| `-- pobackup
| `-- index.po
|-- makemo
|-- makepo
`-- styles
`-- index.css
Mind the accept-to-gettext.inc file, a great script written by Wouter Verhelst to convert information in HTTP ‘Accept-*’ headers to gettext language identifiers.