Plural forms and placeholders in WordPress translations
The basic translation functions are easy: __() returns a string, _e() prints it. The difficult part is sentences with numbers and names. “3 posts deleted” built with concatenation and an if ( 1 === $count ) check is untranslatable: other languages put the number elsewhere, and many have more than two plural forms (Ukrainian and Polish have three, Arabic six, Chinese one).
Problem: A plugin builds messages like “3 posts deleted” from pieces and chooses between singular and plural with an if, so translators cannot produce correct sentences.
Solution: Use one string per message with sprintf() placeholders, _n() for anything that depends on a number, _x() / _nx() when translators need a context, and a translators: comment for every string with placeholders.
Plural forms with _n()
<?php
// Wrong: the word order and the plural rule are fixed to English
echo $count . ' ' . ( 1 === $count ? 'post deleted' : 'posts deleted' );
// Right: gettext picks the correct form for the language
printf(
/* translators: %d: number of deleted posts */
esc_html( _n( '%d post deleted.', '%d posts deleted.', $count, 'textdomain' ) ),
$count
);
_n() only chooses the string; the number is inserted by printf(). The English singular can be written without the number (“One post deleted.”), but keep the placeholder in at least the plural, because in some languages the “singular” form is also used for 21, 31 and so on.
Several placeholders
With more than one placeholder, number them, so a translator can change their order:
<?php
printf(
/* translators: 1: user display name, 2: number of posts */
esc_html( _n( '%1$s published %2$d post.', '%1$s published %2$d posts.', $post_count, 'textdomain' ) ),
esc_html( $user_name ),
$post_count
);
Context: _x() and _nx()
The same English word can need different translations. A context tells translators which one is meant, and creates two separate entries:
<?php
$status = _x( 'Draft', 'post status', 'textdomain' );
$button = _x( 'Draft', 'verb: save as draft', 'textdomain' );
printf(
/* translators: %d: number of comments waiting for moderation */
esc_html( _nx( '%d comment', '%d comments', $pending, 'moderation queue', 'textdomain' ) ),
$pending
);
Translator comments
The comment must stand directly before the function call (inside the printf() parentheses works, as above) and start with translators:. Tools like wp i18n make-pot copy it into the .pot file, and the translator sees what %1$s and %2$d mean.
Numbers and dates
<?php echo esc_html( number_format_i18n( 12345.67, 2 ) ); // 12,345.67 in English, 12.345,67 in German echo esc_html( wp_date( get_option( 'date_format' ) ) ); // today in the site's format and language
How to make a theme or plugin translation-ready in the first place is shown in making a theme or plugin translation-ready, and how to translate an existing one in translating a WordPress theme or plugin.
NOTE: Never pass a variable as the string to translate (__( $label, 'textdomain' )): the extraction tools read the code without running it and cannot see the value, so the string never reaches the translators. Translate the fixed strings where they are defined and pass the results around.