How to automatically remove unused gettext strings?
Asked Answered
K

2

8

I have a web application where I have several translations using gettext. But now I have done a major rewrite of the application and while most of the strings are the same, there are also quite a few that were changed significantly or removed.

I use xgettext to create the .po files and it's great to be able to find new strings in the code and add them to the .po files, but I don't see an option to remove strings that aren't found in the code.

Is there an automated way to remove the strings from the .po files that are no longer in the code?

Kennard answered 3/5, 2012 at 16:54 Comment(1)
It's not automated, but my work around is to run xgettext twice. The first run I use the parameter --no-location. The second run puts the location back on the files. Now the text strings that are no longer used do not have a location and can be easily identified for deletion.Kennard
P
10

Supposing you have a fresh PO template file that contains only strings that should be in the final PO catalog in a file called messages.pot, and target PO catalog in messages.po, you can do this:

msgattrib --set-obsolete --ignore-file=messages.pot -o messages.po messages.po

This marks any obsolete strings. The --set-obsolete switch tells msgattrib to apply the obsolete flag to all strings in the target file, and --ignore-file tells it to ignore strings that appear in specified file (the PO template in our case).

If you also want to completely remove them, there's one more step:

msgattrib --no-obsolete -o messages.po messages.po

You can read more about msgattrib command here.

Peyton answered 27/11, 2014 at 19:37 Comment(0)
S
-1

Сan so

find . -name '*.po' -print0 | while read -d '' -r file; do msgattrib --output-file="$file" --no-obsolete "$file"; done
Sloan answered 20/10, 2014 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.