From 946e6ac8de2baac007286dffc8dc5be130cc1c5e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 6 Jul 2016 14:45:55 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@37987 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 21 +++++ tests/phpunit/tests/comment/template.php | 115 ++++++++++++++++++++++- 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 75ed045cd3..04b443f732 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -893,6 +893,27 @@ function get_comments_number_text( $zero = false, $one = false, $more = false ) $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) ); } else { // % Comments + /* translators: If comment number in your language requires declension, + * translate this to 'on'. Do not translate into your own language. + */ + if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) { + $text = preg_replace( '#.+?#', '', $more ); + $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities + $text = trim( strip_tags( $text ), '% ' ); + + // Replace '% Comments' with a proper plural form + if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) { + /* translators: %s: number of comments */ + $new_text = _n( '%s Comment', '%s Comments', $number ); + $new_text = trim( sprintf( $new_text, '' ) ); + + $more = str_replace( $text, $new_text, $more ); + if ( false === strpos( $more, '%' ) ) { + $more = '% ' . $more; + } + } + } + $output = str_replace( '%', number_format_i18n( $number ), $more ); } } elseif ( $number == 0 ) { diff --git a/tests/phpunit/tests/comment/template.php b/tests/phpunit/tests/comment/template.php index 00fd2b8b95..17ec65e0e3 100644 --- a/tests/phpunit/tests/comment/template.php +++ b/tests/phpunit/tests/comment/template.php @@ -30,4 +30,117 @@ class Tests_Comment_Template extends WP_UnitTestCase { $this->assertEquals( 12, get_comments_number() ); } -} \ No newline at end of file + /** + * @ticket 13651 + */ + function test_get_comments_number_text_declension_with_default_args() { + $post_id = $this->factory->post->create(); + $permalink = get_permalink( $post_id ); + $this->go_to( $permalink ); + + $this->assertEquals( __( 'No Comments' ), get_comments_number_text() ); + + $this->factory->comment->create_post_comments( $post_id, 1 ); + $this->go_to( $permalink ); + + $this->assertEquals( __( '1 Comment' ), get_comments_number_text() ); + + $this->factory->comment->create_post_comments( $post_id, 1 ); + $this->go_to( $permalink ); + + $this->assertEquals( sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), get_comments_number_text() ); + + } + + /** + * @ticket 13651 + * @dataProvider data_get_comments_number_text_declension + */ + function test_get_comments_number_text_declension_with_custom_args( $number, $input, $output ) { + $post_id = $this->factory->post->create(); + $permalink = get_permalink( $post_id ); + + $this->factory->comment->create_post_comments( $post_id, $number ); + $this->go_to( $permalink ); + + add_filter( 'gettext_with_context', array( $this, '_enable_comment_number_declension' ), 10, 4 ); + + $this->assertEquals( $output, get_comments_number_text( false, false, $input ) ); + + remove_filter( 'gettext_with_context', array( $this, '_enable_comment_number_declension' ), 10, 4 ); + } + + function _enable_comment_number_declension( $translation, $text, $context, $domain ) { + if ( 'Comment number declension: on or off' === $context ) { + $translation = 'on'; + } + + return $translation; + } + + function data_get_comments_number_text_declension() { + return array( + array( + 2, + 'Comments (%)', + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + array( + 2, + '2 Comments', + '2 Comments', + ), + array( + 2, + '2 Comments on Hello world!', + '2 Comments on Hello world!', + ), + array( + 2, + '2 Comments on Hello % world!', + '2 Comments on Hello 2 world!' // See #WP37103 + ), + array( + 2, + __( '% Comments', 'twentyten' ), + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + array( + 2, + _x( '%', 'comments number', 'twentyeleven' ), + '2', + ), + array( + 2, + __( '% Replies', 'twentyeleven' ), + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + array( + 2, + __( '% comments →', 'twentyeleven' ), + sprintf( '2 %s →', trim( sprintf( _n( '%s Comment', '%s Comments', 2 ), '' ) ) ), + ), + array( + 2, + __( '% Replies', 'twentytwelve' ), + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + array( + 2, + __( 'View all % comments', 'twentythirteen' ), + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + array( + 2, + __( '% Comments', 'twentyfourteen' ), + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + array( + 2, + __( '% Comments', 'twentyfifteen' ), + sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), + ), + ); + } + +}