4
votes

How to configure poedit to extract strings from xml file?

I have Zend Framework navigation items in .xml like this:

<entry-i>
    <label>Text to translate</label>
    <params>
       ...
    <params>
<entry-i>

And I want poedit to read just messages from <label>s.

5

5 Answers

11
votes

I have been searching for a solution as well, and I have just gotten it to work!

In Poedit (I have 1.4.2), add a new parser (Edit > Preferences) with the following properties:

  • Language: XML
  • List of extensions separated by semicolons (e.g. .cpp;.h): *.xml
  • Parser command: xgettext --force-po -o %o %C %K %F -L glade
  • An item in keywords list: -k%k
  • An item in input files list: %f
  • Source code charset: --from-code=%c

In your translation project, add label and title to your keyword list and update the catalog.

3
votes

The above advice to abuse the Glade extractor to parse non-Glade XML files is misguided. It’s never going to work well (case in point: some comments around here). Of course, it was better than nothing back in 2010.

Starting with gettext 0.19.7 (bundled with Poedit since 1.8.7), there’s a better way: there’s now builtin support in gettext for custom XML files via ITS rules.

The best way to extract strings from a custom XML file is to

  1. Add a custom extractor with your extension, specifying standard gettext invocation, without the -L glade bit.
  2. Write ITS rules for your file format.
  3. Put them in the location of other .its and .loc files in Poedit’s installation.
2
votes

For anyone running into problems with the configuration for Poedit on windows, specifically if you get an error message saying that glade and expat are not available, replace the supplied xgettext.exe with current one from the gnuwin32 project:

http://gnuwin32.sourceforge.net/packages/gettext.htm

You need to download the binaries and the dependencies. However, only the binary xgettext.exe must be extracted and related files (just run it and it will tell you what is missing)

0
votes

Looks like PoEdit does not support XML yet.

I have created a little php script, to extract the labels to .php file, which PoEdit does understand.

$xml = simplexml_load_file("../application/configs/navigation.xml") 
   or die("Error: Cannot open XML file");

echo '<?';
foreach($xml->xpath('//label') as $label){
  echo 'echo _("'.$label.'");'. PHP_EOL;
}
0
votes

It worked great!! I found the problem about "glade not supported" using Poedit 1.4.6 in Windows 7 but I fixed by downloading last gnuwin32 binaries and dependencies as user496209 said. Don't download the complet package because PoEdit comes with its own gettext library, so just donwload binaries and dependencies and replace the requested files into the poedit folder.