In: , , , , ,
On: 2004 / 05 / 11 Viewed: 34850 times

Bilingual ?The ability to maintain a weblog in two (or more) languages is an often requested feature, especially by people like me who just can't make up their mind between French and English :)
I've come up with a very easy way to implement this in an existing website, as you can see by yourself if you click on the tiny flags. I've also added cookie support, so you just need to chose a language once. This may not be *the* perfect solution, it's just one way, and a very simple one to add in an existing site.


The trick : you have two div's, one with attribute "lang=fr", one with "lang=en". A small javascript hides every divs with a particular "lang" and shows the others.

The javascript part

First, put the following code in your index.php, or in an external javascript file :

JavaScript:
  1. function createCookie(name,value,days) {
  2.   if (days) {
  3.     var date = new Date();
  4.     date.setTime(date.getTime()+(days*24*60*60*1000));
  5.     var expires = "; expires="+date.toGMTString();
  6.   }
  7.   else expires = "";
  8.   document.cookie = name+"="+value+expires+"; path=/";
  9. }
  10.  
  11. function readCookie(name) {
  12.   var nameEQ = name + "=";
  13.   var ca = document.cookie.split(';');
  14.   for(var i=0;i <ca.length;i++) {
  15.     var c = ca[i];
  16.     while (c.charAt(0)==' ') c = c.substring(1,c.length);
  17.     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  18.   }
  19.   return null;
  20. }
  21.  
  22. function language(lang_on, lang_off) {
  23.   createCookie("langue_on",lang_on,365);
  24.   createCookie("langue_off",lang_off,365);
  25.     for (var i=0; i<document.getElementsByTagName("div").length; i++ ) {
  26.       if (document.getElementsByTagName("div")[i].lang == lang_on) {
  27.         document.getElementsByTagName("div")[i].style.display="block";
  28.       }
  29.       if (document.getElementsByTagName("div")[i].lang == lang_off) {
  30.         document.getElementsByTagName("div")[i].style.display="none";
  31.       }
  32.     }
  33. }
  34.  
  35. function startlanguage() {
  36.   var notdefined;
  37.   var lang_on = readCookie("langue_on");
  38.   var lang_off = readCookie("langue_off");
  39.   if (lang_on == notdefined) {lang_on = "fr";}
  40.   if (lang_off == notdefined) {lang_off = "en";}
  41.   language(lang_on,lang_off);
  42. }
  43.  
  44. window.onload = function () {startlanguage();}

A few explanations

The first two functions, createCookie() and readCookie(), store cookies and read cookies so the user can set for once a preferred language. If you are already using a rather lengthy javascript file, chances are you already have cookie functions. In this case, no need to duplicate things, just modify the lines above to use your functions.

The next function, language(), is the core of the thingie. First, when called, it creates two cookies : one for language to display, one for language to hide. Then, it "loops" through all div elements of the pages, and toggle their display according to the parameters.
(You're wondering, Why not a simpler function with just one parameter ? Well, first, I suck at javascript, and second, so it is simpler to extend the script to support three or more languages)

Next is startlanguage() : thanks to the last line, this function is called when the page loads, and its purpose is to set the language preferences : either there is cookies, or a default value is set (here, default is "display french and hide english")

Embed in your site

Add two links with the following code :

JavaScript:
  1. <a href="javascript:language('fr','en')" title="Version Francaise">En Fran&ccedil;ais</a>
  2. <a href="javascript:language('en','fr')" title="English Version">In English</a>

And, for example, two divs :

HTML:
  1. <div lang="en">Hello World !</div>
  2. <div lang="fr">Salut Poupoule !</div>

And that's it.

Use this in WordPress

To use this in WordPress, you simply have to write your posts with the div's. If XHTML validation is an important matter to you, you'll have to write a bit of HTML tags so that when WP automatically adds

and so on, you still validate. A validating example is following :

HTML:
  1. <div lang="en"><p>Include an opening "p" tag in the first line from first paragraph.
  2.  
  3. Another line goes here. And as paragraphs as you want.
  4.  
  5. And don't forget to close the "p" it at the end.</p></div>
  6. <div lang="fr"><p>A la premiere ligne du premier paragraphe, incluez un tag "p".
  7.  
  8. Autant de lignes et de paragraphes que vous le souhaitez, bien s&ucirc;r.
  9.  
  10. Et avant de terminer la div, fermez le "p" que vous avez ouvert.</p></div>

The only real difficulty is that long posts become pretty huge when you type them twice to translate :)

Improvements ?

Instead of a simple default value for lang_on and lang_off, I think I'll add a language guess function. It could be guessed from referrer IP, or user's remote address, or maybe with browser's information, I still have to think a bit about this :)

You could also add loops to the main javascript function, so it will not toggle only div elements, but anything you'd need : p, img, span. Actually, working with span elements could be much more simple than with div's.

Related posts

Metastuff

This entry "A Solution for a Bilingual Wordpress" was posted on 11/05/2004 at 8:49 pm and is tagged with , , , , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

25 Blablas

    Pages: « 1 [2] Show All

  1. 21
    Rodrigo Chile »
    wrote, on 25/Aug/06 at 6:41 pm # :

    Hi,

    I didn't understand where to put "Embed in your site" section code... Can somebody tell me where I should put that portion of code?

    Thanks

  2. 22
    namakemono » Archiwum » Dwuj... Germany »
    pingback on 25/Jan/07 at 6:52 am # :

    [...] Rozwiązanie znalazłem bardzo szybko, a jego prostota była dla mnie na tyle wystarczająca by nie szukać dalej. [...]

  3. 23
    Urls Sinistras » Blog Archive &raq... United States »
    pingback on 05/Nov/07 at 11:08 am # :

    [...] A Solution for a Bilingual WordpressThe ability to maintain a weblog in two (or more) languages is an often requested feature, especially by people like me who just can’t make up their mind between French and English [...]

  4. 24
    Mr Surbade France »
    wrote, on 18/Dec/07 at 3:03 pm # :

    "Instead of a simple default value for lang_on and lang_off, I think I'll add a language guess function. It could be guessed from referrer IP, or user's remote address, or maybe with browser's information, I still have to think a bit about this :)"

    I found this : http://erik.range-it.de/wordpress/plugins/getbrowserlanguage/

  5. 25
    Pup Argentina »
    thought, on 09/Feb/08 at 8:20 am # :

    Genius! A lot of people having trouble with multi-lang-posting around the web and waiting for development of complex plugins. This simple solution worked sweet for me.

Pages: « 1 [2] Show All

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It