Localizing text from XML files

Do you guys know any way I could automatically pick up strings from .xml files and store them in a .po / .pot file, just like PoEdit does with the ones from .php files?

The XML structure looks like this:

Read More
<?xml version="1.0" encoding="iso-8859-1"?>
<options>
  <page id="content" label="Content Options" icon="4">
    <section id="post" label="Content Options">
      <option id="title" type="checkbox" default="yes" label="Show post title"></option>
      <option id="comments" type="checkbox" default="yes" label="Show comments link"></option>
      <option id="thumb_size" type="select" default="default" label="Thumbnail Size">
        <value id="default" label="Default Size, (%s)" vars="default_size"></value>
        <value id="100x100" label="Small (100x100)"></value>
        <value id="150x150" label="Medium (150x150)"></value>
        <value id="200x200" label="Large (200x200)"></value>
      </option>

    </section>
  </page>
</options>

Strings within the “label” attribute should be detected by a gettext parser or something…

Related posts

Leave a Reply

2 comments

  1. You should use a localized XML format like XLIFF or TMX , there is a tool to convert XLIFF to .po.

    If you cannot change the source XML you have a few options but it really depends how your outputting this XML info:

    1. Parse the XML ( for label) and create a .po with gettext output for these strings (then possibly merge the .po files).
    2. Duplicate the XML and insert the _() gettext right into the XML, then parse it on output.

    Also have a look at this tool, and there are some command line python tools that can convert xml–>po I think, http://itstool.org/

  2. If you’d like to try itstool, you just need to give it a custom ITS rules file for your format. The following rules works for the snippet you posted:

    <its:rules
        xmlns:its="http://www.w3.org/2005/11/its"
        its:version="1.0">
      <its:translateRule translate="yes" selector="//@label"/>
    </its:rules>
    

    Save this as, say, options.its, then run itstool like this:

    itstool -i options.its -o somefile.pot somefile.xml
    

    After your translators have translated and given you a PO file (say, xx.po), compile it to an mo file, then merge using itstool:

    msgfmt -o xx.mo xx.po
    itstool -m xx.mo -i options.its -o somefile.xx.xml somefile.xml
    

    Check the documentation for more: http://itstool.org/documentation/