Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to translate the generic phrase "x hours and y mintues". Of course, if it's only one hour, it should say "1 hour and y mintutes". The same goes for minutes, "x hours and 1 minute." I know _n() handles the single/plural issue, but what is the best practice on handling two numbers in a phrase?

Method 1

printf(_n("%d hour", "%d hours", $hours), $hours);
_e(" and ");
printf(_n("%d minute", "%d minutes", $mins), $mins);

Works, but concatenating strings makes translation inflexible. I'd expect not all languages would work with "[hour phrase] [and phrase] [minutes phrase]".

Method 2

printf(
    "%s and %s",
    sprintf(_n("%d hour", "%d hours", $hours), $hours),
    sprintf(_n("%d minute", "%d minutes", $mins), $mins)
);

This seems right, but seems like it would be hard to translate without context. Maybe I need to add context with _x()?

Solution I went with:

something more readable @toscho

function nice_time($minutes) {
    $hours = floor($minutes/60);
    $minutes = $minutes % 60;

    if ($hours == 0 && $minutes == 0) {
        return __("no length of time", 'context');
    } else if ($hours == 0 && $minutes == 1) {
        return __("1 minute", 'context');
    } else if ($hours == 1 && $minutes == 0) {
        return __("1 hour", 'context');
    } else if ($hours == 1 && $minutes == 1) {
        return __("1 hour and 1 minute", 'context');
    } else if ($hours == 0) {
        return sprintf(__("%d minutes", 'context'), $minutes);
    } else if ($minutes == 0) {
        return sprintf(__("%d hours", 'context'), $hours);
    } else if ($hours == 1) {
        return sprintf(__("1 hours and %d minutes", 'context'), $minutes);
    } else if ($minutes == 1) {
        return sprintf(__("%d hours and 1 minutes", 'context'), $hours);
    } else {
        return sprintf(__("%d hours and %d minutes", 'context'), $hours, $minutes);
    }
}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

I would opt for readability and use a rather verbose solution:

if ( 1 == $hours and 1 == $mins )
{
    _e( 'One hour and one minute', 'your_text_domain' );
}
elseif ( 1 == $hours )
{
    printf( __( 'One hour and %d minutes', 'your_text_domain' ), $mins );
}
elseif ( 1 == $mins )
{
    printf( __( '%d hours and one minute', 'your_text_domain' ), $hours );
}
else
{
    printf( __( '%d hours and %d minutes', 'your_text_domain' ), $hours, $mins );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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