3

Wordpress 4.4 indroduced a way to display the genitive case of a months name for specific locale by using the function date_i18n (https://core.trac.wordpress.org/ticket/11226#comment:32) which is filtered by wp_maybe_decline_date() (https://core.trac.wordpress.org/browser/tags/4.6/src/wp-includes/functions.php#L172). Even though both my locale (el) and the translation of the string "decline months names: on or off" in my el.po file are correct, the genitive case of the months name is not working. So with this: echo date_i18n( 'j F Y', strtotime( '2016/9/20' ) ); I got the date with the month name in nominative case.

Any ideas? Thanks in advance!

2 Answers 2

3

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.

0

Updated, working version based on the code from @Cubakos:

function multi_force_use_genitive_month_date( $date ) {
   global $wp_locale;

   $months          = $wp_locale->month;
   $months_genitive = $wp_locale->month_genitive;

   $date = str_replace( $months, $months_genitive, $date );

   return $date;
}

Usage in posts loop

<?= multi_force_use_genitive_month_date(get_the_date()); ?>
2
  • 1
    Please edit your answer, and add an explanation: why does that solve the problem better?
    – fuxia
    Oct 30, 2020 at 16:59
  • @Rup yikes, thank for noticing ;)
    – skwrn
    Nov 1, 2020 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.