Textpattern tips, tutorials and code snippets

Multi-lingual sections

A few days ago, Jonathan asked me to write a tutorial based on my answer to mungy, a new member of the TXP forum, who needed help for a bilingual website. This tip follows on from Robert Wetzlmayr’s excellent tutorial on Multi-lingual articles in Textpattern.

Because I am a French TXP user, I chose to create a French/English weblog for the following example. By the end of this tutorial, you will be able to adapt it to your needs. Note that the main difference between my approach and that of Robert is that his emphasis is on single articles – this tip instead offers a multi-lingual site solution.

First, we assume you are using TXP 4.0.7+ in order to benefit from the new txp:variable magic tag. Also note that I use some PHP code snippet written by Robert Wetzlmayr (TXP Core Developer) for his high level in PHP programming.

Your weblog sections structure can be like this:

  • default
  • fr
  • blog
  • journal
  • search
  • recherche

…and so on..

Why this choice? Because we can associate all our articles by section depending on translations and we can also improve the SEO.

We assume in the follow example that your main language for your weblog is English. In the structure above, the “default” section is used to display your default English home page. Then, we will redirect your visitors to the corresponding pseudo French home page.

Create a form to detect language

variable(array('name' =>'accept-language', 'value' => 'en'));
if (preg_match('/(.*?)[,;-]/', $al, $a)) {
   variable (
       array (
             'name' =>'accept-language', 
             'value' => $a[1]

Create a form named “browser_language_detection” (type “misc”) put the above code in it, then save.

So now we have got a <txp:variable named accept-language with a value that has the client’s browser default language in it. Pretty useful, isn’t it?

Code for older TXP versions

$langs = explode(",", @$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$_SESSION['language'] = preg_replace('/(?:(?<=([a-z]{2}))).*/', '', $langs[0]);

This code may be used for older versions of TXP – 4.06 and below. The regex above keeps only the first 2 characters in the begin list of all the accepted browser languages. (i.e: for “en-us” we obtain “en”)

You can now use $_SESSION['language'] as a “variable” where you want to make a test.

At the top of all your templates, call your previous form (very important):

<txp:output_form form="browser_detection_language" />

In your “default” template, you can put this:

<txp:if_variable name="accept-language" value="en">
        echo parse ('
<txp:else />
        header('Location: '.hu.'fr'); 

Note. For TXP 4.0.6 and below users, here is the code:

if ( $_SESSION['language'] == 'en' ) {
    echo parse ('
} else {
    header('Location: '.hu.'fr'); 

Ok. All is fine. If you have lot of French friends, they are redirected to the “fr” section. Otherwise, all the other visitors will see your English articles within the “default” section (I hope you have also lots of English speaking friends too ;)

For the final touch, you can create some kind of flags menu to offer the choice to access to the different language sections:

<ul id="flags-lang" class="no-print">
        <img src="<txp:site_url />img/icons/fr.png" width="16" height="11" alt="Version Française" />
        <a href="<txp:site_url />fr" title="Version Française">Français</a>
        <img id="flat-gb" src="<txp:site_url />img/icons/gb.png" width="16" height="11" alt="English Version" />
        <a href="<txp:site_url />" title="English Version">English</a></li>

It’s time to publish. In this solution, you have different sections for the two languages. It is easy to associate your articles to the corresponding one. You have the advantage to optimize your specific SEO (i.e. xml lang attribute, meta keywords, meta description).

Here is a website that uses this technique.

My last advice. If you plan to create a professional multi-lingual corporate website like Apple.com, please choose the MLP plugin. This “poor solution” is not appropriate for this kind of project, but you can use my tutorial if you are working on Microsoft.com :D

Best regards, Mac users.

1 Comment Comment feed

I forgot to clarify that because comments are associated to individual articles, visitors’ comments are displayed in the corresponding language too.

Add a comment

Use Textile help to style your comments