5

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);
    }
}
5

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 );
}

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.