Recursively apply the msgfmt command to all .po files in directory with find -exec

I have the following directory structure:

webiste/
    locale/
        en/
             LC_MESSAGES/
                     translation.po
        de/
            LC_MESSAGES/
                     translation.po
        fr/
            LC_MESSAGES/
                     translation.po

I am using the find command to get to all the .po files and apply the msgfmt command to them.

So when I run this command:

find . -name \*.po

The output is

./locale/fr/LC_MESSAGES/translation.po
./locale/de/LC_MESSAGES/translation.po
./locale/en/LC_MESSAGES/translation.po

Just as expected but now I when I add this:

find . -name \*.po -exec msgfmt "{}" \;

It outputs nothing and it does not create the .mo files it is supposed to (If I do it to each file individually the msgfmt command works as expected).

This is weird since if I change the command to this:

find . -name *.po -exec echo "{}" \;

It outputs the names of the files correctly.

I've sort of solved it like this:

find . -name \*.po -exec msgfmt "{}" -o {}.mo \;

But it outputs a translation.po.mo

How can this be done?


How about -execdir?

This differs from -exec in that it runs the command from the same directory that it finds the files (rather than the current working directory). Given that you're only going to find one translation.po file in your LC_MESSAGES directories, we can lock things right down and not have to fuss around trying to snap the extension off the end of the path.

find . -name \*.po -execdir msgfmt translation.po -o translation.mo \;

I am using something like this:

find . -name \*.po -execdir sh -c 'msgfmt "$0" -o `basename $0 .po`.mo' '{}' \;

This also removes *.po extension and add *.mo extension.

More generaly (file names with spaces):

find locale_dir/ -name \*.po -print -execdir sh -c 'msgfmt -f -o "$(basename "$0" .po).mo" "$0"' '{}' \;