WordPress.org

Ready to get started?Download WordPress

Forums

Date (Calendar System) Localization (3 posts)

  1. kambiz.k
    Member
    Posted 5 years ago #

    There are some plugins that use the_date filter to localize post's date for different calendar systems (e.g. Jewish, Hijri, Jalali, and so on), however what about localizing dates shown by plugins or in admin area?

    In the current implementation of WordPress (2.6.2), there is no way to globaly change the calendar system. But it is really easy to achieve that and needs only some minor changes in mysql2date and date_i18n functions and involving one or two filters.

    Here is my suggestion base on version 2.6.2:

    function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
    	global $wp_locale;
    	$m = $mysqlstring;
    	if ( empty( $m ) )
    		return false;
    
    	if( 'G' == $dateformatstring ) {
    		return gmmktime(
    			(int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ),
    			(int) substr( $m, 5, 2 ), (int) substr( $m, 8, 2 ), (int) substr( $m, 0, 4 )
    		);
    	}
    
    	$i = mktime(
    		(int) substr( $m, 11, 2 ), (int) substr( $m, 14, 2 ), (int) substr( $m, 17, 2 ),
    		(int) substr( $m, 5, 2 ), (int) substr( $m, 8, 2 ), (int) substr( $m, 0, 4 )
    	);
    
    	if( 'U' == $dateformatstring )
    		return $i;
    
    	if ( -1 == $i || false == $i )
    		$i = 0;
    
    	if ( $translate )
    		$j = date_i18n( $dateformatstring, $i );
    	else
    		$j = @date( $dateformatstring, $i );
    
    	/*
    	if ( !$j ) // for debug purposes
    		echo $i." ".$mysqlstring;
    	*/
    
    	return $j;
    }
    function date_i18n( $dateformatstring, $unixtimestamp ) {
    	global $wp_locale;
    	$i = $unixtimestamp;
    	// Sanity check for PHP 5.1.0-
    	if ( -1 == $i )
    		$i = false;
    
    	$j = apply_filters( 'pre_date_i18n', $i, $dateformatstring );
    	if ($j !== $i)
    		return $j;
    
    	if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
    		$datemonth = $wp_locale->get_month( date( 'm', $i ) );
    		$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
    		$dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
    		$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
    		$datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
    		$datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
    		$dateformatstring = ' '.$dateformatstring;
    		$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
    		$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
    		$dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
    		$dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
    		$dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
    		$dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
    
    		$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
    	}
    	$j = @date( $dateformatstring, $i );
    	return apply_filters( 'date_i18n', $j, $dateformatstring );
    }

    BTW, the above code is fully compatible with plugins that use the_date filter to localize date.

  2. bluebird2
    Member
    Posted 5 years ago #

    I think this is a necessary change. Please implement this suggestion.

  3. kambiz.k
    Member
    Posted 5 years ago #

    For this enhancement, I have created a ticket and submitted a patch on the WordPress Trac.
    http://trac.wordpress.org/ticket/7753

Topic Closed

This topic has been closed to new replies.

About this Topic