wp_maybe_decline_date()
云策文档标注
概述
wp_maybe_decline_date() 函数用于根据本地化设置决定日期字符串是否需要变格(如属格形式)。它主要处理特定语言(如俄语、加泰罗尼亚语)中月份名称在日期格式中的语法变化,确保日期显示符合语言规则。
关键要点
- 函数检查本地化设置是否要求月份名称变格(通过 _x('off', 'decline months names: on or off') 返回 'on' 触发)。
- 支持两种日期格式变格:'j F Y' 或 'j. F' 格式(日期在前,月份在后)和 'F jS' 或 'F j' 格式(月份在前,日期在后)。
- 参数 $datestring 为必需,表示格式化的日期字符串;$format 为可选,指定日期格式以辅助变格判断,默认为空字符串。
- 返回变格后的日期字符串,如果本地化不需要变格或函数不可用(如 SHORTINIT 模式),则返回原日期。
- 包含加泰罗尼亚语('ca' 区域设置)的特殊处理规则,如将 " de abril" 替换为 " d'abril"。
代码示例
function wp_maybe_decline_date( $date, $format = '' ) {
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' ) ) {
$months = $wp_locale->month;
$months_genitive = $wp_locale->month_genitive;
/*
* Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
* and decline the month.
*/
if ( $format ) {
$decline = preg_match( '#[dj].? F#', $format );
} else {
// If the format is not passed, try to guess it from the date string.
$decline = preg_match( '#bd{1,2}.? [^d ]+b#u', $date );
}
if ( $decline ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . 'b#u';
}
foreach ( $months_genitive as $key => $month ) {
$months_genitive[ $key ] = ' ' . $month;
}
$date = preg_replace( $months, $months_genitive, $date );
}
/*
* Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
* and change it to declined 'j F'.
*/
if ( $format ) {
$decline = preg_match( '#F [dj]#', $format );
} else {
// If the format is not passed, try to guess it from the date string.
$decline = preg_match( '#b[^d ]+ d{1,2}(st|nd|rd|th)?b#u', trim( $date ) );
}
if ( $decline ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '#b' . preg_quote( $month, '#' ) . ' (d{1,2})(st|nd|rd|th)?([-–]d{1,2})?(st|nd|rd|th)?b#u';
}
foreach ( $months_genitive as $key => $month ) {
$months_genitive[ $key ] = '$1$3 ' . $month;
}
$date = preg_replace( $months, $months_genitive, $date );
}
}
// Used for locale-specific rules.
$locale = get_locale();
if ( 'ca' === $locale ) {
// " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
$date = preg_replace( '# de ([ao])#i', " d'1", $date );
}
return $date;
}注意事项
- 在 SHORTINIT 模式下,由于 _x() 函数不可用,此函数会直接返回原日期字符串,不进行变格处理。
- 变格功能依赖于本地化设置,仅当 _x('off', 'decline months names: on or off') 返回 'on' 时触发,适用于需要属格的语言。
- 参数 $format 可用于明确指定日期格式以提高变格准确性;如果未提供,函数会尝试从日期字符串中猜测格式。
- 加泰罗尼亚语区域设置('ca')有特殊规则,处理 " de " 到 " d'" 的替换,以符合该语言语法。
- 此函数被 wp_date() 和 WP_Community_Events::format_event_data_time() 等内部函数调用,用于本地化日期显示。
原文内容
Determines if the date should be declined.
Description
If the locale specifies that month names require a genitive case in certain formats (like ‘j F Y’), the month name will be replaced with a correct form.
Parameters
$datestringrequired-
Formatted date string.
$formatstringoptional-
Date format to check. Default empty string.
Source
function wp_maybe_decline_date( $date, $format = '' ) {
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' ) ) {
$months = $wp_locale->month;
$months_genitive = $wp_locale->month_genitive;
/*
* Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
* and decline the month.
*/
if ( $format ) {
$decline = preg_match( '#[dj].? F#', $format );
} else {
// If the format is not passed, try to guess it from the date string.
$decline = preg_match( '#bd{1,2}.? [^d ]+b#u', $date );
}
if ( $decline ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . 'b#u';
}
foreach ( $months_genitive as $key => $month ) {
$months_genitive[ $key ] = ' ' . $month;
}
$date = preg_replace( $months, $months_genitive, $date );
}
/*
* Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
* and change it to declined 'j F'.
*/
if ( $format ) {
$decline = preg_match( '#F [dj]#', $format );
} else {
// If the format is not passed, try to guess it from the date string.
$decline = preg_match( '#b[^d ]+ d{1,2}(st|nd|rd|th)?b#u', trim( $date ) );
}
if ( $decline ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '#b' . preg_quote( $month, '#' ) . ' (d{1,2})(st|nd|rd|th)?([-–]d{1,2})?(st|nd|rd|th)?b#u';
}
foreach ( $months_genitive as $key => $month ) {
$months_genitive[ $key ] = '$1$3 ' . $month;
}
$date = preg_replace( $months, $months_genitive, $date );
}
}
// Used for locale-specific rules.
$locale = get_locale();
if ( 'ca' === $locale ) {
// " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
$date = preg_replace( '# de ([ao])#i', " d'\1", $date );
}
return $date;
}