• Hey guys I have a real dilemma. I’m working on a site that need to be bilingual and I seem to have everything worked out.

    The only thing is that the sidebar it all in english and I’d like to preserve the dynamic nature of the archives.

    So wp_get_archive gets the month and link to that month. Is there a way for me to change the outputted string to spanish using PHP. Of course only when in categoty-3 which is the spanish side.

    Also I guess I’d have to make that link bring back post in that month only for category-3(spanish).

    here is the website:
    http://www.casavidanueva.com/wp/category/spanish/

    Thanks guys in advanced for the help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter 799827

    why wouldn’t something like this work?

    <?php $mes = wp_get_archives();
    $new = str_replace("January","Enero",$mes);
    echo $new;
    ?>

    I am trying to do something similar, only with Arabic.

    krisnrg: Unfortunately, since wp_get_archives automatically echos the date display, this will not work.

    So far, the only solution I can think of is to replicate the wp_get_archives function, but without the automatic print.

    Update: Actually, looks like the “echo” option is either due to be added soon or was added in 2.6. See: http://trac.wordpress.org/ticket/5654

    The output of wp_get_archives can be localized, you just need to install a language file.

    See http://codex.wordpress.org/WordPress_Localization

    Unfortunately, I am using WordPress MU, so I’m not sure a localized version would work.

    Also, I imagine these won’t work for multi-lingual blogs as well, so this may not work for krisnrg. Right?

    If you are using MU – post and ask in their forum:
    http://mu.wordpress.org/forums

    I believe the solution would be same for a multi-language single blog as it would be for WordPress MU.

    In any event, I manage to figure something out. It isn’t ideal, but it seems to work.

    First, you’ll need a new version of wp_get_archives that adds an echo option. This could be added as a plugin or just in custom functions.php file for your template.

    Here is the code I used (basically just copied from the ticket I mentioned above):

    <?php
    
    function wp_get_archives_new($args = '') {
    	global $wpdb, $wp_locale;
    
    	$defaults = array(
    		'type' => 'monthly', 'limit' => '',
    		'format' => 'html', 'before' => '',
    		'after' => '', 'show_post_count' => false,
        'echo' => 1
    	);
    
    	$r = wp_parse_args( $args, $defaults );
    	extract( $r, EXTR_SKIP );
    
    	if ( '' == $type )
    		$type = 'monthly';
    
    	if ( '' != $limit ) {
    		$limit = absint($limit);
    		$limit = ' LIMIT '.$limit;
    	}
    
    	// this is what will separate dates on weekly archive links
    	$archive_week_separator = '–';
    
    	// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
    	$archive_date_format_over_ride = 0;
    
    	// options for daily archive (only if you over-ride the general date format)
    	$archive_day_date_format = 'Y/m/d';
    
    	// options for weekly archive (only if you over-ride the general date format)
    	$archive_week_start_date_format = 'Y/m/d';
    	$archive_week_end_date_format	= 'Y/m/d';
    
    	if ( !$archive_date_format_over_ride ) {
    		$archive_day_date_format = get_option('date_format');
    		$archive_week_start_date_format = get_option('date_format');
    		$archive_week_end_date_format = get_option('date_format');
    	}
    
      // Place to accrue text if we're not echoing...
      $output = ''; 
    
    	//filters
    	$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    	$join = apply_filters('getarchives_join', "", $r);
    
    	if ( 'monthly' == $type ) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ( $arcresults ) {
    			$afterafter = $after;
    			foreach ( $arcresults as $arcresult ) {
    				$url	= get_month_link($arcresult->year,	$arcresult->month);
    				$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    				if ( $show_post_count )
    					$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    				$output .= get_archives_link ( $url, $text, $format, $before, $after );
    			}
    		}
    	} elseif ('yearly' == $type) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code>year</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ($arcresults) {
    			$afterafter = $after;
    			foreach ($arcresults as $arcresult) {
    				$url = get_year_link($arcresult->year);
    				$text = sprintf('%d', $arcresult->year);
    				if ($show_post_count)
    					$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    				$output .= get_archives_link ( $url, $text, $format, $before, $after );
    			}
    		}
    	} elseif ( 'daily' == $type ) {
    		$query = "SELECT DISTINCT YEAR(post_date) AS <code>year</code>, MONTH(post_date) AS <code>month</code>, DAYOFMONTH(post_date) AS <code>dayofmonth</code>, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ( $arcresults ) {
    			$afterafter = $after;
    			foreach ( $arcresults as $arcresult ) {
    				$url	= get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
    				$date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
    				$text = mysql2date($archive_day_date_format, $date);
    				if ($show_post_count)
    					$after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
    				$output .= get_archives_link ( $url, $text, $format, $before, $after );
    			}
    		}
    	} elseif ( 'weekly' == $type ) {
    		$start_of_week = get_option('start_of_week');
    		$query = "SELECT DISTINCT WEEK(post_date, $start_of_week) AS <code>week</code>, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $limit";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		$arc_w_last = '';
    		$afterafter = $after;
    		if ( $arcresults ) {
    				foreach ( $arcresults as $arcresult ) {
    					if ( $arcresult->week != $arc_w_last ) {
    						$arc_year = $arcresult->yr;
    						$arc_w_last = $arcresult->week;
    						$arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
    						$arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
    						$arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
    						$url  = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', get_option('home'), '', '?', '=', $arc_year, '&amp;', '=', $arcresult->week);
    						$text = $arc_week_start . $archive_week_separator . $arc_week_end;
    						if ($show_post_count)
    							$after = '&nbsp;('.$arcresult->posts.')'.$afterafter;
    						$output .= get_archives_link ( $url, $text, $format, $before, $after );
    					}
    				}
    		}
    	} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
    		('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC ";
    		$query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
    		$key = md5($query);
    		$cache = wp_cache_get( 'wp_get_archives' , 'general');
    		if ( !isset( $cache[ $key ] ) ) {
    			$arcresults = $wpdb->get_results($query);
    			$cache[ $key ] = $arcresults;
    			wp_cache_add( 'wp_get_archives', $cache, 'general' );
    		} else {
    			$arcresults = $cache[ $key ];
    		}
    		if ( $arcresults ) {
    			foreach ( $arcresults as $arcresult ) {
    				if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
    					$url  = get_permalink($arcresult);
    					$arc_title = $arcresult->post_title;
    					if ( $arc_title )
    						$text = strip_tags(apply_filters('the_title', $arc_title));
    					else
    						$text = $arcresult->ID;
    					$output .= get_archives_link ( $url, $text, $format, $before, $after );
    				}
    			}
    		}
    	}
    
      if ( $echo )
        echo $output;
      else
     	  return $output;
    }
    
    ?>

    Then, I created a function to translate the months (I am working in Farsi, but Spanish should work just as well). You could place this in either the plugin or the functions.php file I mentioned.

    //translate_date()
    //Gets the featured post and outputs the html
    function translate_date($content, $language_option) {
      $english = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    
      if($language_option = "farsi")
        $language = array('ژانویه', 'feb', 'مارس', 'آوریل', 'مه', 'ژوئن', 'ژوئیه', 'اوت', 'سپتامبر', 'اکتبر', 'نوامبر', 'دسامبر');
    
      return str_replace($english, $language, $content);
    
    }

    Finally, this is the code in the sidebar of my template:

    echo translate_date(wp_get_archives_new('type=monthly&show_post_count=1&echo=0'), "farsi");

    Now, this isn’t perfect, because you’ll have to call the function in a bunch of other places, such as the headers of archive pages. Since the_time() automatically echos, you need to replace that. Here is the code I used for the_time():

    echo translate_date(apply_filters('the_time', get_the_time("F"), "F"), "farsi");

    The “F” is the type of date display, in this case just the full month. You could also do “j F Y”, which would look like “1 Enero 2008”. Just make sure to change the date display in both places.

    I hope this helps.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘wp_get_archives change the output string to spanish’ is closed to new replies.