Help - Search - Members - Calendar
Full Version: Language Switching
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
MrTouz
Ive been searching around the web for hours now without any luck.
I am looking for some language script, i have my website and i need to be able to have 2 languages on my site where users from a link or an image can select the language they want.
I have 2 folders on my site /fr /en FR is for French and EN is for english

Inside these 2 folders the file names are the same the only thing that changes is the content inside and their path.

What i want is a script working with this one :

CODE
<?php
if(!isset($_GET['id'])) {
    include "home.php";
}elseif(!file_exists($_GET['id'].".php")) {
    include "error.php";
}else{ //if all goes well
    include $_GET['id'].".php";
};?>


So for example now the link for my site is :

index.php?id=home

what i want is by default this home page is from the /en folder, but if the user wants by clicking the link it changes this /en/home.php to the file at /fr/home.php

so something like index.php?id=home&lan=en where id selects the page and lan selects the path ?

i looked around and tried to modify the code above but i just can't get a hold of something, just donno where to start :/ Plus the Google doesnt help at all keywords wont work well : language script using php, php language switcher, multi language websites using php, php multilingual script.... list goes on and on... can't get a hold of something...
The only thing i found was the switch() but can't make it work.

Big Help is needed here :/
Thanks a lot !
Alex
Well, what you can do to fit it with the current directory structure would be to do something like this (index.php below en/ and fr/):

CODE
<?php
$languages = array('en','fr');

if(!in_array($_GET['lang'],$languages))
   include_once 'unsupported_language.php';
else
   switch($_GET['mode'])
   {
      case 'page':
         include_once $_GET['lang'].'/page.php';
         break;
      default:
         include_once $_GET['lang'].'/home.php';
   }
?>


Adding an extra case for each page. (you can make it dynamic, but I prefer to specify everything to make damn sure there's no way they can include something they aren't meant to see)
MrTouz
i kind of not understand how am i suppose to use your code.

Can you please give me an example of a link ? lets say to set my site in 'en' is it something like :

unsupported_languages.php?lang=en

that that will give me the page in EN ?

really lost... php skills are very very low !
_________

Now i got it !!!!! ok ok ok very smart move and easily done.

Now question, is it possible to add cookies or sessions to this so it never forgets what a user was in with ?

_____

Well little problem (damn i should wait befor i post !!)

how do i select a page ?

because now, i inserted your code inside lol.php so i do : lol.php?lang=fr gives me the home.php from the fr path and when i put EN it puts me the EN path, but how do i change the page ?

i tried combining the code i had while doing :

lol.php?id=home&lang=fr

but doesnt work :/

oh i am so lost !
Mark
The easiest way is to make a en.php and fr.php file containing all your text in descriptive named variables. And then at the top you set which language file to import. e.g.
CODE
switch ($_GET['id'])
{
    case 'en':
        require ( "../languages/en.php" );
        break;
    case 'fr':
        require ( "../languages/fr.php" );
        break;
}

And then in the langauge files you'd have it like
$aLanguage['home-page-header'] = "Welcome to mysite!";
$aLanguage['home-page-explanation'] = "Welcome to mysite! Here we do this and this and this. We have lots of fun and do lots of things. zOMG."

And then in your php where you fill in the HTML you just use the the $aLanguage variable, and whichever language is set will determine which $aLanguage variable it uses (english or french)
IamShipon1988
Here try this website. It has a decent tutorial on multi-language sites. http://www.phpsimplicity.com/tips.php?id=15 also here is another tutorial http://www.askbee.net/articles/php/MultiLa...ic_website.html
Bogey
QUOTE(Mark @ Jun 22 2008, 05:34 PM) *
The easiest way is to make a en.php and fr.php file containing all your text in descriptive named variables. And then at the top you set which language file to import. e.g.
CODE
switch ($_GET['id'])
{
    case 'en':
        require ( "../languages/en.php" );
        break;
    case 'fr':
        require ( "../languages/fr.php" );
        break;
}

And then in the langauge files you'd have it like
$aLanguage['home-page-header'] = "Welcome to mysite!";
$aLanguage['home-page-explanation'] = "Welcome to mysite! Here we do this and this and this. We have lots of fun and do lots of things. zOMG."

And then in your php where you fill in the HTML you just use the the $aLanguage variable, and whichever language is set will determine which $aLanguage variable it uses (english or french)

This is the best solution. Here is a tutorial for a non-programmer smile.gif (WARNING: PHP Codes included)

Alright, as always, lets do it with a demo and not try to do this for universal projects. Demos work better and once you learn you can go universal (Apply it for your site in simple terms laugh.gif )

Alright, first of all create a page index.php in a testing directory you will delete later on when you are done with this tutorial.

