gettextxgettext

Overwrite output filename of xgettext


I am using xgettext with standard input because the input is not available in a file. However, I'd like it to output a filename I specify as a comment above each string.

Current behaviour

#: Standardinput:13
msgid "User"
msgstr ""

#: Standardinput:13
msgid "Invite"
msgstr ""

#: Standardinput:14
msgid "Group"
msgstr ""

Expected behaviour

If I could set a filename to path/to/file.txt, it should output this instead:

#: path/to/file.txt:13
msgid "User"
msgstr ""

#: path/to/file.txt:13
msgid "Invite"
msgstr ""

#: path/to/file.txt:14
msgid "Group"
msgstr ""

I read every option that I can set in the docs and found nothing about it.


Solution

  • The #: path/to/file.txt:14 text is called "location" and the most control you have over this is the --no-location and --add-location flags. See xgettext.c:xgettext_open() for the source code reason why.

    Meanwhile, the obvious thing to do is take your input from standard in, pipe the output to standard out, substitute manually, then store in a target PO file. Example:

    xgettext -k_ -Lc -o- - < hello.c \
        | sed 's@#: standard input:@#: path/to/file.c:@g' \
        > messages.po
    

    Obviously change the patterns inside the sed call to match your xgettext's format and the file you want to represent.

    Less obvious is to symlink standard in to a file name that looks like your target. Example:

    $ ln -s /dev/stdin file.c
    $ echo 'int main() { printf(gettext("Hello World\n")); return 0; }' \
          | xgettext --omit-header -o- file.c
    #: file.c:1
    #, c-format
    msgid "Hello World\n"
    msgstr ""
    

    Being able to name, for output purposes, the file when input is standard in seems like a reasonable feature. I suggest opening a request at GNU.