How to: Create a Multi-Language Blog

Using WordPress plugins to automatically translate blog content
This is the easiest solution. A few WordPress plugins allows you to put some clickable flags in your sidebar, that will automatically translate your content to another language using, for example, Google Translate.
This is a great solution if you’re looking for a way to make your content available for people that doesn’t speak your language, but you guessed it, the automatic translation isn’t as good as a human translated text.

Anyways, if you’re interested in theses plugins, here’s a small list. I didn’t tried them all myself, though.

Using categories and a few hacks

If you’re a perfectionist, you probably don’t really like the automatic translations provided by Google. In my opinion, if it’s okay for a personal blog, but automatic translations should be avoided on a professional blog.

For this hack, we will use categories. Go to your dashboard and create categories/sub-categories following the example below:

English
    One category
    Two categories
Français
    Une catégorie
    Deux catégories
Deutsch
    Eine kategorie
    Zwei kategorien

Of course this is just an example, you can add as many categories as you want. The thing to remember here is that we’re using top level categories for languages, each of them having sub-categories organized by topics.

Now, you should post at least one example post in each sub-category. Once you’re done, we can start editing our header.php template.

The idea is quite simple: First, we must display the available languages, and then, a menu with the sub-category related to the current language. We must also think about defining a default language, which will be English in this example.

1 – Creating the language menu
Insert the following code where you want the language selection menu to appear:

<?php
wp_list_categories('title_li=');
$lang = 1; // Sets the default language
?>

As you can see, we just need the good old wp_list_categories() function to get our available languages.
Then, we have to keep a php variable containing the id of the top level category, in order to display the navigation menu in the selected language. We must give a default value to the $lang variable. This default value will be the category id of the language you want to set as default.

I noticed that many people should have trouble to find the ID of a particular category. If you do, you should definitely read this post.

2 – Navigation menu
Now, we got a php variable containing the id of the mother category. As the top-level categories are languages, we know which language the user is using.

We simply have to display second-level categories.

<ul>
<?php
wp_list_categories('title_li=&child_of=$lang');
?>
</ul>

3 – Retrieving category
Here’s a sample code you can use to automatically list posts from a category. This code get the current category (in this example, the language) and then displays the related posts.

<?php foreach((get_the_category()) as $category) {
      	$thecat = $category->cat_ID . ' ';
    	query_posts('child_of='.$thecat);
	if (have_posts()) : while (have_posts()) : the_post();
	   //Classic WP loop
	endwhile;endif;
?>
  • Leave a Comment