Recursive xgettext?
Asked Answered
O

5

12

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.

Ostracod answered 8/12, 2009 at 2:32 Comment(0)
O
22

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!

Ostracod answered 8/12, 2009 at 2:58 Comment(7)
well it's not working for me, but you showed me a way for possibility otherwise would be accepted and live with it as if there is no better way that thisPard
Hi, Nice work, but becarefull to add -j at end of the command, unless it wont worksDimitrovo
This is wrong, because if there are too many files to fit on one commandline, xargs will run xgettext multiple times, causing the earlier invocations to be clobbered. Just adding -j isn't right, either, since you need to delete the file first or you'll join with the old data.Sofiasofie
And adding -j when a file doesn't exist throws an error. xgettext isn't very mature...Sofiasofie
See also answer https://mcmap.net/q/909958/-how-to-use-xgettext-how-to-parse-directory with --files-from option if many files are parsedKraft
@GlennMaynard I am trying to extract gettexted labels from my project which has more than 3,000 .php files and yes some files are ignored. How can I get ALL the gettexted labels? Thanks in advanceNiles
@Niles Please post your question as a question. Thanks.Sofiasofie
G
8

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
Gambill answered 20/2, 2011 at 23:30 Comment(1)
Some warning just in case: pygettext.py does not support the option -f.Cauline
P
5

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).

Plow answered 25/9, 2016 at 22:57 Comment(0)
D
0

Here's a solution for Windows. At first, install gettext and find from the GnuWin32 tools collection.

You can run the following command afterwards:

find /source/directory -iname "*.php" -exec xgettext -j -o /output/directory/messages.pot {} ;

The output file has to exist prior to running the command, so the new definitions can be merged with it.

Detrusion answered 10/12, 2009 at 16:20 Comment(1)
Perhaps installing and using Cygwin? I know it's not a native solution, but should get you the same result.Ostracod
S
0

This is the solution I found for recursive search on Mac:

xgettext -o translations/messages.pot --keyword=gettext `find . -name "*.php"`

Generates entries for all uses of method gettext in files whose extension is php, including subfolders and inserts them in translations/messages.pot .

Stubstad answered 5/11, 2015 at 20:12 Comment(1)
Leave a little descriptive textScaliger

© 2022 - 2024 — McMap. All rights reserved.