Skip to content

php-format comment missing #427

Closed

Description

@kkmuffme

Bug Report

Describe the current, buggy behavior

The

#, php-format

comment is not added (anymore?) when running wp i18n make-pot

#121 (comment) provides an example where it should exist, but it seems only the plural issue was fixed there, but the "php-format" comment is/was still missing.

Describe how other contributors can replicate this bug

wp i18n make-pot for printf( _n( '%d cat', '%d cats', 1, 'foo' ), 1 );

See correct results with:
xgettext test.php for

<?php
printf( ngettext( '%d cat', '%d cats', 1 ), 1 );

Describe what you would expect as the correct outcome

#: test.php:2
#, php-format
msgid "%d cat"
msgid_plural "%d cats"
msgstr[0] ""
msgstr[1] ""

Let us know what environment you are running this on

WP-CLI version: 2.11.0

Provide a possible solution

If there is a placeholder add the php-format comment (like it's already done for the "translators:" anyway, so in those cases + those where we don't have a translators comment)

Activity

  1. swissspidy commented on Feb 4, 2025

    @swissspidy
    Member

    I don‘t recall ever adding this, so this was never there. It‘s specifically for xgettext/msgfmt. From the docs:

    These flags should not be added by a human. Instead only the xgettext program adds them. In an automated PO file processing system as proposed here, the user’s changes would be thrown away again as soon as the xgettext program generates a new template file.

    The c-format flag indicates that the untranslated string and the translation are supposed to be C format strings. The no-c-format flag indicates that they are not C format strings, even though the untranslated string happens to look like a C format string (with ‘%’ directives).

    When the c-format flag is given for a string the msgfmt program does some more tests to check the validity of the translation. See Invoking the msgfmt Program, Special Comments preceding Keywords and C Format Strings.

    From what I can see in the msgfmt code at first glance, it checks whether singular&plurals have the same amount of formats and whether those are valid.

    I don’t think we really need this for WordPress translations though… GlotPress doesn‘t use this and has its own validation for things like incorrect number of placeholders.

    Using this command here with msgfmt is also unlikely, and I don‘t see us adding similar validation to the make-mo command….

    Curious to learn about your use case though!

  2. kkmuffme commented on Feb 4, 2025

    @kkmuffme
    Author

    it checks whether singular&plurals have the same amount of formats and whether those are valid.

    Exactly that's what it would be useful for. Right now we have a custom post-processor to check those, however it would be much better if translators would get an error in their translation tool right away.

    Using this command here with msgfmt is also unlikely

    Lots of plugins use that actually instead of the wp-cli commands for po/mo

  3. swissspidy commented on Feb 4, 2025

    @swissspidy
    Member

    Hmm okay, well I guess it would be relatively easy to add, so PRs welcome :)

    We already have SPRINTF_PLACEHOLDER_REGEX in MakePotCommand, so in PhpFunctionsScanner and JsFunctionsScanner we can check the extracted strings for placeholders and then do $translation->addFlag( 'php-format' ). This should be easy to add tests for too.

    This should help with anyone using msgfmt.

    Ideally we add some validation logic to make-mo and make-php as well. We already do some validation in make-pot (see audit_strings() method, which we could extract into a reusable trait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions