Introduce wp_maybe_decline_date() for languages where certain date formats need to be declined, and hook it to the date_i18n filter.

If the locale specifies that month names require a genitive case in certain formats like `'j F Y'` or `'j. F'`, the month name will be replaced with a correct form. 

Fixes #11226.

git-svn-id: https://develop.svn.wordpress.org/trunk@35517 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2015-11-04 21:27:41 +00:00
parent 3ffaa37727
commit 8cbd66ca86
3 changed files with 69 additions and 12 deletions

View File

@@ -158,6 +158,47 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
return $j;
}
/**
* Determines if the date should be declined.
*
* 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.
*
* @since 4.4.0
*
* @param string $date Formatted date string.
* @return string The date, declined if locale specifies it.
*/
function wp_maybe_decline_date( $date ) {
global $wp_locale;
/* 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'
if ( @preg_match( '#^\d{1,2}\.? \w+#u', $date ) ) {
$months = $wp_locale->month;
foreach ( $months as $key => $month ) {
$months[ $key ] = '#' . $month . '#';
}
$date = preg_replace( $months, $wp_locale->month_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;
}
/**
* Convert integer number to format based on the locale.
*