Opened 4 years ago
#3435 new defect (bug)
Please use _n() in order to handle plural forms correctly
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | trunk |
Component: | Extend - BuddyPress | Keywords: | good-first-bug |
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.