Error: msgstr is not a valid PHP when translating a wordpress theme

I’m trying to translate a theme for WordPress using Poedit. When I try to translate % comment string, whether I use % in translation or not, I get this error:

Error: ‘msgstr’ is not a valid PHP format string, unlike ‘msgid’.
Reason: The character that terminates the directive number 1 is not a valid conversion specifier.

Read More

That string’s resource is line below:

comments_number(
    __( '0 comments' , 'hbthemes' ) ,
    __( '1 comment' , 'hbthemes' ),
    __( '% comments' , 'hbthemes' )
);

I took a look at gettext manual but didn’t understand what would make these error. At first I wondered how just a single % could be in a format string, but the theme natively shows comments count correctly.

EDIT:

If I use %% instead of % in translation, the error remains and if I change it inside the PHP code, I get comments count twice, e.g. 22 instead of 2.

UPDATE:

The documentation here says % will be replaced by number of comments, so I think actual problem is with Poedit not the code.

Related posts

Leave a Reply

2 comments

  1. What exactly are you trying to accomplish? It seems to me you are reinventing the wheel and doing it wrong way on top of it. It thinks you should use sprintf(__(‘%d comments’), get_comments_number()) and it wants to force you to have it correct. You should not translate it this way. I am not sure wordpress uses gettext itself. If it is, use ngettext(‘%d comment’, ‘%d comments’, get_comments_number()). That way, it will have correct forms for other languages, that differs between 1, 3 and 5 for example.

    See http://codex.wordpress.org/Function_Reference/_n
    maybe this way:

        $localized = _n( "%d comment", "%d comments", get_comments_number(), 'hbthemes' );
        // Not sure how it behaves on number 0, try $localized, $localized, $localized
        comments_number( __('0 comment', 'hbthemes'), $localized, $localized);
    

    Problem is how WordPress has this designed, it cannot be translated well. It should help you if you remove #, php-format flag before this string in po file by hand. It will not try to validate sprintf format where it is not printf format.