Make WordPress Core


Ignore:
Timestamp:
11/04/2015 09:27:41 PM (10 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r35494 r35517  
    157157    $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
    158158    return $j;
     159}
     160
     161/**
     162 * Determines if the date should be declined.
     163 *
     164 * If the locale specifies that month names require a genitive case in certain
     165 * formats (like 'j F Y'), the month name will be replaced with a correct form.
     166 *
     167 * @since 4.4.0
     168 *
     169 * @param string $date Formatted date string.
     170 * @return string The date, declined if locale specifies it.
     171 */
     172function wp_maybe_decline_date( $date ) {
     173    global $wp_locale;
     174
     175    /* translators: If months in your language require a genitive case,
     176     * translate this to 'on'. Do not translate into your own language.
     177     */
     178    if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
     179        // Match a format like 'j F Y' or 'j. F'
     180        if ( @preg_match( '#^\d{1,2}\.? \w+#u', $date ) ) {
     181            $months = $wp_locale->month;
     182
     183            foreach ( $months as $key => $month ) {
     184                $months[ $key ] = '#' . $month . '#';
     185            }
     186
     187            $date = preg_replace( $months, $wp_locale->month_genitive, $date );
     188        }
     189    }
     190
     191    // Used for locale-specific rules
     192    $locale = get_locale();
     193
     194    if ( 'ca' === $locale ) {
     195        // " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..."
     196        $date = preg_replace( '# de ([ao])#i', " d'\\1", $date );
     197    }
     198
     199    return $date;
    159200}
    160201
Note: See TracChangeset for help on using the changeset viewer.