Jump to content

multilingual website


beta3designs

Recommended Posts

Hello there!

well this is not really a problem but before I continue I would like to know wether this script is good or I should code it another way...

Well the thing is I need to do a multilingual site so I thought about having some flags in the corner that have a link like this: index.php?lang=en to determine the language and maybe store the language into a session then depending on the language stored require a file like en.php (English) or de.php (German), etc. and each one contains different arrays with different things like these: $content['welcome'], $content['copyright'], etc. or $header['title'], $menu['about']...

 

Well I hope you get the idea... So what i wrote is this:

 

index.php:

<?php
session_start();

if(empty($_SESSION['language'])){
$_SESSION['language'] = 'en';
}else{
if(isset($_GET['lang'])){
	$_SESSION['language'] = $_GET['lang'];
}
}

if($_SESSION['language'] == 'en'){
require('en.php');
}elseif($_SESSION['language'] == 'es'){
require('es.php');
}elseif($_SESSION['language'] == 'de'){
require('de.php');
}
?>

<html>
<head>
<title>Test</title>
</head>
<body>
<a href="index.php?lang=en">English</a>
<a href="index.php?lang=es">Español</a>
<a href="index.php?lang=de">Deutsch</a>
<p><?php print $content['welcome']?></p>
</body>
</html>

 

en.php:

<?php
$content = array(
"welcome" => 'Hello, welcome!'
);
?>

 

es.php:

<?php
$content = array(
"welcome" => 'Hola, bienvenido!'
);
?>

 

de.php:

<?php
$content = array(
"welcome" => 'Hallo, willkommen!'
);
?>

 

But I haven't handled sessions that much and I don't know if this is a good way to do this?

 

Thanks in advance!!

 

P.S: That code is just an example it is not finished so that's why there's some xhtml missing...

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

  • 2 weeks later...

if(empty($_SESSION['language'])){
   $_SESSION['language'] = 'en';
}else{
   if(isset($_GET['lang'])){
      $_SESSION['language'] = $_GET['lang'];
   }
}

 

I'd write that a little different

 

<?php
if(isset($_GET['lang']))
$_SESSION['language'] = $_GET['lang'];
if(empty($_SESSION['language']))
$_SESSION['language'] = 'en';

 

You want the GET request to take presidence over the assigning of the language. If it isn't set, then assign the default language.

Link to comment
https://forums.phpfreaks.com/topic/166775-multilingual-website/#findComment-888297
Share on other sites

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.