Изменить номер даты на другой язык/сценарий?


Ранее я мог изменять числа даты и времени (в систему чисел и дат другого языка, например кхмерского) для каждого сообщения, используя такой скрипт в моем functions.php :

function KhmerNumDate ($text) {
$text = str_replace('1', '១', $text);
$text = str_replace('2', '២', $text);
$text = str_replace('3', '៣', $text);
$text = str_replace('4', '៤', $text);
$text = str_replace('5', '៥', $text);
$text = str_replace('6', '៦', $text);
$text = str_replace('7', '៧', $text);
$text = str_replace('8', '៨', $text);
$text = str_replace('9', '៩', $text);
$text = str_replace('0', '០', $text); 
return $text;
}


add_filter('date', 'KhmerNumDate');
add_filter('the_date', 'KhmerNumDate');
add_filter('the_time', 'KhmerNumDate');

Но теперь это не работает - мой код хорош? Будет ли код отличаться в зависимости от того, какую тему я использую (в настоящее время я использую измененную дочернюю тему из двадцати двенадцати)?

Author: Nathan, 2012-11-05

3 answers

Пожалуйста, попробуйте этот способ:

Измените дату, the_date, the_time на get_date, get_the_date, get_the_time.

function KhmerNumDate ($text) {
    $text = str_replace('1', '១', $text);
    $text = str_replace('2', '២', $text);
    $text = str_replace('3', '៣', $text);
    $text = str_replace('4', '៤', $text);
    $text = str_replace('5', '៥', $text);
    $text = str_replace('6', '៦', $text);
    $text = str_replace('7', '៧', $text);
    $text = str_replace('8', '៨', $text);
    $text = str_replace('9', '៩', $text);
    $text = str_replace('0', '០', $text); 
    return $text;
    }


add_filter('get_date', 'KhmerNumDate');
add_filter('get_the_date', 'KhmerNumDate');
add_filter('get_the_time', 'KhmerNumDate');
 3
Author: haksrun, 2015-05-28 01:36:26

Оказывается, это было из-за того, как работает тема Двадцать Двенадцать (в их стремлении упростить перевод...).

Дата публикации в Двадцать двенадцатом подготовлена функцией в functions.php вызванный twentytwelve_entry_meta()

Поэтому, чтобы заменить/перевести числа в дате, я ищу строку с $date= в функции twentytwelve_entry_meta() (или предпочтительно воспроизвести функцию twentytwelve_entry_meta() в дочернем functions.php как и я):

$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a>',
        esc_url( get_permalink() ),
        esc_attr( get_the_time() ),
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() )

    );

    //my added code - the_curlang() is used with the xili-language plugin, it returns the current language of the page

$languagedate = the_curlang();

if ( $languagedate == 'km_kh' ) {
$date = str_replace('1', '១', $date);
$date = str_replace('2', '២', $date);
$date = str_replace('3', '៣', $date);
$date = str_replace('4', '៤', $date);
$date = str_replace('5', '៥', $date);
$date = str_replace('6', '៦', $date);
$date = str_replace('7', '៧', $date);
$date = str_replace('8', '៨', $date);
$date = str_replace('9', '៩', $date);
$date = str_replace('0', '០', $date); 
}

Тогда все выглядит великолепно!

Я не очень-то программист, так что мой код может быть не лучшим вариантом, но, по крайней мере, он работает.

 2
Author: Nathan, 2012-11-05 16:27:19

Я хотел бы предложить использовать встроенную функцию Wordpress для ваших конкретных потребностей, которая date_i18n($date_format, $time, $gmt). Просто передайте php date() формат и timestamp и Wordpress отобразит дату на вашем языке (язык определен в wp-config.php).

Пример со страницы кодекса:

echo date_i18n(get_option('date_format') ,strtotime("11/15-1976")); 
 1
Author: Ahmad M, 2012-11-05 16:04:12