A use-case for gettext_noop
It can be useful in certain situations. For instance, let's say you've got an array of allowed values for a drop-down in a form:
$allowed_values = ["red", "orange", "blue"];
In the form, you want the user to see the translated values, but for the request to submit the untranslated values:
<select name="color">
<?php foreach ($allowed_values as $allowed_value) : ?>
<option value="<?= htmlspecialchars($allowed_value) ?>">
<?= htmlspecialchars(_($allowed_value)) ?>
</option>
<?php endforeach ?>
</select>
Unfortunately, xgettext
can't find the translatable strings, because they haven't been marked. You can mark them like this:
$allowed_values = [_("red"), _("orange"), _("blue")];
But now you can't retrieve the untranslated values later.
Instead, you could do:
$allowed_values = [gettext_noop("red"), gettext_noop("orange"), gettext_noop("blue")];
Now everything can work as intended, as long as you implement the rest of this answer.
How to implement gettext_noop
PHP doesn't actually include gettext_noop
by default. To implement it yourself, add this PHP:
function gettext_noop($string)
{
return $string;
}
And in your invocation of xgettext
on the command-line, include this --keyword
argument:
xgettext --keyword=gettext_noop ...