Wordpress 4.4 indroduced a way to display the genitive case of a months name for specific locale by using the function date_i18n
(:32) which is filtered by wp_maybe_decline_date()
(.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!
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/ticket/11226#comment:32) which is filtered by wp_maybe_decline_date()
(https://core.trac.wordpress/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!
Share Improve this question edited Sep 29, 2016 at 7:56 mlimon 4002 silver badges11 bronze badges asked Sep 29, 2016 at 6:12 vadimsvadims 3531 gold badge4 silver badges13 bronze badges2 Answers
Reset to default 3Although, 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.
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()); ?>