如何在中文中使用国际化的Human_time_diff()函数?

时间:2016-06-30 作者:KDX

正在查看https://codex.wordpress.org/Function_Reference/human_time_diff

我用的是英文版的Wordpress。

在我的主题模板中,我想使用human_time_diff() 当通过帖子循环时。

根据Codex文件中的说明:

<?php
    printf( _x( \'%s ago\', \'%s = human-readable time difference\',
    \'your-text-domain\' ), human_time_diff( get_the_time( \'U\' ),
    current_time( \'timestamp\' ) ) );
?>
我仍然不知道如何使用上面的代码实现交换。例如:。

min -> 分鐘
hour -> 小時
dat -> 天
week -> 週
month -> 月
year -> 年
ago 零件应笔直向前。

有没有一个例子可以说明它是如何工作的?

还有,我需要担心英语中的复数吗?

2 个回复
最合适的回答,由SO网友:KDX 整理而成

在不修改Wordpress核心的情况下对我有效的完整解决方案是克隆human_time_diff() 功能并将其放置在内部functions.php 作为重命名的human_time_diff_chinese(), 然后交换所有出现的human_time_diff() 使用此新human_time_diff_chinese() 作用

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

SO网友:TheDeadMedic

如果您检查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 );
}
如您所见,只需翻译%s min, %s mins 等字符串。