Rene Saarsoo
Rene Saarsoo

Reputation: 13927

Gettext .po files under version control

Currently using Gettext on a project and the .po files are nicely kept under version control.

PO files of course contain translations, but in addition to that they also contain some metadata - information about the exact files and line numbers where the translatable strings are located.

The problem is that each time you update the PO files the metadata changes a whole lot more than the actual translations. This makes it really hard to later see from version control diff what actually was changed - you just see a myriad of changes to file names and line numbers. Like that:

- #: somefile.js:43
- #: somefile.js:45
- #: somefile.js:118
+ #: somefile.js:203
+ #: somefile.js:215
  msgid "Translate me please"
  msgstr "Tõlgi mind palun"

- #: somefile.js:23
- #: somefile.js:135
+ #: otherfile.js:23
+ #: otherfile.js:135
  msgid "Note"
  msgstr "Märkus"

- #: andThatFile.js:18
  #: orThisFile.js:131
- msgid "Before I was like this"
- msgstr "Selline olin ma enne"
+ msgid "I happen to be changed"
+ msgstr "Paistab, et mind muudeti"

Of course, a simple fix would be to just disable the generation of filename/linenumber comments in xgettext output. But I actually find those file names to be quite useful hints when translating.

I surely cannot be the only one who doesn't like the diffs of his PO files. Suggestions?

Upvotes: 41

Views: 5106

Answers (4)

Peter Eisentraut
Peter Eisentraut

Reputation: 36759

The gitattributes/textconv approach is the right way to go. I'd like to offer a better solution regarding to tools to do the preprocessing.

In .gitattributes:

*.po    diff=po

In .gitconfig:

[diff "po"]
    textconv=msgcat --no-location --no-wrap --sort-output

msgcat from the gettext package is a useful tool there. It has a number of options you can play with. The option --no-location is especially what you want to filter out the line number differences. The other options might be useful if xgettext and/or msgmerge and/or your editor keep reformatting the strings in annoying ways. (In that case, it would also be good to pass those same options to those tools, and reconfigure your editor.)

Upvotes: 25

Alex Brown
Alex Brown

Reputation: 42932

A simple fix would be to apply a grep filter to remove comment metadata from the viewed diff. You can either do this to the output of the version control diff utility:

myVersionControl diff REV1 REV2 filea | grep -v '^..#'

or you may be able to instruct the version control diff utility to ignore these before it makes the comparison, which will likely result in a more reliable and prettier output:

I don't know what version control system you use, but git (for example) allows you to preprocess the input to diff and remove the comment lines for certain file types (thanks VonC), see man gitattributes and search for Performing text diffs of binary files. Here's the body of a sample script to save as /usr/local/bin/strippocomments which will do that:

grep -v '^#:' $1

You can then tell git to use this script to preprocess po files, by adding the following to the file .git/info/attributes in your repository:

*.po diff=podiff

and to the file .git/config in your repository:

[diff "podiff"]
    textconv = /usr/local/bin/strippocomments

Using git diff should then not include any lines starting with #:.

Note that the diffs generated from git diff using this approach should not be used for patching - but git format-patch will still use the default diff, so patches generated for emailing will still be ok.

Upvotes: 28

VonC
VonC

Reputation: 1328602

You could look at the different options offered by a custom diff a .gitattribute file, like specifying a special diff for po files

[diff "mypodiff"]
    command = mypodiff
*.po   diff=mypodiff

with mypodiff a script calling any diff tool able to filter out the line that you wa wnt

Upvotes: 7

bialix
bialix

Reputation: 21503

GNU gettext package has numerous useful utilities to perform various tasks with PO files. There is msgcmp to compare two PO files, msgcomm to select common/unique messages, msgattrib to select/filter/transform existing PO files. Depends on what you actually need from diff of PO file, I think you need to use either msgattrib or msgcomm.

If you need to just compare two PO files without comments about file/line then simple script to grep and save in temp dir your old and new PO files would be sufficient.

Upvotes: 8

Related Questions