WordPress.org

Make WordPress Core

Changeset 35517


Ignore:
Timestamp:
11/04/2015 09:27:41 PM (3 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.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

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

    r35466 r35517  
    159159
    160160add_filter( 'widget_text', 'balanceTags' );
     161
     162add_filter( 'date_i18n', 'wp_maybe_decline_date' );
    161163
    162164// RSS filters
  • 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
  • trunk/src/wp-includes/locale.php

    r35372 r35517  
    140140
    141141        // The Months
    142         $this->month['01'] = /* translators: month name */ __('January');
    143         $this->month['02'] = /* translators: month name */ __('February');
    144         $this->month['03'] = /* translators: month name */ __('March');
    145         $this->month['04'] = /* translators: month name */ __('April');
    146         $this->month['05'] = /* translators: month name */ __('May');
    147         $this->month['06'] = /* translators: month name */ __('June');
    148         $this->month['07'] = /* translators: month name */ __('July');
    149         $this->month['08'] = /* translators: month name */ __('August');
    150         $this->month['09'] = /* translators: month name */ __('September');
    151         $this->month['10'] = /* translators: month name */ __('October');
    152         $this->month['11'] = /* translators: month name */ __('November');
    153         $this->month['12'] = /* translators: month name */ __('December');
     142        $this->month['01'] = /* translators: month name */ __( 'January' );
     143        $this->month['02'] = /* translators: month name */ __( 'February' );
     144        $this->month['03'] = /* translators: month name */ __( 'March' );
     145        $this->month['04'] = /* translators: month name */ __( 'April' );
     146        $this->month['05'] = /* translators: month name */ __( 'May' );
     147        $this->month['06'] = /* translators: month name */ __( 'June' );
     148        $this->month['07'] = /* translators: month name */ __( 'July' );
     149        $this->month['08'] = /* translators: month name */ __( 'August' );
     150        $this->month['09'] = /* translators: month name */ __( 'September' );
     151        $this->month['10'] = /* translators: month name */ __( 'October' );
     152        $this->month['11'] = /* translators: month name */ __( 'November' );
     153        $this->month['12'] = /* translators: month name */ __( 'December' );
     154
     155        // The Months, genitive
     156        $this->month_genitive['01'] = /* translators: month name, genitive */ _x( 'January', 'genitive' );
     157        $this->month_genitive['02'] = /* translators: month name, genitive */ _x( 'February', 'genitive' );
     158        $this->month_genitive['03'] = /* translators: month name, genitive */ _x( 'March', 'genitive' );
     159        $this->month_genitive['04'] = /* translators: month name, genitive */ _x( 'April', 'genitive' );
     160        $this->month_genitive['05'] = /* translators: month name, genitive */ _x( 'May', 'genitive' );
     161        $this->month_genitive['06'] = /* translators: month name, genitive */ _x( 'June', 'genitive' );
     162        $this->month_genitive['07'] = /* translators: month name, genitive */ _x( 'July', 'genitive' );
     163        $this->month_genitive['08'] = /* translators: month name, genitive */ _x( 'August', 'genitive' );
     164        $this->month_genitive['09'] = /* translators: month name, genitive */ _x( 'September', 'genitive' );
     165        $this->month_genitive['10'] = /* translators: month name, genitive */ _x( 'October', 'genitive' );
     166        $this->month_genitive['11'] = /* translators: month name, genitive */ _x( 'November', 'genitive' );
     167        $this->month_genitive['12'] = /* translators: month name, genitive */ _x( 'December', 'genitive' );
    154168
    155169        // Abbreviations for each month.
Note: See TracChangeset for help on using the changeset viewer.