Thursday, December 6, 2012

Simplest Way To Create a Multilingual Website.

Are you interested in having a multilingual website? This is a tutorial that shows you how you can do that in PHP.


php-multi-language-site

php-multi-language-site


The first thing we need to do is to create a couple of files that will contain the text for each of the languages that will be supported by the website. For demo purpose I have chosen English, Spanish and German. Make a directory named “directory”. In this directory create 3 files: lang.de.php, lang.en.php, and lang.es.php. In our main file (index.php) we will include a file (common.php) containing a piece of code that gets the requested language. Here’s the content of those 3 language files:

lang.en.php


01.<?php

02./*

03.------------------

04.Language: English

05.------------------

06.*/

07.

08.$lang = array();

09.

10.$lang['PAGE_TITLE'] = 'My website page title';

11.$lang['HEADER_TITLE'] = 'My website header title';

12.$lang['SITE_NAME'] = 'My Website';

13.$lang['SLOGAN'] = 'My slogan here';

14.$lang['HEADING'] = 'Heading';

15.

16.// Menu

17.

18.$lang['MENU_HOME'] = 'Home';

19.$lang['MENU_ABOUT_US'] = 'About Us';

20.$lang['MENU_OUR_PRODUCTS'] = 'Our products';

21.$lang['MENU_CONTACT_US'] = 'Contact Us';

22.$lang['MENU_ADVERTISE'] = 'Advertise';

23.$lang['MENU_SITE_MAP'] = 'Site Map';

24.?>



lang.es.php


01.<?php

02./*

03.-----------------

04.Language: Spanish

05.-----------------

06.*/

07.

08.$lang = array();

09.

10.$lang['PAGE_TITLE'] = 'Título de la página de mi sitio web';

11.$lang['HEADER_TITLE'] = 'Mi sitio web de la cabecera título';

12.$lang['SITE_NAME'] = 'Mi Sitio Web';

13.$lang['SLOGAN'] = 'Mi lema aquí';

14.$lang['HEADING'] = 'Título';

15.

16.// Menu

17.

18.$lang['MENU_HOME'] = 'Inicio';

19.$lang['MENU_ABOUT_US'] = 'Sobre Nosotros';

20.$lang['MENU_OUR_PRODUCTS'] = 'Nuestros productos';

21.$lang['MENU_CONTACT_US'] = 'Contáctenos';

22.$lang['MENU_ADVERTISE'] = 'Publicidad';

23.$lang['MENU_SITE_MAP'] = 'Mapa del Sitio';

24.?>



lang.de.php


01.<?php

02./*

03.-----------------

04.Language: German

05.-----------------

06.*/

07.

08.$lang = array();

09.

10.$lang['PAGE_TITLE'] = 'Meine Webseite Titel';

11.$lang['HEADER_TITLE'] = 'Meine Website-Header Titel';

12.$lang['SITE_NAME'] = 'Meine Website';

13.$lang['SLOGAN'] = 'Mein Slogan hier';

14.$lang['HEADING'] = 'Position';

15.

16.// Menu

17.

18.$lang['MENU_HOME'] = 'Heim';

19.$lang['MENU_ABOUT_US'] = 'Ãœber uns';

20.$lang['MENU_OUR_PRODUCTS'] = 'Unsere Produkte';

21.$lang['MENU_CONTACT_US'] = 'Kontaktieren Sie uns';

22.$lang['MENU_ADVERTISE'] = 'Werben';

23.$lang['MENU_SITE_MAP'] = 'Site Karte';

24.?>



As you can notice, some constants are created using the define() function. In every file the defined constants have the same name, bu the values is different. We will output the values of the constants inside the index.php file. Therefore we will see different text every time we will call other language file.

Determine the right language


Let’s analyze the common.php file:


01.<?php

02.session_start();

03.header('Cache-control: private'); // IE 6 FIX

04.

05.if(isSet($_GET['lang']))

06.{

07.$lang = $_GET['lang'];

08.

09.// register the session and set the cookie

10.$_SESSION['lang'] = $lang;

11.

12.setcookie('lang', $lang, time() + (3600 * 24 * 30));

13.}

14.else if(isSet($_SESSION['lang']))

15.{

16.$lang = $_SESSION['lang'];

17.}

18.else if(isSet($_COOKIE['lang']))

19.{

20.$lang = $_COOKIE['lang'];

21.}

22.else

23.{

24.$lang = 'en';

25.}

26.

27.switch ($lang) {

28.case 'en':

29.$lang_file = 'lang.en.php';

30.break;

31.

32.case 'de':

33.$lang_file = 'lang.de.php';

34.break;

35.

36.case 'es':

37.$lang_file = 'lang.es.php';

38.break;

39.

40.default:

41.$lang_file = 'lang.en.php';

42.

43.}

44.

45.include_once 'languages/'.$lang_file;

46.?>



After we determine the value of $lang, we use switch() to compare its value with some different values, and execute a different piece of code depending on which value it equals to. After the value of the $lang_file is determined, the script will include the necessary language file. As you can see I have used sessions to register the value of $lang. This way users can navigate through the whole site and see the content in the chosen language (lang=[language here] does not need to be passed in every URL). Additionally, I have used cookies to store the selected language in users computer for 30 days. When the visitor will come back he will see the site in the language that he previously selected.

How if the website’s language requested?


In this demo I have chosen to use some image flags, each image having a link to index.php?lang=[LANG HERE]. So, to see the site in german we will use the German image flag which links to index.php?lang=de.

Lastly, the constants values should be outputted in the page. Examples:

for document title


1.<title><?php echo $lang['PAGE_TITLE']; ?></title>



for header menu


1.<ul>

2.<li><a href="/"><?php echo $lang['MENU_HOME']; ?></a></li>

3.<li><a href="about_us"><?php echo $lang['MENU_ABOUT_US']; ?></a></li>

4.<li><a href="our_products"><?php echo $lang['MENU_OUR_PRODUCTS']; ?></a></li>

5.<li><a href="contact_us"><?php echo $lang['MENU_CONTACT_US']; ?></a></li>

6.<li><a href="advertise"><?php echo $lang['MENU_ADVERTISE']; ?></a></li>

7.<li><a href="sitemap"><?php echo $lang['MENU_SITE_MAP']; ?></a></li>

8.</ul>







No comments:

Post a Comment