Please avoid being “smart” about _n()
-
When checking some pending translated strings (Swedish) for your plugin, I stumbled upon a thing in your code that you really need to do in a different way.
I’m referring to https://plugins.trac.ww.wp.xz.cn/browser/fluid-checkout/tags/1.3.2/inc/checkout-steps.php#L1774 where the following happens:
// @codingStandardsIgnoreStart 'package_name' => apply_filters( 'woocommerce_shipping_package_name', sprintf( _nx( 'Shipping', 'Shipping %d', ( $i + 1 ), 'shipping packages', 'woocommerce' ), ( $i + 1 ) ), $i, $package ), // @codingStandardsIgnoreEndThe use of _n() (well, here it’s _nx(), but anyway) is to provide a method for correct handling of plurals.
In English, it’s simple, right: If the integer == 1, then we use singular “I can see 1 chair”; if we’ve got any other number, then we use plural instead “I can see 0 chairs”, or “I can see 1234 chairs”.But many languages have a different structure!
Russian, for instance, has three cases. They’ll use:
“singular” for 1, 21, 31, 41…, 101, 121, and so on;
“dual” for 2, 3, 4, 22, 23, 24, 32… 102, 103, 104, and so on; and
“plural” for everything else (5~~20, 25~~30, etc.)
If you try to be smart and via _n() skip the number for for the special case 1 == n, then Russian won’t be able to do this, because the same translation will also need to work for 21, 31, and so on.Japanese, on the other hand, has only one single case. For every occurrence of _n() there will be only one translation possible.
And Japanese won’t be able to show a special string for “A single chair”, because the same string needs to cater for any other number of chairs, too.The solution:
If you want a slightly different string for the special case 1 == n, then use separate logic for that. As a nice side-effect, you can then remove the “Coding standards band-aid” you’ve put there right now.You may have more similar case, I just happened to see this one and wanted to tell you. If you’ve got any questions around internationalization, then you’re more than welcome to register in the global Slack for WordPress contributors via https://make.ww.wp.xz.cn/chat/ join “polyglots” and ask there.
The page I need help with: [log in to see the link]
The topic ‘Please avoid being “smart” about _n()’ is closed to new replies.