Software localization

How to Combine Two PO Translation Files Together

Are you implementing i18n with gettext and want to find out how to merge PO files? Here is what you need to know.
Software localization blog category featured image | Phrase

PO files are used in i18n specifically for the gettext tool for storing message translations for a specific locale.

If you maintain more than two PO files, and you want to combine them together, without introducing any conflicts, then you may need to use another tool to help with that process.

Merging PO Files

The msgcat tool that comes with the installation of the gettext tooling into our system offers such functionality. You can install the gettext tooling on Mac via homebrew, on Windows via pre-compiled libraries, and on Linux via the relevant package manager. Afterwards, you have access to more than 10 tools, including msgcat.

It’s a command-line tool with the following invocation signature:

msgcat [options] [inputfiles]...

To make this clearer with an example, let’s say we have two files, first.po and second.po, and we want to combine them together into a third.po file:

msgcat first.po second.po -o third.po

The tool offers several options for combining those files together. For example, we could employ the use of:

--use-first: to take the first available translation for each message even if it's empty and don’t merge them together,

--no-location: to strip filename or line comments,

-s, --sort-output: to sort all keys by msgid.

If you want to combine multiple msgids, we have an option to filter common messages based on a rule. For example:

--less-than=<NUM>: allow only messages with less than NUM conflicting definitions; by default, NUM is unlimited so it allows all messages to pass through.

--more-than=<NUM>: allow only messages with more than NUM conflicting definitions; this is used to extract multiple conflicting definitions in a new file for further processing; by default, NUM is 0.

Note that there is also the msgmerge tool that can combine two PO files together, but it has a 'nasty' side effect. When merging two files, the tool will only move translations and comments from the first file to the destination file while discarding any existing translations and comments of the second one. This is why it should be mainly used to update existing translations taken from a definition file into a destination file that may contain old translations.

Having said that, using the msgcat tool should be the preferred choice for merging two PO files together.

Are There Any Good Alternatives to Poedit Out There?

Poedit is a well-known desktop application that allows manipulating PO files in your computer environment, coming with both a free and pro version; the latter offers more advanced features, such as online suggestions and machine translation.

If you are not happy working with poEdit or you do not like the interface, there are quite some other options. Here, we will explore the most useful ones:

Vim + po.vim plugin

If you are a Vim power user, you can integrate PO automation tasks using the po.vim plugin. You can then use it in your terminal, and it will register several action helpers, such as moving to the next untranslated message, adding labels, or copying messages from different msgids. This plugin will save you time and effort to automate keystrokes.

Gtranslator

For GNOME users, there is a similar tool called Gtranslator, which offers a few benefits, editing in multiple tabs and translation memory among others. The development branch is actively maintained and hosted in GitLab. However, its major limitation is that you need to have installed GNOME on your platform so this option is more suitable for Linux users.

Software Localization Platform

Desktop applications had their time. Today, major SaaS providers offer tools that manage PO files in the cloud. This often includes more crucial and safer features, such as online backups, and the ability to transform messages into different formats.

Therefore, it is more safe and useful to use a software localization platform like Phrase. It offers support for PO files out of the box so when you sync those files, you have the option to export them into a different format.

Finally, if you are working with a large team of developers, designers, translators, and managers, and if productivity is crucial to timely deliver translatable content, then a software localization platform is the way to go.