12

I'm doing i18n for a php project using gettext. I'd like to use the automatic comment feature to give hints to translators when translating long phrases replaced by id. What I want to obtain is the following po file

#: full-path-to-file/index.phtml:3
#. a very long text which should replaced by _('foobar')
msgid "foobar"
msgstr ""

In this way the translator can see what he should translate when he see the key foobar using POEdit or some analogue tool in the programmer comment box.

I've tried with this code but it doesn't work

<?php
/// TRANSLATORS: a very long text which should replaced by _('foobar')
_('foobar');
?>

Am I missing something or automatic comments just don't work for php?

Even Wikipedia mentions this feature, I've tried to copy their example in a C file, but I cannot get it working even with C. The command line I've used is

xgettext -C -o - main.c

But the generated output is

#: main.c:16
#, c-format
msgid "My name is %s.\n"
msgstr ""

So I'm definitely missing something, should I use any xgettext flag or particular version to enable this feature.

Fabio
  • 18,856
  • 9
  • 82
  • 114
  • And in case you try to extract from javascript using --language=Python, append a hash # to the comment start, meaning you start comments like //#, this way xgettext will extract the translation comments correctly. – Radu Maris May 28 '15 at 19:28

1 Answers1

12

To make xgettext extract comments from your source, you need to pass an argument to tell it what comments to look for.

From the documentation:

-c[tag]
--add-comments[=tag]

Place comment blocks starting with tag and preceding keyword lines in the output file. Without a tag, the option means to put all comment blocks preceding keyword lines in the output file.

Passing -c/ or --add-comments=/ as an argument will make it recognize the "triple slash" format.

John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • @John Flatness - any idea why this doesn't work with .js files in Python mode? More details here: http://stackoverflow.com/questions/12052844/extract-translator-comments-with-xgettext-from-javascript-in-python-mode – ragulka Aug 21 '12 at 10:22
  • 2
    Note that if you use `-c/` to up comments such as `/// hint to translator`, it will also pick `// x/y is always > 2 here`, too. The xgettext will emit `#. y is always > 2 here` into the .pot file for this example. – Mikko Rantalainen Nov 21 '18 at 10:38