Make WordPress Core


Ignore:
Timestamp:
07/06/2016 02:45:55 PM (9 years ago)
Author:
SergeyBiryukov
Message:

I18N: Introduce an on/off switch for locales where comment number needs to be declined.

When enabled, the switch would override the theme's pseudo-plural '% Comments' string with a correct form of _n( '%s Comment', '%s Comments', $number ).

Historically, comments_popup_link() and get_comments_number_text() did not support plural forms and used a pseudo-plural style instead, so some locales were forced to come up with workarounds to display the number of comments in their language correctly.

This change should make those functions more i18n-friendly.

Fixes #13651.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/template.php

    r35242 r37987  
    3131    }
    3232
     33    /**
     34     * @ticket 13651
     35     */
     36    function test_get_comments_number_text_declension_with_default_args() {
     37        $post_id = $this->factory->post->create();
     38        $permalink = get_permalink( $post_id );
     39        $this->go_to( $permalink );
     40
     41        $this->assertEquals( __( 'No Comments' ), get_comments_number_text() );
     42
     43        $this->factory->comment->create_post_comments( $post_id, 1 );
     44        $this->go_to( $permalink );
     45
     46        $this->assertEquals( __( '1 Comment' ), get_comments_number_text() );
     47
     48        $this->factory->comment->create_post_comments( $post_id, 1 );
     49        $this->go_to( $permalink );
     50
     51        $this->assertEquals( sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), get_comments_number_text() );
     52
     53    }
     54
     55    /**
     56     * @ticket 13651
     57     * @dataProvider data_get_comments_number_text_declension
     58     */
     59    function test_get_comments_number_text_declension_with_custom_args( $number, $input, $output ) {
     60        $post_id = $this->factory->post->create();
     61        $permalink = get_permalink( $post_id );
     62
     63        $this->factory->comment->create_post_comments( $post_id, $number );
     64        $this->go_to( $permalink );
     65
     66        add_filter( 'gettext_with_context', array( $this, '_enable_comment_number_declension' ), 10, 4 );
     67
     68        $this->assertEquals( $output, get_comments_number_text( false, false, $input ) );
     69
     70        remove_filter( 'gettext_with_context', array( $this, '_enable_comment_number_declension' ), 10, 4 );
     71    }
     72
     73    function _enable_comment_number_declension( $translation, $text, $context, $domain ) {
     74        if ( 'Comment number declension: on or off' === $context ) {
     75            $translation = 'on';
     76        }
     77
     78        return $translation;
     79    }
     80
     81    function data_get_comments_number_text_declension() {
     82        return array(
     83            array(
     84                2,
     85                'Comments (%)',
     86                sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ),
     87            ),
     88            array(
     89                2,
     90                '2 Comments',
     91                '2 Comments',
     92            ),
     93            array(
     94                2,
     95                '2 Comments<span class="screen-reader-text"> on Hello world!</span>',
     96                '2 Comments<span class="screen-reader-text"> on Hello world!</span>',
     97            ),
     98            array(
     99                2,
     100                '2 Comments<span class="screen-reader-text"> on Hello % world!</span>',
     101                '2 Comments<span class="screen-reader-text"> on Hello 2 world!</span>' // See #WP37103
     102            ),
     103            array(
     104                2,
     105                __( '% Comments', 'twentyten' ),
     106                sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ),
     107            ),
     108            array(
     109                2,
     110                _x( '%', 'comments number', 'twentyeleven' ),
     111                '2',
     112            ),
     113            array(
     114                2,
     115                __( '<b>%</b> Replies', 'twentyeleven' ),
     116                sprintf( _n( '%s Comment', '%s Comments', 2 ), '<b>2</b>' ),
     117            ),
     118            array(
     119                2,
     120                __( '% <span class="reply">comments &rarr;</span>', 'twentyeleven' ),
     121                sprintf( '2 <span class="reply">%s &rarr;</span>', trim( sprintf( _n( '%s Comment', '%s Comments', 2 ), '' ) ) ),
     122            ),
     123            array(
     124                2,
     125                __( '% Replies', 'twentytwelve' ),
     126                sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ),
     127            ),
     128            array(
     129                2,
     130                __( 'View all % comments', 'twentythirteen' ),
     131                sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ),
     132            ),
     133            array(
     134                2,
     135                __( '% Comments', 'twentyfourteen' ),
     136                sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ),
     137            ),
     138            array(
     139                2,
     140                __( '% Comments', 'twentyfifteen' ),
     141                sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ),
     142            ),
     143        );
     144    }
     145
    33146}
Note: See TracChangeset for help on using the changeset viewer.