Skip to:
Content

bbPress.org

Opened 5 years ago

Last modified 3 weeks ago

#3435 new defect (bug)

Please use _n() in order to handle plural forms correctly

Reported by: tobifjellner's profile tobifjellner Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: Extend - BuddyPress Keywords: good-first-bug has-patch
Cc:

Description

English has a very simple structure for plural handling when there is a number in a string that count stuff:
If n = 1 then use singular. In all other cases, use plural.
So many developers tend to just "test for 1 or more than 1" and if the number is higher than 1, then they'll create something with printf(), like "You've got n new emails!"

However, this will create problems with many languages that have more complicated structures for handling of strings that include numbers to count stuff.

In Russian, for instance, there are three cases:
"Singular" is used not only for 1, but also for 21, 31, 41, i.e. anything that ends in 1 (except the special case 11)
"Dual" is used for numbers that end in 2, 3, 4, 22, 23, 24, i.e. anything that ends in 2, 3 or 4 (except the special cases 12-14).
"Plural" is used for everything else 0, 5-20, 25-30...

If you think this is complicated, then have a look at Arabic. For every string with _n() they translate it into 6 (six!) different versions, depending on the number. See for yourself at https://translate.wordpress.org/projects/wp/dev/ar/default/?filters%5Bstatus%5D=either&filters%5Boriginal_id%5D=12071412&sort%5Bby%5D=translation_date_added&sort%5Bhow%5D=asc

So with our example string above, the translator would be forced to choose one of these three forms and accept that the translation will be incorrect in many cases... or try to rebuild the string, perhaps by saying something like "You've got new mails: n".

But there IS a good and elegant solution for this. _n()
(And nowadays we can use a similar function in Javascript!)

I stumbled upon a place in bbPress trunk where this needs to be fixed:
https://plugins.trac.wordpress.org/browser/bbpress/trunk/includes/extend/buddypress/notifications.php#L89

<?php
if ( $action_item_count > 1 ) {
    filter = 'bbp_multiple_new_subscription_notification';
    $text   = sprintf( esc_html__( 'You have %1$d new replies to %2$s', 'bbpress' ), $action_item_count, $topic_title );

Note: It's still often nice to keep the test for "1" in the code and have a different string in that case. Something like "Hey, there's a new mail waiting for you."

I was verbose here so that this ticket could be used for reference in the future. There may be more occurrences of this problem in bbPress, I didn't search for more.

Attachments (2)

3435.patch (965 bytes) - added by dilip2615 3 weeks ago.
attached patch 3435.patch to update plural wording for subscription notifications using _n() (fix for #3435).
notifications.php (8.5 KB) - added by dilip2615 3 weeks ago.
also review it.

Download all attachments as: .zip

Change History (5)

#1 @dilip2615
3 weeks ago

$filter = 'bbp_multiple_new_subscription_notification';

/*
 * Use _n() so the message uses "reply" for 1 item and "replies" for multiple items.
 */
$text = sprintf(
	_n(
		'You have %1$d new reply to %2$s',
		'You have %1$d new replies to %2$s',
		(int) $action_item_count,
		'bbpress'
	),
	(int) $action_item_count,
	$topic_title
);

$text = esc_html( $text );

Hello @tobifjellner, I updated #3435 to use _n() for correct singular/plural wording. When $action_item_count is 1, it shows new reply, otherwise it shows new replies. I tested the notification output for both cases and the text looks correct. Thanks

@dilip2615
3 weeks ago

attached patch 3435.patch to update plural wording for subscription notifications using _n() (fix for #3435).

@dilip2615
3 weeks ago

also review it.

#2 @tobifjellner
3 weeks ago

Nice. Looks good to me, but I'm not a coder, nor committer.

#3 @tobifjellner
3 weeks ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.