Make WordPress Core

Opened 17 years ago

Closed 8 years ago

#4916 closed defect (bug) (wontfix)

comment_type function doesn't return the comment type string

Reported by: hudatoriq's profile hudatoriq Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.3
Component: I18N Keywords: needs-patch
Focuses: Cc:

Description

The comment_type function in comment-template.php only echoes string that explain the comment type. Sometimes we need the string to be returned instead of echoed, e.g. for i18n purposes.

Attachments (1)

comment-template.diff (980 bytes) - added by hudatoriq 17 years ago.

Download all attachments as: .zip

Change History (15)

#1 @westi
17 years ago

  • Milestone 2.4 (next) deleted
  • Resolution set to invalid
  • Status changed from new to closed

Then use get_comment_type in general all simple WordPress template tags come in two forms:

  1. One which echo's - e.g. the_title
  2. One which returns - e.g. get_the_title

Marking as INVALID as get_comment_type provides what you need.

http://trac.wordpress.org/browser/trunk/wp-includes/comment-template.php#L201

#2 @hudatoriq
17 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Yes, there is get_comment_type() function. But it's rather different. get_comment_type() only checks and returns the string code of the type: 'trackback', 'pingback', and 'comment'. Whereas comment_type() receives the labels of each string code thus selecting which labels will be displayed on the page.

The function the_title() also has 'echo' parameter although there is already get_the_title() function

http://trac.wordpress.org/browser/trunk/wp-includes/post-template.php#L19

#3 @westi
17 years ago

  • Keywords reporter-feedback added

Can you provide an example of what you are trying to do?

Do we need to add a filter to the output of comment_type to allow you to translate the generated text?

Maybe something like this:

function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
	$type = get_comment_type();
	switch( $type ) {
		case 'trackback' :
			$output = $trackbacktxt;
			break;
		case 'pingback' :
			$output = $pingbacktxt;
			break;
		default :
			$output = $commenttxt;
	}
	echo apply_filters('comment_type',$output);
}

#4 @Nazgul
17 years ago

  • Milestone set to 2.3

#5 @hudatoriq
17 years ago

I was internationalizing a wordpress template when I met this problem.

Usage example on WP template:

For a unilanguage theme, we can simply write it like this:

<?php comment_type('Comment', 'Trackback', 'Pingback'); ?> by <?php comment_author_link(); ?> on <?php comment_date(); ?>

The problem is, I can't get it internationalized, 'cause in this case, I need the comment_type to returning the output instead of echoing it.
If my proposal is approved, this is how the code above will look like after it gets internationalized

<?php printf(__('%1$s by %2$s on %3$s', 'themedomain'), comment_type(__('Comment', 'themedomain'), __('Trackback', 'themedomain'), __('Pingback', 'themedomain'), false), get_comment_author_link(), get_comment_date()); ?>

Adding a filter is a good idea too. But isn't a good solution for a template.
The purpose of the file comment-template.php is to help people develop their WP template, isn't it? The combination of both solution (filter & new parameter) maybe?

#6 follow-up: @hudatoriq
17 years ago

  • Owner changed from hudatoriq to westi
  • Status changed from reopened to new

Hi, westi.

So.. where are we going to?

#7 in reply to: ↑ 6 @westi
17 years ago

  • Milestone changed from 2.3 to 2.4 (next)
  • Status changed from new to assigned

Replying to hudatoriq:

So.. where are we going to?

I had a chat on irc the other day with nbachiyski:

08:24 < nikolayb> looking at it
08:24 < westi|wah> I wonder wether the code in comment_type should be in get_comment_type so that you can call that with the translatable strings
08:26 < nikolayb> I think that all echoing template functions should only do echo get_<the-function>
08:28 < nikolayb> I guess get_comment_type is used for other things also
08:28 < westi|wah> yeah i'm thinking that would make more sense..
08:28 < nikolayb> in other contexts I mean
08:28 < nikolayb> so we can
08:28 < nikolayb> so we can't easily change it
08:28 < westi|wah> all get_comment_type does though is give you $comment->comment_type at present
08:29 < westi|wah> changing it could cause issues though
08:29 < nikolayb> don't spam plugins use it?
08:29 < westi|wah> possibly - i would hope they didn't though as it is really a template tag
08:29 < nikolayb> we can add another function in the middle: get_comment_type_text or something
08:29 < nikolayb> and comment_type would echo its value
08:31 < westi|wah> that sounds sensible

Moving this to 2.4 as it needs more work to resolve.

#8 @hudatoriq
17 years ago

Yeah, the inconsistency of what xxxx() function does to get_xxxx() should be fixed. There are other functions carrying the same problem. It'll be a hard work 'coz will break some plugins compatibility. But I hope you guys can do it earlier than 2.4.

#9 @Denis-de-Bernardy
15 years ago

  • Component changed from General to Comments

#10 @westi
15 years ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from 2.9 to Future Release

Needs a new patch and a good I18N solution.

Moving to future for now.

#11 @westi
12 years ago

  • Keywords i18n added
  • Owner westi deleted
  • Status changed from accepted to assigned

Removing ownership, tagging as i18n, would love to see a proposal to address this and improve the translation support here.

#12 @kobenland
12 years ago

Is a core solution even necessary?

I would have thought Themes hook into the 'get_comment_type' filter and provide their own translation, like this:

<?php
function theme_slug_get_comment_type( $type ) {
    $comment_types = array(
        'comment' => _x( 'Comment', 'noun', 'theme-slug' ),
        'trackback' =>  __( 'Trackback', 'theme-slug' ),
        'ping' => __( 'Pingback', 'theme-slug' );
    );
    return $comment_types[$type];
}
add_filter( 'get_comment_type', 'theme_slug_get_comment_type' );

No?

Last edited 12 years ago by kobenland (previous) (diff)

#13 @chriscct7
9 years ago

  • Component changed from Comments to I18N
  • Keywords comment i18n removed

@kobenland and/or @westi did either of you have any update on this?

#14 @wonderboymusic
8 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from assigned to closed

No movement in 4 years

Note: See TracTickets for help on using tickets.