Configure wordpress for multiple languages
Introduction
This article will show how to configure a website with a wordpress blog for multiple languages. The concept behind this development is to provide the same pages in different languages, you will need to rewrite each article in each language. You may wish to have this functionality if your website is heavily used by more than one language. In this case automatic translations are not an option as they are not accurate enough.
Technologies used
- HTTP
- PHP
- XHTML
- Wordpress
Most browsers include a language preference in the HTTP request that gets sent to the web server to request a web page.
Requirements of code
- Define a default language. Incase the language setting not be made available in a HTTP request.
- Dynamically switch the default language. For HTTP requests with the language setting.
- Switch the current language. This is especially useful if a user does not speak the default languages, but has a preferred language to read.
Getting Started
We'll start by enabling the script in your website, then we'll incorporate the class into wordpress.
1. Define a php include script
The following will instruct your website to include a script called common_variables.php in each page.
<?php
include('common/common_variables.php');
?>
2. Create the include directory
Create a directory called common, this is where you will put the following scripts.
3. Create the include script
Create a php file called common_variables.php and paste this code
<?php
session_start();
if(isset($_SESSION['lang'])===false)
{
include("detect_default_language.php");
$_SESSION['lang'] = getlang();
}
$curlang = $_SESSION['lang'];
?>
The purpose of this script is to ensure all pages create and use a session.
It opens the users session file and checks for a variable called "lang".
If this variable is set a local variable is created and populated with the current language setting.
Otherwise a it includes a script to detect the language and populate the "lang" variable.
4. Create the default language selection script
Create a php file called detect_default_language.php and paste this code
<?php
function getlang()
{
$langs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$pos1 = strpos($langs,'en');
if($pos1===false){$pos1=9999; }
$pos2 = strpos($langs,'fr');
if($pos2===false){$pos2=9999; }
/**********************************************************************/
if($pos2<$pos1) {$ret_lang = 'fr'; }
elseif($pos2>$pos1) {$ret_lang = 'en'; }
else {$ret_lang = 'fr'; }
/**********************************************************************/
return $ret_lang;
}
?>
This script detects "preferred" languages from the users HTTP request.
If there is no default language declared"else {$ret_lang = 'fr'; }" defines the language to use.
If there is a default language declared it checks which one of the available languages is declared first.
Once the language is detected this script parses the this setting back to the requestor (our script "common_variables.php").
5. Create the language selection script
Create a php file called change_language.php and paste this code
<?php
session_start();
$oldurl = $_GET['oldurl'];
$curlang=$_SESSION['lang'];
if($curlang=='fr')
{
$curlang='en';
$_SESSION['lang']='en';
}
else
{
$curlang='fr';
$_SESSION['lang']='fr';
}
header("Location: http://".substr($oldurl,0,strlen($oldurl)-9));
?>
The final script, we want to allow the user to change the current language setting.
The above script toggles between two languages and returns to the last page the user was browsing.
You can call this script with an anchor link or a form.
example call from an anchor link:
<?php
echo '<a href="common/change_language.php?oldurl='.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'" title="Change Language">Change Language</a>';
?>
example call from a form:
<?php
echo '<form action="common/change_language.php">
<input type="hidden" name="oldurl" value="'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'" />
<input type="submit" value="Change Language" />
</form>';
?>
6. Integrate with Wordpress
Now we have all the script required and have integrated it with our website, the next part is to include this in wordpress. To do this we simply call our script and define a custom table prefix in the config file. This forces wordpress to use different table prefixes dependant on the current language selection.
Example Wordpress config file (wp-congig.php)
<?php
include("../common/common_variables.php");
define('DB_NAME', 'database');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
if($curlang=='en')
{
$table_prefix = 'en_wp_';
define ('WPLANG', '');
}
else
{
$table_prefix = 'fr_wp_';
define ('WPLANG', 'fr_FR');
}
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
require_once(ABSPATH . 'wp-settings.php');
?>
Include the script in your wordpress index files
You will also need to include your class to select the right langauge in the index files. Wordpress has two of these files you will need to update on in the root directory and one in the wp-admin directory. include("../common/common_variables.php");
Final stage
Now all you need to do is navigate to your wordpress blog (in a new language) and it will automatically enter setup mode and install the new tables.
Comment
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sakuiwe1/public_html/articles/wordpress_language/index.php on line 233
