1

I'm writing a plugin that works with taxonomy terms. It has, among other things, the capability to create new terms. I'm going to suppose that if users have custom taxonomies, they have taken care of having the singular and plural names set up and translated.

The question is: How do I format my plugin messages to use translated labels for taxonomies?

I need to output messages such as:

  • "Created 1 category."
  • "Matched 2 tags."
  • "Updated 5 movies." (Supposing "movies" is a custom taxonomy.)

In my understanding, the (translated) singular and plural labels for a taxonomy are available as $term_object->labels->singular_name and $term_object->labels->name.

Is it correct to assume that these properties contain translated strings? Or is it necessary to wrap them with some i18n function?

The _n() and the _nx() functions provide for singular and plural forms, but it doesn't make sense using them. I've been using:

$message = sprintf( 
    'Created %d %s', 
    count($created), 
    count($created) == 1 ?
        $term_object->labels->singular_name
        : $term_object->labels->name 
);

What is the best practice here?

1

You have to use a plural aware function like _n() or _nx(), because the words around the number might change in some languages depending on the amount items.
For visible numbers use number_format_i18n() and %s, not %d.

You cannot reuse the labels, because plural forms change depending on context in some languages.

This is how I would write your example:

// bare number, needed for _n()
$num  = count( $created );

// Singular or plural, we use %s, because we don't know what 
// number_format_i18n() will return.
$text = _n(
    'Created %s category',
    'Created %s categories',
    $num,
    'unique_plugin_textdomain'
);
// thousands separator etc.
$display_number = number_format_i18n( $num );
// Finally, the result:
$message        = sprintf( $text, $display_number );
  • The thing is I cannot anticipate which custom taxonomies a user might have, so I cannot provide strings for them. If they have a taxonomy called "Movies", my plugin can't know that. But you're also right about the labels not helping here, now that I think better. In Finnish, the plural label for movies would be "Elokuvat" but for the act of creating the noun goes in the accusative case: "Luotiin 5 elokuvaa". So I'm going to go with sprintf(__n( "Created 1 term in taxonomy %s", "Created %s terms in taxonomy %s", count($created),'textdomain'), number_format_i18n( count($created)), $taxonomy ); – P_Enrique Feb 4 '14 at 21:18
  • @HeikkiU Then let the user enter their own translation for that string. – fuxia Feb 4 '14 at 21:20
  • Thanks for pointing out the number_format_i18n() – P_Enrique Feb 4 '14 at 21:24
  • @HeikkiU How do you know the target language has an equivalent for accusative case? You don’t. :) – fuxia Feb 4 '14 at 21:26
  • I understood that; that's why I said: "You're right about labels not helping here." Anyway, thanks for pointing me in the right direction. – P_Enrique Feb 5 '14 at 0:32
0

Yes, register_taxonomy() expects array of translated labels and get_taxonomy_labels() provides translated default fallbacks.

You do not need to additionally pass labels through translation function since they assumed to be passed through it already.

Note that you do need to translate your own custom string (Created...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.