How to automatically add comments to your gettext strings when running xgettext

245 views Asked by At

So I use gettext in my php project to translate strings. However, in som cases, the context of a string is not obvious so I would like to add a comment to the string for the translator. However, it is not clear to me how to do this in a good way.

I would love to do it like this:

_("My text string","My comment that describe the string context for my translator")

When I code I use:

find ./app -iname "*.php" | xargs xgettext -d app/Locale/messages

To scan for new strings and then compile the messages.po file.

Is it possible to add comments to text strings like this? Or what is the best practice for adding comments for translators?

Best! Samuel

I haven't seen any similar solutions yet when googling...

2

There are 2 answers

3
Lauri Nurmi On

It is not possible quite the way you describe, but use e.g. --add-comments=TRANSLATORS for xgettext, and any comment nearby a _() call and beginning with TRANSLATORS: will be added to the output. See also the documentation.

0
Stas Trefilov On

Context-aware functions (pgettext family) are not available in php extension, so you have to either provide comments with -c|--add-comments command line option, or use a third-party libraries like vertilia/text that provide context-enabled translations and can also handle heredoc/nowdoc strings by keeping your gettext process intact.

Disclaimer: I am a developer of vertilia/text.