Jump to content

Creating Multilingual PHP Website


Jungchen

Recommended Posts

Hi

I'm new to PHP and I would like to build a multilingual website.

I would like to have some sort of site with an intro screen where the user can choose a language (among 5). To be perfect, the user's choice should be stored in a cookie.

Then the index.php page would come up. On that page I would like to have a language menu somewhere, where the language can be changed instantly (maybe changing the cookie).

 

All language strings would be stored in files like this : lang/en.php , lang/fr.php containing code as follows :

 

     

 $TEXT['welcometitle'] = "Welcome";
$TEXT['welcome1'] = "Hello and welcome to my site";

 

Inside the webpages text would be called with an echo:

 

<h1><?php echo $TEXT['welcometitle']; ?></h1>
<p><?php echo $TEXT['welcome1']; ?></p>

 

My problem is that I do not know how to create the cookie on the user side and how to create a function allowing the change of language.

 

I'm sure some of you know how to do this :o)

 

thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/122029-creating-multilingual-php-website/
Share on other sites

I'm sure you know how to create a cookie with setcookie(). Based on that cookie you include the language file and use get variables to change it. The basic idea:

 

<?php
if(isset($_COOKIE['lang'])){ //check if the cookie is set
     $lang = $_COOKIE['lang'];
     if($lang == 'en' or $lang == 'fr'){ //check if the cookie has a correct value
          setcookie('lang', $lang, time()+60*60*24); //set the cookie to expire after 24h
          include('lang/' . $lang . '.php'); //include the language file, ie: lang/en.php
     }
}
?>

 

The language may be changed like this:

<a href="index.php?l=en">English</a>
<a href="index.php?l=fr">French</a>

 

And use the $_GET superglobal to change the included file. Remember to set also the cookie when the get variable is set.

I'm sure you know how to create a cookie with setcookie(). Based on that cookie you include the language file and use get variables to change it. The basic idea:

 

As I'm really new to all this... I do not really know how to create such a cookie that remembers the user's choice of language...

 

What is important for me that all text strings are externalized in one php file per language. And that the user can change the language from a simple menu. By clicking on a language the php file is loaded and the site appears in the chosen language. how can I achieve this and what code do I need to add to my we pages to make this work?

 

thanks

It is exactly what the code i suggested does. It checks if a cookie is set and if yes, it read the language files. You should add also the $_GET part, where the language is change, something like this:

 

<?php
if(isset($_GET['lang'])){ //see if the user has changed the language (by clicking the language link)
     $lang = $_GET['lang']; //set the language variable
     setcookie('lang', $lang, time()+60*60*24); //set the cookie
} elseif(isset($_COOKIE['lang'])){ //check if the user has the language cookie
     $lang = $_COOKIE['lang']; //set the language variable
} else{ //see if the cookie isn't set, neither the user has changed the language
     $lang = 'en'; //set the default language
     setcookie('lang', 'en', time()+60*60*24); //set the default cookie
}
include("lang/$lang.php"); //include the language file
?>

Hi again

 

Well I experimented a while with the code you gave me and it works ;-)

 

I now have this :

 

<?php
if(isset($_GET['lang'])){ //see if the user has changed the language (by clicking the language link)
     $lang = $_GET['lang']; //set the language variable
     setcookie('lang', $lang, time()+60*60*24); //set the cookie
} elseif(isset($_COOKIE['lang'])){ //check if the user has the language cookie
     $lang = $_COOKIE['lang']; //set the language variable
} else{ //see if the cookie isn't set, neither the user has changed the language
     $lang = 'en'; //set the default language
     setcookie('lang', 'en', time()+60*60*24); //set the default cookie
}
include("lang/$lang.php"); //include the language file
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
</head>

<body>

<?php include("lang/languages.php"); ?>

<h1><?php echo $TEXT['welcometitle']; ?></h1>
<p><?php echo $TEXT['welcome1']; ?></p>

</body>
</html>

 

As you can see I made an include of a file called languages.php. I would like this file to contain the language menu as you showed me:

 

<a href="index.php?lang=en">English</a>
<a href="index.php?lang=fr">Français</a>
<a href="index.php?lang=de">Deutsch</a>

 

My problem is now, that the links in this menu all link back to index.php. But I would like this links to work on whatever site, so that when the user is on index.php he can change from french to german... and when he is on a page aboutus.php, he only changes the language of THAT page. So without returning to index.php...

 

How can I achieve this?

 

Thanks a lot :-)

You should make the links that point to the actual page with some simple PHP:

 

<?php
$page = basename($_SERVER['PHP_SELF']); //this will get the actual page
echo '<a href="' . $page . '?lang=en">English</a>';
echo '<a href="' . $page . '?lang=fr">French</a>';
?>

Hi

In fact i changed the code to this :

 

<ul>
    <li><a href="?lang=en">English</a> /</li>
    <li><a href="?lang=fr">Français</a> /</li>
    <li><a href="?lang=de">Deutsch</a> /</li>
</ul>

 

It seems to work... or am I gonna get problems doing it this way?

 

 

Now I have a second BIG question concerning the mulitilingual aspect of the site:

 

I would like the user to come onto my site through an intro page where he/she can choose it's preferred language... after choosing the language, the site would go on to the actual web page (the language now being stored inside the cookie)... BUT IF the user has already visited my site and has the cookie, he would actually not get to the intro page but directly to the site.

 

I've seen this work inside the XAMPP interface but I can't reproduce the code... there is the actual index.php and site called splash.php (intro page with language choice), once a language is stored in the cookie, the index page opens. If there is no cookie or if the cookie is empty, then the user first goes through the splash (intro) screen...

 

Thank you so much... You've been an excellent help so far... my website is really starting to work and I LOVE PHP! :-)

you could do this:

 

- Create a cookie of the users selected language.

- Load the cookie and read in the language.

- Select the proper language file.

 

Above Explained:

place all of you codes in different files:

 

/lang/en.php

define(HELLO, "Hello");

 

/lang/es.php

define(HELLO, "Hola");

 

and so on...

 

next in your "index.php", load the cookie, then include the proper file. Example:

 

if (isset($_COOKIE['lang'])) {
     switch($_COOKIE['lang']){
          case "en":include 'lang/en.php';break;
          case "es":include 'lang/es.php';break;
     }
}else{
     include 'lang/en.php';
}

echo HELLO;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.