Date format with genitive case of the month name is not working

Although, in my wp your echo displayed correctly (so maybe double check that you use the correct locale and that “decline months names: on or off” is translated as “on” in your locale), you can “force” genitive case, by making a generic wrap function based on the wp_maybe_decline_date().

I have tested and used this, in order to overcome the wp_maybe_decline_date() regex that ,matches formats like 'j F Y' or 'j. F', while I wanted to use 'l j F Y'

Example use case:

In our theme functions.php we define the wrapper function like:

/**
 * [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;
}

Then, in our template file where we want the genitive case to appear, we wrap the date_i18n(), like:

<?php echo multi_force_use_genitive_month_date( date_i18n( 'l j F Y' ) ); ?>

Hope this helps.

Leave a Comment