Adding a localization menu to WordPress
You may have noticed a small drop-down menu labelled 'Localisation' in the sidebar of this blog. It's something I've been playing about with since localising the Giraffe theme and, while it doesn't magically translate the entire blog, it does provide a viewer with a simple method to change the locale.
But what is a locale? In WordPress terms it refers to the framework around which your posts are displayed. That is, the words, phrases, dates, and times, that surround your posts. For example:



Try the live version now, if you want. You'll need a Chinese font to display the Chinese localisation, but the others should work fine.
Providing the viewer with this functionality is actually a very simple process. Unfortunately, because of the way WordPress works, it is not possible to put it into a plugin. Although the locale function is hooked into the plugin framework, the plugins are loaded after the localisation takes place, and by then it is too late to change it.
Instead, a bit of technical jiggery-pokery is required. In other words, you need to hack a little. The change is small though, and in a file that does not vary much between WordPress versions. It is not a difficult modification and just requires a text editor.
First an explanation of how localisation works. WordPress provides a localisation in the form of .mo files. These are like mini dictionaries, and allow WordPress to reference a translation by an English phrase. These files are downloaded from the WordPress localisation repository and are then installed into the /wp-includes/languages directory (you may need to create this as it does not exist by default). Finally, the WPLANG setting in the wp-config.php file is changed to reflect the chosen localisation.
For example, to get a Spanish localisation:
define(WPLANG, 'es_ES');
These changes will affect most of the administration interface, as well as several other parts of the blog (such as dates and times). However, to be fully localised, the theme also needs to support localisation.
Providing the viewer with the ability to choose a localisation does not give them the ability to understand your posts. However, it does make the site more navigable, and it gives it that additional personal touch.
So, edit wp-config.php and replace
define ('WPLANG','');
With:
if (isset ($_GET['lang']))
{
setcookie ('language', $_GET['lang'], time () + 60 * 60 * 24 * 2, '/');
define ('WPLANG', $_GET['lang']);
}
else if (isset ($_COOKIE['language']))
define ('WPLANG', $_COOKIE['language']);
else
define ('WPLANG', '');
You can modify the default language by changing the last definition of WPLANG. You can also change the cookie's expiry time by modifying the 60 * 60 * 24 * 2 - this is two days worth of seconds.
Now you can add a little bit of HTML to provide the options to the user:
<form method="get" action="/" style="padding: 1em">
<select name="lang" style="width: 50%">
<option value="">English</option>
<option value="it_IT">Italiano</option>
<option value="zh_CN">Chinese</option>
</select>
<input type="submit" style="border: 1px solid #999;" value="<?php _e('Go')?>"/>
</form>
This can be placed in any appropriate place in your blog. On this website I have added it to the sidebar (through the use of the sidebar-extra.php file in the Giraffe theme).
Naturally you will also need to install the .mo file for each language you want to provide.






