beta3designs Posted July 21, 2009 Share Posted July 21, 2009 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... Quote Link to comment Share on other sites More sharing options...
waynew Posted July 30, 2009 Share Posted July 30, 2009 Hi, seems pretty ok to me. Simple but effective. I would however question whether you could put the languages into one file instead of many? Quote Link to comment Share on other sites More sharing options...
beta3designs Posted July 31, 2009 Author Share Posted July 31, 2009 Yes, I also thought about using a multidimensional array perhaps? But yes I think everything in a file would be better. Well thanks anyway, I appreciate your help Quote Link to comment Share on other sites More sharing options...
OOP Posted August 1, 2009 Share Posted August 1, 2009 Hi there, Your code looks fine. I don't think having multiple files for the language would be a problem. In fact, I prefere to have each language in a separate file. Regards Quote Link to comment Share on other sites More sharing options...
gevans Posted August 1, 2009 Share Posted August 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
beta3designs Posted August 3, 2009 Author Share Posted August 3, 2009 Alright thanks... really appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.