Menu

Create your website multilingual – Second way

30 Mai 2011 - PHP

Yesterday I posted you an easy way to make your website multilingual. There are several ways to do this. Today I wanted to post youa second way to create a multilingual website. The method from yesterday was a version to use, if you want to translate your whole website and to have the possibility to insert also different looks.

If you want to translate only text parts, you can use translation property files. These are read to the session and inserted from the session to your website.I use both ways on my own websites. The way from yesterday I use for the content on my website, the version from today I use for my menu and the links in the page footer on the website, because this version is more usefull for small text parts. The menu on my site is shown in this look:

 

 

Like you see, I have the flags for germen and english in the menu. When I press the english flag, the menu texts are read from an english text properties file to the session and shown on page.

 

 

The method for reading the files is quit easy. You must have the properties files all in the same folder. I named them is this way:

  • language_DE.properties
  • language_EN.properties

In both files are the same properties stored in, but the value of each property is the translation in the given language, the key is in all files the same:

German:

# Links
home=Home
aboutus=Über uns
blog=Blog
references=Referenzen
services=Leistungen
prices=Preise
modules=HP Module
specials=Sonderangebote
contact=Kontakt
imprint=Impressum
links=Links

English:

# Links
home=Home
aboutus=About us
blog=Blog
references=References
services=Services
prices=Prices
modules=HP Modules
specials=Specials
contact=Contact
imprint=Imprint
links=Links

The keys of the properties are also the keys to read it from session array.

function readLanguageFile(&$language) {
  $file = "./language_".$language.".properties";
  $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  for($i = 0; $i < count($lines); $i++) {
    if(substr($lines[$i], 0, 1) != '#') {
      $splitted = explode("=", $lines[$i]);
      $_SESSION[$splitted[0]] = $splitted[1];
    }
  }
}

This method reads all properties directly in the session. It ignores empty lines and commented line with the # sign. I call this method on each page call. So I always read the correct language after every link click. The language is read in the same way like in the post before from the GET parameter.

Then you can use the language snippets in this way:

<a href="index.php?lang=<?php echo $language; ?>"><?php echo $_SESSION['home']; ?></a>
<a href="ueberuns.php?lang=<?php echo $language; ?>"><?php echo $_SESSION['aboutus']; ?></a>

The $language variable can be edited by the flag links, which are only changing the GET parameter in the URL.

Print Friendly, PDF & Email

3 thoughts on “Create your website multilingual – Second way

mobile website development

Your blog is very good,very novel,let I learned a lot from,thank you very much.if you have time also attention our website.

Reply
hp drivers

Very nice post, I surely love this site, keep it up.

Reply
Svetlana tkachenko

No. Don’t reinvent the wheel. There is a JavaScript library to do this, used, for example, in all Firefox OS applications. L10n.js. Can not recall full name; it is included in Gaia repository.

Reply

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.