I'm looking for a function that given a string it switches the string to singular/plural. I need it to work for european languages other than English.
Are there any functions that can do the trick? (Given a string to convert and the language?)
Thanks
Here is my handy function:
function plural( $amount, $singular = '', $plural = 's' ) {
if ( $amount === 1 ) {
return $singular;
}
return $plural;
}
By default, it just adds the 's' after the string. For example:
echo $posts . ' post' . plural( $posts );
This will echo '0 posts', '1 post', '2 posts', '3 posts', etc. But you can also do:
echo $replies . ' repl' . plural( $replies, 'y', 'ies' );
Which will echo '0 replies', '1 reply', '2 replies', '3 replies', etc. Or alternatively:
echo $replies . ' ' . plural( $replies, 'reply', 'replies' );
And it works for some other languages too. For example, in Spanish I do:
echo $comentarios . ' comentario' . plural( $comentarios );
Which will echo '0 comentarios', '1 comentario', '2 comentarios', '3 comentarios', etc. Or if adding an 's' is not the way, then:
echo $canciones . ' canci' . plural( $canciones, 'ón', 'ones' );
Which will echo '0 canciones', '1 canción', '2 canciones', '3 canciones', etc.