PO/POT file operations

Last updated on
25 October 2021

Here are some commands that can be used to combine several PO translation files into one larger file, merge in translations from another source, and other operations. Most of the commands will run only on MacOS/Linux command lines, and require the gettext package to be installed.

Merging translations

Merging is the process of taking your existing translations and merging in new translations for the same strings from a new file, without losing your original translations. There are several ways to merge translations:

Combining files on Unix/Linux/MacOS command line

If you have a number of PO files in a single directory with different strings in them, and you have the gettext project installed on your computer, you can run a command similar to this in the directory to make one big file:

$ msgcat --use-first general.po [^g]*.po | msgattrib --no-fuzzy -o nl.po

This command assumes that the main file you have is called general.po (its headers will be used for the output), and that your output language is Dutch, so that the final file should be called nl.po. You will need to make appropriate changes for your situation.

If msgcat does not work for some reason, you can try a command like this to combine all PO files in subdirectories (for instance, starting at the root of your Drupal installation):

cat `find . -name "*.nl.po"` > nl.po

Combining files on Windows command line

If you have a number of PO files in a single directory, you can open up a command window, change to that directory, and run the following command to make them into one big file called nl.po (change the file name to your language):

for %f in (*.po) do type "%f" >> module.po

Removing untranslated strings

Here is a command that will remove the untranslated strings from a PO file (suggested to run before importing it):

msgattrib -s --no-location --translated huge.po > clean_file_for_import.po

Removing duplicates

Here is a command that will remove duplicate strings from a PO file:

msguniq -u --no-location --strict with_duplicates.po > clean_file_for_import.po

Finding "fuzzy" translations

When there's a new version of a module or core, some of the source strings will have minor changes from the previous version, such as corrections of grammar and typos. For these strings, it's useful to have the translations for the old strings available for reuse. However, since the old translations are not exact translations of the new text, the best thing to do is to mark them as "fuzzy". Here's how to make such a file and import it into the localization server:

  1. Export a template file for the new version, and a translation file for the old version(s). See Downloading and importing translation files for details.
  2. Run the command msgmerge on the two files. This will result in a translation file for the new version, with all matching and almost-matching strings from the old version.
  3. Run the command msgattrib with the parameter --fuzzy. This removes all the non-fuzzy strings, and then removes the "fuzzy" tag from all the remaining strings.
  4. Review the file in a .po editor to remove the most obvious wrong suggestions. Some fuzzy matches will be completely wrong. For instance, msgmerge may suggest "field" as a fuzzy match for "file".
  5. Import the resulting file to the localization server as suggestions. See Downloading and importing translation files for details.

Separating a PO file into per-project pieces

If you export a PO file from a working Drupal site, it will contain all the translations for Drupal core and all of your modules, themes, and profiles. If you want to contribute your translations to the localization server, you will need to split the file by project (Drupal core, and each contributed module/theme/profile). Here's how to do it, for each project you want to import:

  1. Export translations from the localization manage languages screen (export sub-tab). This will make one large "compendium" PO file.

  2. Download a POT file for the project. See Downloading and importing translation files for details.

  3. Run this command to extract just the strings in this POT file from your compendium:

    msgmerge --compendium compendium.po -o projectname-language.po /dev/null projectname.pot

  4. Repeat for additional projects.

Help improve this page

Page status: No known problems

You can: