覆盖1个单词的翻译

时间:2015-02-16 作者:user3428971

WP Job Manager中的一些单词尚未翻译。我现在在de po文件中编辑了大部分内容,但对于“5小时前发布”这句话,我找不到翻译“小时”的地方

声明为%s 在po文件中,无法在任何插件文件中找到单词hour。。。

那么在哪里更改这个插件的“小时”术语呢。

#: templates/content-job_listing.php (Line 18)
msgid "%s ago"
msgstr "%s geleden"

// Content-job_listing.php
<li class="date">
    <date>
        <?php printf( 
            __( \'%s ago\', \'wp-job-manager\' ), 
            human_time_diff( get_post_time( \'U\' ), current_time( \'timestamp\' ) )
        ); ?>
    </date>
</li>

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

由于您的问题是围绕WP主题开发人员使用了一个没有任何挂钩的函数来解决的,因此我们可以轻松地更改输出,因此您必须复制&;在函数中粘贴以下代码。php文件

function human_time_diff( $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 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 < 30 * DAY_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 >= 30 * DAY_IN_SECONDS ) {
        $months = round( $diff / ( 30 * DAY_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 );
    }

    /**
     * Filter the human readable difference between two timestamps.
     *
     * @since 4.0.0
     *
     * @param string $since The difference in human readable text.
     * @param int    $diff  The difference in seconds.
     * @param int    $from  Unix timestamp from which the difference begins.
     * @param int    $to    Unix timestamp to end the time difference.
     */
    return apply_filters( \'human_time_diff\', $since, $diff, $from, $to );
}
\'%s分钟\',\'%s分钟\'

\'%s小时\',\'%s小时\'

\'%s天\',\'%s天\'

\'%s周\',\'%s周\'

\'%s个月\',\'%s个月\'

这些是您需要根据自己的措辞更改的格式;确保只更改单词(分钟、小时、天、周、月),而不是%s

希望这有帮助。

SO网友:cybmeta

我认为您需要了解您使用的代码是如何工作的。输出字符串的代码为:

<?php printf( __( \'%s ago\', \'wp-job-manager\' ), human_time_diff( get_post_time( \'U\' ), current_time( \'timestamp\' ) ) ); ?>
在上述代码中,__( \'%s ago\', \'wp-job-manager\' ) 由wp job manager语言文件和%s 替换为的输出human_time_diff() 函数,格式为“1天”、“2个月”等。输出human_time_diff() 已被WordPress语言文件翻译,此外,还可以过滤输出。因此,您有两个选项:1)修改human_time_diff() 函数使用human_time_diff 过滤器或2)覆盖核心ussing的翻译gettext 滤器

1-Using human_time_diff filter. 示例:

add_filter( \'human_time_diff\', function($since, $diff, $from, $to) {

    //Here you can build your own human time diff strings
   //For example
   if ( empty( $to ) ) {
        $to = time();
    }

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

    if ( $diff < HOUR_IN_SECONDS ) {
        $since = "WOW, a lot of time ago!!!!";
    }

   return $since;

}, 10, 4 );
2Override the translations 使用gettext 滤器例如:

add_filter( \'gettext\', function($translated, $original, $domain) {

    if ( $original == "%s hour" ) {
        //fill $translated string with whatever you want
        $translated = "Some other string you want instead of original translattion";
    }
    return $translated;

}, 10, 3 );

结束