0

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 Answers2

1

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Lauri Nurmi
  • 566
  • 1
  • 3
  • 14
  • Aha, so if I runt xgettext -add-comments=TRANSLATORS and in my code do something like this _("Home" /* TRANSLATORS: This is the home button. */) Did I understand it correctly? – Samuel Sylvander Apr 10 '23 at 05:34
  • I have always inserted the comment right before the `_()` call, but possibly it can be within `_()` as well. – Lauri Nurmi Apr 10 '23 at 10:12
  • Nah that didn't work actually. As you said, it had to be before to be added to the correct string. – Samuel Sylvander Apr 12 '23 at 05:08
0

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Stas Trefilov
  • 173
  • 1
  • 6