WordPress.org

Make WordPress Core

Ticket #13651: comments-number-i18n-custom-parsing.diff

File comments-number-i18n-custom-parsing.diff, 17.7 KB (added by nbachiyski, 9 years ago)
  • wp-includes/l10n.php

     
    272272 * @since 2.5
    273273 * @param string $single Single form to be i18ned
    274274 * @param string $plural Plural form to be i18ned
    275  * @return array array($single, $plural)
     275 * @param string $domain Gettext domain. Optional. If not present, the domain passed to {@link translate_nooped_plural()} will be used.
     276 * @return array array('single' => $single, 'plural' => $plural, ...)
    276277 */
    277 function _n_noop( $singular, $plural ) {
    278         return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null );
     278function _n_noop( $singular, $plural, $domain = null ) {
     279        return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain );
    279280}
    280281
    281282/**
     
    283284 *
    284285 * @see _n_noop()
    285286 */
    286 function _nx_noop( $singular, $plural, $context ) {
    287         return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context );
     287function _nx_noop( $singular, $plural, $context, $domain = null ) {
     288        return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain );
    288289}
    289290
    290291/**
     
    293294 * @since 3.1
    294295 * @param array $nooped_plural array with singular, plural and context keys, usually the result of _n_noop() or _nx_noop()
    295296 * @param int $count number of objects
    296  * @param string $domain Optional. The domain identifier the text should be retrieved in
     297 * @param string $domain Optional. The domain identifier the text should be retrieved in. Used only in case $nooped_plural doesn't contain a domain
    297298 */
    298299function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
     300        $domain = is_null( $nooped_plural['domain'] )? $domain : $nooped_plural['domain'];
    299301        if ( $nooped_plural['context'] )
    300302                return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
    301303        else
  • wp-includes/comment-template.php

     
    560560}
    561561
    562562/**
    563  * Display the language string for the number of comments the current post has.
    564  *
    565  * @since 0.71
    566  * @uses apply_filters() Calls the 'comments_number' hook on the output and number of comments respectively.
    567  *
    568  * @param string $zero Text for no comments
    569  * @param string $one Text for one comment
    570  * @param string $more Text for more than one comment
    571  * @param string $deprecated Not used.
     563 * Return the text for the number of comments the current post has. The arguments are the same as those of {@link comments_number_text()}
     564 *
     565 * @see comments_number_text()
    572566 */
    573 function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
    574         if ( !empty( $deprecated ) )
    575                 _deprecated_argument( __FUNCTION__, '1.3' );
     567function get_comments_number_text( $no_comments = null, $one_comment = null, $many_comments = null, $domain = 'default' ) {
     568        return _get_comments_number_text_helper( $no_comments, $one_comment, $many_comments, null, $domain );
     569}
    576570
     571function get_comments_number_text_x( $no_comments = null, $one_comment = null, $many_comments = null, $context = null, $domain = 'default' ) {
     572        return _get_comments_number_text_helper( $no_comments, $one_comment, $many_comments, $context, $domain );
     573}
     574
     575/**
     576 * @access private
     577 */
     578function _get_comments_number_text_helper( $no_comments = null, $one_comment = null, $many_comments = null, $context, $domain = 'default' ) {
    577579        $number = get_comments_number();
     580        if ( 0 == $number ) {
     581                $text = is_null( $no_comments )? __( 'No Comments' ) : translate( $no_comments );
     582        } else {
     583                $format = _get_comments_number_translated_format( $number, $one_comment, $many_comments, $context, $domain);
     584                $text = sprintf( $format, number_format_i18n( $number) );
     585        }
     586        return apply_filters( 'get_comments_number_text', $text, $number );
     587       
     588}
    578589
    579         if ( $number > 1 )
    580                 $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
    581         elseif ( $number == 0 )
    582                 $output = ( false === $zero ) ? __('No Comments') : $zero;
    583         else // must be one
    584                 $output = ( false === $one ) ? __('1 Comment') : $one;
     590/**
     591 * @access private
     592 */
     593function _get_comments_number_translated_format( $number, $one_comment, $many_comments, $context, $domain ) {
     594        if ( is_null( $one_comment ) || is_null( $many_comments) )
     595                return $context? _nx( '1 Comment', '%s Comments', $number, $context, $domain ) : _n( '1 Comment', '%s Comments', $number, $context, $domain );
     596        else
     597                return $context? _nx( $one_comment, $many_comments, $number, $context, $domain ) : _n( $one_comment, $many_comments, $number, $domain );
     598}
    585599
    586         echo apply_filters('comments_number', $output, $number);
     600function comments_number_text( $no_comments = null, $one_comment = null, $many_comments = null, $domain = 'default' ) {
     601        $number = get_comments_number();
     602        $after_old_filter = apply_filters( 'comments_number', get_comments_number_text( $no_comments, $one_comment, $many_comments, $domain ), $number );
     603        echo apply_filters( 'comments_number_text', $after_old_filter, $number );
    587604}
    588605
     606function comments_number_text_x( $no_comments = null, $one_comment = null, $many_comments = null, $context, $domain = 'default' ) {
     607        $number = get_comments_number();
     608        $after_old_filter = apply_filters( 'comments_number', get_comments_number_text_x( $no_comments, $one_comment, $many_comments, $context, $domain ), $number );
     609        echo apply_filters( 'comments_number_text', $after_old_filter, $number );
     610}
     611
     612
    589613/**
     614 * Return an HTML link to the current post's comments. The arguments are the same as those of {@link comments_number_link()}
     615 *
     616 */
     617function get_comments_number_link( $no_comments = null, $one_comment = null, $many_comments = null, $domain = 'default', $disabled = null, $attrs = array() ) {
     618        global $wpcommentspopupfile, $wpcommentsjavascript;
     619        $number = get_comments_number();
     620       
     621        $disabled = is_null( $disabled )? __( 'Comments Off' ) : translate( $disabled );
     622
     623        if ( 0 == $number && !comments_open() && !pings_open() ) {
     624                return "<span ".apply_filters( 'get_comments_number_link_attrs', $attrs ).">$disabled</span>";
     625        }
     626
     627        if ( post_password_required() ) {
     628                return __( 'Enter your password to view comments.' );
     629        }
     630       
     631        if ( post_password_required() ) {
     632                echo __('Enter your password to view comments.');
     633                return;
     634        }
     635
     636        $attrs['href'] = $number? get_comments_link() : get_permalink() . '#respond';
     637        if ( isset( $wpcommentsjavascript ) && $wpcommentsjavascript ) {
     638                $home = $wpcommentspopupfile? get_option( 'siteurl' ) : home_url();
     639                $attrs['href'] = $home . '/' . $wpcommentspopupfile . '?comments_popup=' . get_the_ID();
     640                $attrs['onclick'] = 'wpopen(this.href); return false;';
     641        }
     642        $attrs['title'] = sprintf( __('Comment on %s'), get_the_title() );
     643        $attrs = apply_filters( 'get_comments_number_link_attrs', $attrs );
     644       
     645        $text = get_comments_number_text( $no_comments, $one_comment, $many_comments, $domain );
     646        $html_attrs = wp_html_attributes( $attrs );
     647        return "<a $html_attrs>$text</a>";
     648}
     649
     650/**
     651 * Display an HTML link to the current post's comments
     652 *
     653 * @param string $zero text if there are no comments. Optional. Default is 'No Comments'
     654 * @param string $nooped_plural the result of {@link _n_noop()} or {@link _nx_noop()}, which determines what the text will be depending on different comment numbers
     655 * @param string $disabled text if comments are disabled. Options. Default is 'Comments Off'
     656 * @param array $attrs associative array of html attributes and their values (raw, not escaped)
     657 */
     658function comments_number_link( $no_comments = null, $one_comment = null, $many_comments = null, $domain = 'default', $disabled = null, $attrs = array() ) {
     659        echo get_comments_number_link( $no_comments, $one_comment, $many_comments, $domain, $disabled, $attrs );
     660}
     661
     662/**
    590663 * Retrieve the text of the current comment.
    591664 *
    592665 * @since 1.5.0
     
    9491022}
    9501023
    9511024/**
    952  * Displays the link to the comments popup window for the current post ID.
    953  *
    954  * Is not meant to be displayed on single posts and pages. Should be used on the
    955  * lists of posts
    956  *
    957  * @since 0.71
    958  * @uses $wpcommentspopupfile
    959  * @uses $wpcommentsjavascript
    960  * @uses $post
    961  *
    962  * @param string $zero The string to display when no comments
    963  * @param string $one The string to display when only one comment is available
    964  * @param string $more The string to display when there are more than one comment
    965  * @param string $css_class The CSS class to use for comments
    966  * @param string $none The string to display when comments have been turned off
    967  * @return null Returns null on single posts and pages.
    968  */
    969 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
    970         global $wpcommentspopupfile, $wpcommentsjavascript;
    971 
    972         $id = get_the_ID();
    973 
    974         if ( false === $zero ) $zero = __( 'No Comments' );
    975         if ( false === $one ) $one = __( '1 Comment' );
    976         if ( false === $more ) $more = __( '% Comments' );
    977         if ( false === $none ) $none = __( 'Comments Off' );
    978 
    979         $number = get_comments_number( $id );
    980 
    981         if ( 0 == $number && !comments_open() && !pings_open() ) {
    982                 echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
    983                 return;
    984         }
    985 
    986         if ( post_password_required() ) {
    987                 echo __('Enter your password to view comments.');
    988                 return;
    989         }
    990 
    991         echo '<a href="';
    992         if ( $wpcommentsjavascript ) {
    993                 if ( empty( $wpcommentspopupfile ) )
    994                         $home = home_url();
    995                 else
    996                         $home = get_option('siteurl');
    997                 echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
    998                 echo '" onclick="wpopen(this.href); return false"';
    999         } else { // if comments_popup_script() is not in the template, display simple comment link
    1000                 if ( 0 == $number )
    1001                         echo get_permalink() . '#respond';
    1002                 else
    1003                         comments_link();
    1004                 echo '"';
    1005         }
    1006 
    1007         if ( !empty( $css_class ) ) {
    1008                 echo ' class="'.$css_class.'" ';
    1009         }
    1010         $title = the_title_attribute( array('echo' => 0 ) );
    1011 
    1012         echo apply_filters( 'comments_popup_link_attributes', '' );
    1013 
    1014         echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
    1015         comments_number( $zero, $one, $more );
    1016         echo '</a>';
    1017 }
    1018 
    1019 /**
    10201025 * Retrieve HTML content for reply to comment link.
    10211026 *
    10221027 * The default arguments that can be override are 'add_below', 'respond_id',
  • wp-includes/formatting.php

     
    28982898
    28992899}
    29002900
    2901 ?>
     2901/**
     2902 * Builds an HTML attributes string out of an associattive array
     2903 *
     2904 * You don't have to escape the attribute values.
     2905 *
     2906 * Example:
     2907 * <code>wp_html_attributes( array( 'class' => 'post', 'id' => 'post-11' ) )</code> will return <code>'class="post" id="post-11"'</code>
     2908 *
     2909 * @param $attr array associative array in the form attribute-name => attribute-value
     2910 * @return string the attributes suitable for insertion into an HTML tag
     2911 */
     2912function wp_html_attributes( $attrs ) {
     2913        $attrs = wp_parse_args( $attrs );
     2914        $strings = array();
     2915        foreach( $attrs as $key => $value ) {
     2916                $strings[] = $key.'="'.esc_attr( $value ).'"';
     2917        }
     2918        return implode( ' ', $strings );
     2919}
     2920 No newline at end of file
  • wp-includes/deprecated.php

     
    25772577        return true;
    25782578}
    25792579
     2580/**
     2581 * Display the language string for the number of comments the current post has.
     2582 *
     2583 * @since 0.71
     2584 * @uses apply_filters() Calls the 'comments_number' hook on the output and number of comments respectively.
     2585 *
     2586 * @param string $zero Text for no comments
     2587 * @param string $one Text for one comment
     2588 * @param string $more Text for more than one comment
     2589 * @param string $deprecated Not used.
     2590 */
     2591function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
     2592        _deprecated_function( __FUNCTION__, '3.1', 'comments_number_text()'  );
     2593       
     2594        if ( !empty( $deprecated ) )
     2595                _deprecated_argument( __FUNCTION__, '1.3' );
     2596
     2597        $number = get_comments_number();
     2598
     2599        if ( $number > 1 )
     2600                $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
     2601        elseif ( $number == 0 )
     2602                $output = ( false === $zero ) ? __('No Comments') : $zero;
     2603        else // must be one
     2604                $output = ( false === $one ) ? __('1 Comment') : $one;
     2605
     2606        echo apply_filters('comments_number', $output, $number);
     2607}
     2608
     2609/**
     2610 * Displays the link to the comments popup window for the current post ID.
     2611 *
     2612 * Is not meant to be displayed on single posts and pages. Should be used on the
     2613 * lists of posts
     2614 *
     2615 * @since 0.71
     2616 * @uses $wpcommentspopupfile
     2617 * @uses $wpcommentsjavascript
     2618 * @uses $post
     2619 *
     2620 * @param string $zero The string to display when no comments
     2621 * @param string $one The string to display when only one comment is available
     2622 * @param string $more The string to display when there are more than one comment
     2623 * @param string $css_class The CSS class to use for comments
     2624 * @param string $none The string to display when comments have been turned off
     2625 * @return null Returns null on single posts and pages.
     2626 */
     2627function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
     2628        global $wpcommentspopupfile, $wpcommentsjavascript;
     2629       
     2630        _deprecated_function( __FUNCTION__, '3.1', 'comments_number_link()'  );
     2631
     2632        $id = get_the_ID();
     2633
     2634        if ( false === $zero ) $zero = __( 'No Comments' );
     2635        if ( false === $one ) $one = __( '1 Comment' );
     2636        if ( false === $more ) $more = __( '% Comments' );
     2637        if ( false === $none ) $none = __( 'Comments Off' );
     2638
     2639        $number = get_comments_number( $id );
     2640
     2641        if ( 0 == $number && !comments_open() && !pings_open() ) {
     2642                echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
     2643                return;
     2644        }
     2645
     2646        if ( post_password_required() ) {
     2647                echo __('Enter your password to view comments.');
     2648                return;
     2649        }
     2650
     2651        echo '<a href="';
     2652        if ( $wpcommentsjavascript ) {
     2653                if ( empty( $wpcommentspopupfile ) )
     2654                        $home = home_url();
     2655                else
     2656                        $home = get_option('siteurl');
     2657                echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
     2658                echo '" onclick="wpopen(this.href); return false"';
     2659        } else { // if comments_popup_script() is not in the template, display simple comment link
     2660                if ( 0 == $number )
     2661                        echo get_permalink() . '#respond';
     2662                else
     2663                        comments_link();
     2664                echo '"';
     2665        }
     2666
     2667        if ( !empty( $css_class ) ) {
     2668                echo ' class="'.$css_class.'" ';
     2669        }
     2670        $title = the_title_attribute( array('echo' => 0 ) );
     2671
     2672        echo apply_filters( 'comments_popup_link_attributes', '' );
     2673
     2674        echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
     2675        comments_number( $zero, $one, $more );
     2676        echo '</a>';
     2677}
     2678 No newline at end of file
  • wp-content/themes/twentyten/loop.php

     
    5353         *
    5454         * Without further ado, the loop:
    5555         */ ?>
    56 <?php while ( have_posts() ) : the_post();
     56<?php while ( have_posts() ) : the_post(); ?>
    5757
    58 $comment_number_template = _n( '1 Comment', '% Comments', get_comments_number(), 'twentyten' );
    59 ?>
    60 
    6158<?php /* How to display posts of the Gallery format. The gallery category is the old way. */ ?>
    6259
    6360        <?php if ( 'gallery' == get_post_format( $post->ID ) || in_category( _x( 'gallery', 'gallery category slug', 'twentyten' ) ) ) : ?>
     
    9996                                <a href="<?php echo get_term_link( _x( 'gallery', 'gallery category slug', 'twentyten' ), 'category' ); ?>" title="<?php esc_attr_e( 'View posts in the Gallery category', 'twentyten' ); ?>"><?php _e( 'More Galleries', 'twentyten' ); ?></a>
    10097                                <span class="meta-sep">|</span>
    10198                        <?php endif; ?>
    102                                 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), $comment_number_template, $comment_number_template ); ?></span>
     99                                <span class="comments-link"><?php comments_number_link( 'Leave a comment', '1 Comment', '%s Comments', 'twentyten' ); ?></span>
    103100                                <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    104101                        </div><!-- .entry-utility -->
    105102                </div><!-- #post-## -->
     
    122119                        <div class="entry-utility">
    123120                                <?php twentyten_posted_on(); ?>
    124121                                <span class="meta-sep">|</span>
    125                                 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), $comment_number_template, $comment_number_template ); ?></span>
     122                                <span class="comments-link"><?php comments_number_link( 'Leave a comment', '1 Comment', '%s Comments', 'twentyten' ); ?></span>
    126123                                <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    127124                        </div><!-- .entry-utility -->
    128125                </div><!-- #post-## -->
     
    164161                                        </span>
    165162                                        <span class="meta-sep">|</span>
    166163                                <?php endif; ?>
    167                                 <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), $comment_number_template, $comment_number_template ); ?></span>
     164                                <span class="comments-link"><?php comments_number_link( 'Leave a comment', '1 Comment', '%s Comments', 'twentyten' ); ?></span>
    168165                                <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    169166                        </div><!-- .entry-utility -->
    170167                </div><!-- #post-## -->