Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive xgettext?

How can I compile a .po file using xgettext with PHP files with a single command recursively?

My PHP files exist in a hierarchy, and the straight xgettext command doesn't seem to dig down recursively.

like image 285
neezer Avatar asked Dec 08 '09 02:12

neezer


3 Answers

You cannot achieve this with one single command. The xgettext option --files-from is your friend.

find . -name '*.php' >POTFILES
xgettext --files-from=POTFILES

If you are positive that you do not have too many source files you can also use find with xargs:

find . -name "*.php" -print0 | xargs -0 xgettext

However, if you have too many source files, xargs will invoke xgettext multiple times so that the maximum command-line length of your platform is not exceeded. In order to protect yourself against that case you have to use the xgettext option -j, --join-existing, remove the stale messages file first, and start with an empty one so that xgettext does not bail out:

rm -f messages.po
echo >messages.po
find . -name "*.php" -print0 | xargs -0 xgettext --join-existing

Compare that with the simple solution given first with the list of source files in POTFILES!

Using find with --exec is very inefficient because it will invoke xgettext -j once for every source file to search for translatable strings. In the particular case of xgettext -j it is even more inefficient because xgettext has to read the evergrowing existing output file messages.po with every invocation (that is with every input source file).

like image 100
Guido Flohr Avatar answered Nov 12 '22 01:11

Guido Flohr


Got it:

find . -iname "*.php" | xargs xgettext

I was trying to use -exec before, but that would only run one file at a time. This runs them on the bunch.

Yay Google!

like image 30
neezer Avatar answered Nov 12 '22 02:11

neezer


For WINDOWS command line a simpe solution is:

 @echo off
echo Generating file list..
dir html\wp-content\themes\wpt\*.php /L /B /S > %TEMP%\listfile.txt
echo Generating .POT file...
xgettext -k_e -k__ --from-code utf-8  -o html\wp-content\themes\wpt\lang\wpt.pot -L PHP --no-wrap -D html\wp-content\themes\wpt -f %TEMP%\listfile.txt
echo Done.
del %TEMP%\listfile.txt
like image 8
Antonio Avatar answered Nov 12 '22 00:11

Antonio