WordPress MultiLanguage Support the Simple Way

created September 14, 2012, last updated September 14, 2012.

.
closeThis post was last updated 11 years 6 months 8 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

WordPress is a great multilanguage CMS, for a while I thought of it purely as a blogging tool but now I use it all the time when I need to build a quick and effective marketing site. If you find a theme that you like and is adaptable you can put together a professional, great looking site that is easy for you or your clients to maintain in very little time.

Getting WordPress to work well with multi language content isn’t so straight forward, there are a lot of plugins available that offer multi language solutions but these seemed to be over complex for what I wanted.

My WordPress marketing sites are relatively simple with just a few pages used as marketing landing pages and stepping stones to e-commerce sites. I already created a simple method in PHP of detecting browser language and matching language to content in my pages however the WordPress frontend language remained in the default language of the installation. I did not want to modify core template files within WordPress as this makes upgrading difficult so after wasting time with Plugins I developed a very simple solution that sets the WordPress interface language for each visitor based on their browser language.

The WordPress WP-CONFIG.php file holds the main configuration settings for your WordPress installation, a global PHP constant WPLANG is defined here that sets the admin and frontend interface language using standard country and language codes, the default if WPLANG is not specified is American English en-US.

WordPress language packs come in .mo files that are located in the WordPress wp-content/languages folder, they are named after the language code, i.e. en_US.mo, de_DE.mo etc.

By detecting the browser language of the visitor and then checking if a corresponding .mo file exists in the languages folder we can automatically switch the WordPress interface language to match the visitors browser language – nice!

Decide which languages you want to support and download the corresponding .mo files, you can find all the language files available in the WordPress language file repository here http://svn.automattic.com/wordpress-i18n/ e.g. here are the German language files http://svn.automattic.com/wordpress-i18n/de_DE/branches/2.8/messages/

If your WordPress site does not have a wp-content/languages folder, create a new one and copy your language files into it. Note you should copy your default language file too.

Now edit the WP-CONFIG.php file in the root folder of your WordPress installation and find the WP-LANG constant definition and comment it out:

#define (‘WPLANG’, ”);

and below it paste in the following code

/**
 * Set language and load language files
 */
function detectlanguage() {
    $langcode = explode(";", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $langcode = explode(",", $langcode['0']);
    return $langcode['0'];
    }

function fileExists($fileName, $caseSensitive = true) {

    if(file_exists($fileName)) {
        return $fileName;
    }
    if($caseSensitive) return false;

    // Handle case insensitive requests            
    $directoryName = dirname($fileName);
    $fileArray = glob($directoryName . '/*', GLOB_NOSORT);
    $fileNameLowerCase = strtolower($fileName);
    foreach($fileArray as $file) {
        if(strtolower($file) == $fileNameLowerCase) {
            return $file;
        }
    }
    return false;
}	

$languageDefault='en_US';
$language = detectlanguage();

if (isset($_GET['set_lang'])) { $language = $_GET['set_lang']; }

$languageFile= 'wp-content/languages/'. str_replace('-','_',$language). '.mo';
$wpLanguageFile=fileExists($languageFile,false);

// check if WP language file exists for browser language
if ($wpLanguageFile) {
	$wpLanguageFile = basename($wpLanguageFile, '.mo');
	// language supported
	define ('WPLANG', $wpLanguageFile);
    //echo "Language - $language detected. Language file exists and is supported."; 
} else {
	// language not supported default to EN
    //echo "Language - $language detected. Language file does not exist - defaulting to $languageDefault."; 
	define ('WPLANG', $languageDefault);
}

Save the file and test. You can force the language selection by specifying it in the URL, i.e. http://yourwordpresssite.com/?set_lang=de_DE which will force the interface language to German if you have the German language files installed. Uncomment the echo’s and you will see the language selection confirmed.

If all is working well the WordPress interface language will now switch to the browser language of your visitors.

Note that this will only change the language of the built in WordPress interface in the frontend or backend. Your content language will not change, you need to create a translation system for your content using portable object files, or alternatively use the Google translation tool.

 

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.