In index.php we will have it check which language file we will need, and to make it simple, lets use a technique that is used in a templating system. Me would have like three files in the first directory... just to make it work better.

The files in the directory would be...
  • index.php
  • includes.php
  • language.php
  • language/en.php
  • language/fr.php
Index.php is the main page for public view... includes.php would be what the name infers... it would include files so you could include one file into one file that is included into all files. This way, if you create one file that needs to be included into all files and there is a file that is already included into all files, than you can include that file into that file and it is included into every file automatically... saves time smile.gif Also good if you want to set universal variables biggrin.gif I do this and it serves me well.

Obviously language/en.php and language/fr.php are the language files.

language.php would determine which language to use for rendering. If no language is set, it would default to english. Here is how index.php would look like in the example (The content would grow in the example).

CODE
<?php
session_start();
include('includes.php');
?>

And here is how includes.php would look like
CODE
<?php
include('language.php');
?>

And here is how language.php would look like
CODE
<?php
$language = array('fr','en');
$lang = ((isset($_SESSION['lang'])) ? $_SESSION['lang'] : $_GET['lang']);
$lang = ((!in_array($lang) && !empty($lang)) ? $_GET['lang'] : 'en');

if(!isset($_SESSION['lang']) && isset($_GET['lang']))
{
  $_SESSION['lang'] = $lang;
}

include("language/{$lang}.php");
?>

en.php would look like...
CODE
<?php
$lang['helloworld'] = "Hello World!";
$lang['maincontent'] = "Welcome to my site. I appreciate the fact that you came here.";
$lang['lang'] = "English";
$lang['title'] = "Language Test";
?>

and the fr.php would look like
CODE
<?php
$lang['helloworld'] = "Bonjour tout le monde!";
$lang['maincontent'] = "Bienvenue sur mon site. J'apprécie le fait que vous êtes venu ici.";
$lang['lang'] = "Français";
$lang['title'] = "Langue d'essai";
?>

(Sorry for the poor translations... I don't know French so I used google translator laugh.gif )

So, now your index.php would look like...
CODE
<?php
session_start();
include('includes.php');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title><?php echo $lang['title']; ?></title>
</head>
<body>
<p style="color: #CECECE;">Language <?php echo "<a href=\"".$_SERVER['REUQEST_URI']."?lang=en\">English</a> <a href=\"".$_SERVER['REUQEST_URI']."?lang=fr\">Français</a>\n"; ?></p>
<hr />
<h1 style="text-align: center;"><?php echo $lang['helloworld']; ?></h1>
<p><?php echo $lang['maincontent']; ?></p>
</body>
</html>

That is all to it. Real easy once you create the language files and the link and that is all...

If you need anything else ask here...
MrTouz
thank you all guys for your comments.
And thanks a lot for the tut.
Bogey
No problem... there is an error in the index.php in the tut...

<?php
session_start();
include('includes.php');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

should be

<?php
session_start();
include('includes.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Gawdl3y
As for your question about saving to cookies...

At the beginning of your index.php or whatever, but after session_start():
CODE
$CookieName = 'Mr-Touz-Language-Cookie';
if(!isset($_COOKIE[$CookieName]))
  $Lang = 'en';
if(isset($_GET['lang'])) {
  $Lang = $_GET['lang'];
  setcookie($CookieName, $Lang, time() + 3153600);
  header("Location:  ". $_SERVER['PHP_SELF']); // Just gets rid of the ?lang= at the end of the URL
}


And change the include thing to:
CODE
include('language/'. $Lang .'.php');



And I recommend adding
CODE
ob_start("gz_handler");

after your <?php tag, but only in your index.php, or files that aren't ever included. It will compress the output of your stuff so that a 10kb page might end up being 1kb in a browser after being compressed. Also, it allows you to send headers even after outputting something, because it starts the Output Buffer.
MrTouz
Since i couldn't do it (abviously) i had a friend of mine to do it smile.gif he is actually pretty good with PHP, but he also removed some of my old codings and replaced all, i have several codes in ONE now, going as well withthe language script.

Thanks again for the help smile.gif (feels good not be alone)
Bogey
Congratulations on your site smile.gif
MrTouz
thanks biggrin.gif
Sickness
I'm actually working on a website that is under french and english here is the link


This is working with a cookie named language with javascript that find the language of the browser and redirect the person to a php script that set a cookie

and when the cookie is set that include the good language file

the code look like this
CODE
if(!isset($_COOKIE['language'])){
//the javascript script
header("location: language/index.php");
}
else{
include("language/".$_COOKIE['language']."/index.php");
}


If you want my help just pm me wink.gif
Hughjackman
Hi ,
I often use resources from this website :- LangLearner All the resources on there are especially for children and teenagers. You can visit and learn online Languages. I'm learning Languages with this tool, and it's very helpful.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.