1

Looking at https://codex.wordpress.org/Function_Reference/human_time_diff

I'm using an English version of Wordpress.

In my theme template, I would like to define custom text of min, hour, day, week, month, year in Chinese using human_time_diff() when looping through posts.

According to the instruction in the Codex documentation:

<?php
    printf( _x( '%s ago', '%s = human-readable time difference',
    'your-text-domain' ), human_time_diff( get_the_time( 'U' ),
    current_time( 'timestamp' ) ) );
?>

I still don't know how to make the implementation to do the swap using the code above. eg.

min -> 分鐘
hour -> 小時
dat -> 天
week -> 週
month -> 月
year -> 年

ago part should be straight forward.

Is there an example that can demonstrate how it works?

Also, do I need to worry about plurals in English?

2

If you check out the source of human_time_diff:

if ( $diff < HOUR_IN_SECONDS ) {
    $mins = round( $diff / MINUTE_IN_SECONDS );
    if ( $mins <= 1 )
        $mins = 1;
    /* translators: min=minute */
    $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
    $hours = round( $diff / HOUR_IN_SECONDS );
    if ( $hours <= 1 )
        $hours = 1;
    $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
    $days = round( $diff / DAY_IN_SECONDS );
    if ( $days <= 1 )
        $days = 1;
    $since = sprintf( _n( '%s day', '%s days', $days ), $days );
} elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
    $weeks = round( $diff / WEEK_IN_SECONDS );
    if ( $weeks <= 1 )
        $weeks = 1;
    $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
} elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
    $months = round( $diff / MONTH_IN_SECONDS );
    if ( $months <= 1 )
        $months = 1;
    $since = sprintf( _n( '%s month', '%s months', $months ), $months );
} elseif ( $diff >= YEAR_IN_SECONDS ) {
    $years = round( $diff / YEAR_IN_SECONDS );
    if ( $years <= 1 )
        $years = 1;
    $since = sprintf( _n( '%s year', '%s years', $years ), $years );
}

As you can see, just translate the %s min, %s mins etc. strings.

  • What does _n do? And what does _x do in Codex documentation? I know it's easy to just modify the source of human_time_diff function in formatting.php but I would like to avoid modifying the Wordpress core so it won't be an issue when it comes to upgrading. Is there a way to do some filtering/repalcement of the result in functions.php? – KDX Jul 1 '16 at 22:35
0

The full solution that works for me without modifying the Wordpress core is to clone the human_time_diff() function and place it inside functions.php as a renamed human_time_diff_chinese(), then swap all occurrence of human_time_diff() function with this new human_time_diff_chinese() function.

function human_time_diff_chinese( $from, $to = '' ) {
  if ( empty( $to ) ) {
    $to = time();
  }

  $diff = (int) abs( $to - $from );

  if ( $diff < HOUR_IN_SECONDS ) {
    $mins = round( $diff / MINUTE_IN_SECONDS );
    if ( $mins <= 1 )
      $mins = 1;
    /* translators: min=minute */
    $since = sprintf( _n( '%s 分鐘', '%s 分鐘', $mins ), $mins );
  } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
    $hours = round( $diff / HOUR_IN_SECONDS );
    if ( $hours <= 1 )
      $hours = 1;
    $since = sprintf( _n( '%s 小時', '%s 小時', $hours ), $hours );
  } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
    $days = round( $diff / DAY_IN_SECONDS );
    if ( $days <= 1 )
      $days = 1;
    $since = sprintf( _n( '%s 天', '%s 天', $days ), $days );
  } elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
    $weeks = round( $diff / WEEK_IN_SECONDS );
    if ( $weeks <= 1 )
      $weeks = 1;
    $since = sprintf( _n( '%s 週', '%s 週', $weeks ), $weeks );
  } elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
    $months = round( $diff / MONTH_IN_SECONDS );
    if ( $months <= 1 )
      $months = 1;
    $since = sprintf( _n( '%s 個月', '%s 個月', $months ), $months );
  } elseif ( $diff >= YEAR_IN_SECONDS ) {
    $years = round( $diff / YEAR_IN_SECONDS );
    if ( $years <= 1 )
      $years = 1;
    $since = sprintf( _n( '%s 年', '%s 年', $years ), $years );
  }

  return apply_filters( 'human_time_diff_chinese', $since, $diff, $from, $to );
}
  • 1
    Why? Why aren't you translating the strings as I described? The real answer is a MO/PO file with the %s min etc. strings translated! – TheDeadMedic Jul 4 '16 at 9:23
  • @TheDeadMedic I thought you were asking me to translate the strings by direct modification of the formatting.php file. The solution I'm using is straight forward and simple for me to implement. I'm not familiar with working on MO/PO files nor seeing any MO/PO in my Wordpress installation. Do you have a complete instruction for MO/PO showing as example to translate %s min? If it works out, it would be a cleaner solution and I'll accept your answer as a more viable solution. – KDX Jul 5 '16 at 5:44

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.