3

I want to put keywords and comments in my source file.

the keywords documentation for gettext says: if keywordspec is of the form ‘id:argnum...,"xcomment"’, xgettext, when extracting a message from the specified argument strings, adds an extracted comment xcomment to the message.

I couldn't find any samples to help me with this.

This is my X-Pedit-KeywordsList header,

"X-Poedit-KeywordsList: __;_ex;\n"

And this is a sample line in my php source code:

_ex("unlock_level", "Available at level #.")

I expect the output to be:

# "Available at level #."
msgid "unlock_level"

How should I edit my keywordslist header (and/or the source)?

Paiman Roointan
  • 514
  • 5
  • 17

3 Answers3

3

An example for the keyword spec (bourne shell syntax!) is:

xgettext --keyword='_ex:1,"my comment"' so.php

Unfortunately, this is not what you want. It produces this po entry:

#. my comment
#: so.php:3
msgid "unlock_level"
msgstr ""

The above command-line translates to "extract the first argument all all calls to _ex() as the msgid, and always add the comment 'my comment' to the PO entry". You can only specify which arguments are the singular, the plural, or the message context.

The X-POEdit-KeywordsList seems to be a custom header used by POEdit. It doesn't help you either.

You can kind of achieve the desired result by changing your sources to this:

<?
# TRANSLATORS: Available at level #.
_ex("unlock_level");
?>

Now invoke xgettext like this:

xgettext --add-comments=TRANSLATORS: --keyword=_ex so.php

You get this PO entry:

#. TRANSLATORS: Available at level #.
#: so.php:3
msgid "unlock_level"
msgstr ""

The option --add-comments=TRANSLATORS: has the effect that it adds comments that immediately precede a keyword iff that comment starts exactly with the string "TRANSLATORS:". You can exchange "TRANSLATORS:" with a string of your choice. You can also omit the argument to --add-comments and extract all comments that immediately precede keywords.

Not quite what you originally wanted but as close as you can get.

Guido Flohr
  • 1,871
  • 15
  • 28
2

Poedit supports translator comments. I ended up adding localization keys to my source file like this:

// TRANSLATORS: "Available at level #."
__("unlock_level")

And this is what I get in my po file by pressing the update button in Poedit:

#. TRANSLATORS: "Available at level #."
msgid "unlock_level"
Paiman Roointan
  • 514
  • 5
  • 17
2

I just came across this post because I was also looking for a way to do this, and just wanted to add that @Paiman Roointans' solution works because PoEdit uses the TRANSLATORS: tag as default tag for comment extraction via gettext.

This can actually be seen if you open PoEdit -> Preferences -> Extractors, then click on the little "+" button at the bottom left of the window, which will show you the command PoEdit fires by default for an extraction of your translation strings from the source file, which is sth like:

xgettext -L PHP --add-comments=TRANSLATORS: --force-po -o %o %C %K %F

where %o %C %K %F are the respective placeholders, for example %o for the output filename, %K for the keyword list (you specify in POEdits keyword mask), etc.

To change the default comment identifier in PoEdit, simply go to Translation -> Properties -> Click on "Advanced Extraction Preferences" under de "Translation Properties" tab of the window which then pops up. Normally the first field there should tell you the current string being used as comment identifying tag. Change that for example to mysamplecommentkey, and write a translation like this in your PHP code, for example:

/* mysamplecommentkey: This is a test */
gettext( "Translate Me" );

And your "Translate Me" will have the This is a test comment attached to it when extracting from the source code.

And, just another point: You should rather use a POT instead of a PO template file for your gettext localizations in PoEdit. Set all of your keyword extractors and / or comment identifier string, then extract from your source code into your .pot file, everytime when you update your translations.

Then, in all of your PO files, you simply hit update from POT-File in PoEdit, and it will correctly update all of your translations, including all of the comments for translators, which will all be drawn from the POT file directly too (so you better write these translator comments in a language that all of your translators most likely understand).

It took me some time to figure all of this out, and I would have saved tons of hours if I knew all of this from upon the beginning, so I wanted to share this here. If used correctly, PoEdit (already the free version of it) can save you a lot of time!

DevelJoe
  • 856
  • 1
  • 10
  • 24