虽然,在我的wp中echo
正确显示(因此可能需要再次检查您是否使用了正确的区域设置,以及“拒绝月份名称:开或关”在您的区域设置中是否翻译为“开”),您可以通过基于wp_maybe_decline_date()
.
为了克服wp_maybe_decline_date()
正则表达式,匹配以下格式\'j F Y\'
或\'j. F\'
, 当我想使用\'l j F Y\'
示例用例:
在我们的主题中functions.php
我们定义包装函数如下:
/**
* [multi_force_use_genitive_month_date]
* Call this to force genitive use case for months in date translation
* @param string $date Formatted date string.
* @return string The date, declined if locale specifies it.
*/
function multi_force_use_genitive_month_date( $date ) {
global $wp_locale;
// i18n functions are not available in SHORTINIT mode
if ( ! function_exists( \'_x\' ) ) {
return $date;
}
/* translators: If months in your language require a genitive case,
* translate this to \'on\'. Do not translate into your own language.
*/
if ( \'on\' === _x( \'off\', \'decline months names: on or off\' ) ) {
// Match a format like \'j F Y\' or \'j. F\'
$months = $wp_locale->month;
$months_genitive = $wp_locale->month_genitive;
foreach ( $months as $key => $month ) {
$months[ $key ] = \'# \' . $month . \'( |$)#u\';
}
foreach ( $months_genitive as $key => $month ) {
$months_genitive[ $key ] = \' \' . $month . \'$1\';
}
$date = preg_replace( $months, $months_genitive, $date );
}
// Used for locale-specific rules
$locale = get_locale();
return $date;
}
然后,在希望生成格出现的模板文件中,我们将
date_i18n()
, 例如:
<?php echo multi_force_use_genitive_month_date( date_i18n( \'l j F Y\' ) ); ?>
希望这有帮助。