My l10n makefile

To easily gather .pot files, combine them with existing .po files and converting them to .mo files, I created this makefile for use with automake:

.PHONY: _site.pot backup
POFILES:=$(wildcard *.po)
NOW=$(shell date +%s)
 
all:    backup
        make $(POFILES:.po=.mo)
 
%.mo:   %.po
        echo Generating $@
        msgfmt -o $@ $< || echo -e "\033[33;1m $@ is wrong \033[0m"
 
%.po:   _site.pot
        if [ -e $@ ]; then \
        mv $@ $(@:.po=.potmp) ; msgmerge --no-fuzzy-matching $(@:.po=.potmp) _site.pot > $@ ; \
        rm $(@:.po=.potmp) ; \
        else \
        echo Creating new file ; cp _site.pot $@ ; \
        fi
 
_site.pot: 
        find /var/www/html -iname "*.php" | grep -v wpcode > my-php-files
        xgettext --from-code=utf-8 -d site -f my-php-files --keyword=_e --keyword=__ -o - | sed "s/CHARSET/UTF-8/" > _site.pot
        rm my-php-files
 
backup:
        mkdir -f .backup
        tar zcf .backup/backup-$(NOW).tgz *.po Makefile

Requirements

You'll need GNU Gettext to run the commands of this makefile and of course, you'll need the make command to be able to execute the makefile. On my CentOS, both are installed using the command yum install gettext make

Let's break it down

The first lines define some values. The .PHONY variable makes the make command think that _site.pot should always be updated and that backup isn't a file to be created either. The POFILES gathers the names of all *.po files, which we are going to use later. And the NOW will contain the date in Unix epoch format:

.PHONY: _site.pot backup
POFILES:=$(wildcard *.po)
NOW=$(shell date +%s)

Then the first target block depends on creating a backup first and then building a .mo file for every .po file available.

all:    backup
        make $(POFILES:.po=.mo)

This macro generates a .mo file from the .po file. The target is defined as $@, the source as $<

%.mo:   %.po
        echo Generating $@
        msgfmt -o $@ $< || echo -e "\033[33;1m $@ is wrong \033[0m"

The next target requires _site.pot (po template) as a dependency. If a .po file already exists, it is merged with the newly gathered .pot file. Otherwise the .pot file is simply copied to a .po file.

%.po:   _site.pot
        if [ -e $@ ]; then \
        mv $@ $(@:.po=.potmp) ; msgmerge --no-fuzzy-matching $(@:.po=.potmp) _site.pot > $@ ; \
        rm $(@:.po=.potmp) ; \
        else \
        echo Creating new file ; cp _site.pot $@ ; \
        fi

The next target is required by %.po. It finds all *.php files in /var/www/html (you may want to change that) and looks for __("...") or _e("..."). All hits are saved in the _site.pot file.

_site.pot: 
        find /var/www/html -iname "*.php" | grep -v wpcode > my-php-files
        xgettext --from-code=utf-8 -d site -f my-php-files --keyword=_e --keyword=__ -o - | sed "s/CHARSET/UTF-8/" > _site.pot
        rm my-php-files

The backup target creates a directory .backup and generates a backup in it. Great for when something failed.

backup:
        mkdir -p .backup
        tar zcf .backup/backup-$(NOW).tgz *.po Makefile
© GeekLabInfo My l10n makefile is a post from GeekLab.info. You are free to copy materials from GeekLab.info, but you are required to link back to http://www.geeklab.info

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Leave a Reply