Today, I had to convert an XLIFF file containing translations into a CSV file.
Since the file was quite extensive, I did not want to do this manually and looked for
a suitable program to handle the task.
The programs I found were all quite old and, unfortunately, no longer worked.
The file looked roughly like this:
<?xml version="1.0"?> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <file source-language="en-US" datatype="plaintext" original=""> <body> <trans-unit id="defaultTitle"> <source>defaultTitle</source> <target>The Example App</target> </trans-unit> <trans-unit id="whatIsThisApp"> <source>whatIsThisApp</source> <target>Help</target> </trans-unit> <trans-unit id="metaDescription"> <source>metaDescription</source> <target>This is "The Example App", a reference for building your own applications using Contentful.</target> </trans-unit> <!-- ... weitere Einträge --> </file> </xliff>
jq, yq, xq
The solution turned out to be quite simple after all. The "xq" tool, which I was already using anyway,
could convert the XML file to CSV without any trouble.
The xq program is part of yq, which is similar to jq.
Confused?
- jq -> For queries on JSON files
- yq -> For queries on YAML files
- xq -> For queries on XML files
On my Arch Linux, the program is easy to install via pacman.
sudo pacman -S yq
That puts xq on disk too.
The actual conversion then works with this one-liner:
cat messages.en-US.xlf | xq -r '.xliff.file.body."trans-unit"[] | [.source, .target] | @csv' > messages.en-US.csv
You can do other neat things with these programs too.
For example, extract all URLs from an XML sitemap.
curl https://magerun.net/post-sitemap.xml | xq -r '.urlset.url[].loc'
eat and fx
If you do more on the CLI, you should also take a look at eat and fx.
These are very useful helpers too.
With eat, you can convert content from various sources into JSON. The tool "eats" almost anything, so to speak.
For example, you can quickly turn the XML sitemap used above into JSON.
curl https://magerun.net/post-sitemap.xml | eat
And fx is similar to jq, but offers interaction. You can filter JSON data live or expand and collapse content. Very handy when you want to take a look at something.
Example:
cat composer.json | fx