0

If I'm just echoing regular text, I do this:

<?php _e('This post is closed to new comments.','my-theme') ?>

But how would I translate the text in comments_number(); so that the "Comments" text can be translated? Like this:

<?php comments_number( 'Comments (0)', 'Comments (1)', 'Comments (%)' ); ?>
2
  • 1
    `comments_number( __('Comments (0)'), __('Comments (1)'), __('Comments (%)') ); See Theme internationalization.
    – Max Yudin
    Commented Jul 5, 2016 at 6:38
  • Thanks @MaxYudin that works. You should post that as an answer to get credit :)
    – jetyet47
    Commented Jul 6, 2016 at 18:01

2 Answers 2

2

You have to make these strings translatable by using __() function:

comments_number( __('Comments (0)'), __('Comments (1)'), __('Comments (%)') );

If you want to use the custom textdomain, e.g. 'test':

comments_number( __('Comments (0)', 'test'), __('Comments (1)', 'test'), __('Comments (%)', 'test') );

For more information see:

-1

The comments_number function is calling get_comments_number_text function: https://developer.wordpress.org/reference/functions/get_comments_number_text/

In that function there are your strings to translate

function get_comments_number_text( $zero = false, $one = false, $more = false ) {
    $number = get_comments_number();

    if ( $number > 1 ) {
        if ( false === $more ) {
            /* translators: %s: number of comments */
            $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
        } else {
            // % Comments
            $output = str_replace( '%', number_format_i18n( $number ), $more );
        }
    } elseif ( $number == 0 ) {
        $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
    } else { // must be one
        $output = ( false === $one ) ? __( '1 Comment' ) : $one;
    }
    /**
     * Filter the comments count for display.
     *
     * @since 1.5.0
     *
     * @see _n()
     *
     * @param string $output A translatable string formatted based on whether the count
     *                       is equal to 0, 1, or 1+.
     * @param int    $number The number of post comments.
     */
    return apply_filters( 'comments_number', $output, $number );
}
2
  • I don't understand how this answers my question...
    – jetyet47
    Commented Jul 6, 2016 at 1:22
  • Sorry, I misreaded your question and was actually answering something else... The comment of Max Yudin gives you correct answer... Commented Jul 6, 2016 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.