4554551n Posted January 18, 2010 Share Posted January 18, 2010 Hi all, How do I create a PHP code such that if a user clicks on a button they get transferred to the equivalent page in another language, I have index.php <?php include_once 'file.php'; ?> <html><div class="menu"> <a href="index.php?lang=en" > <img title="Eng" src="images/en.png" alt="" /> </a> <a href="index.php?lang=de"> <img title="Ger" src="images/de.png" alt="" /> </a> <a href="index.php?lang=hu" > <img title="Hun" src="images/hu.png" alt="" /> </a> </div> </html> and I have another file.php <?php ob_start(); session_start(); header('Cache-control: private'); if(isSet($HTTP_GET_VARS['lang'])) { $lang = $HTTP_GET_VARS['lang']; //* register the session and set the cookie *// $HTTP_SESSION_VARS['lang'] = $lang; setcookie("lang", $lang, time() + (3600 * 24 * 30)); } else if(isSet($HTTP_SESSION_VARS['lang'])) { $lang = $HTTP_SESSION_VARS['lang']; } else if(isSet($HTTP_COOKIE_VARS['lang'])) { $lang = $HTTP_COOKIE_VARS['lang']; } else { $lang = 'en'; } switch ($lang) { case 'en': $lang_file = 'lang.en.php'; break; case 'de': $lang_file = 'lang.de.php'; break; case 'hu': $lang_file = 'lang.hu.php'; break; default: $lang_file = 'lang.en.php'; include_once './languages/'.$lang_file; } ?> and it doesn't work, basically all I want is php script, so when user browse through page can change it in another language... Here is another way, but I found also problem here.... I have index.php <?php if($HTTP_GET_VARS['lang'] == 'de' || $HTTP_GET_VARS['lang'] == "") { include 'lang_de.php';} if($HTTP_GET_VARS['lang'] == 'de') { include 'lang_de.php';} if($HTTP_GET_VARS['lang'] == 'en') { include 'lang_en.php';} if($HTTP_GET_VARS['lang'] == 'hu') { include 'lang_hu.php';} global $lang1, $lang2, $lang3, $lang4, $txt4; ?> <html>Some content here</html> Problem is when user change language somewhere deep in the page, script automatically returns him to the home of page, so it's useless, cause u can't browse page on another language except default language. All I want is one nice script for browsing through page on whatever language user want. What's wrong with this? I mean in first case script doesn't work and I don't why.... and in the second case script works, but not properly... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/ Share on other sites More sharing options...
al Posted January 18, 2010 Share Posted January 18, 2010 Try like on this page: http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997329 Share on other sites More sharing options...
4554551n Posted January 18, 2010 Author Share Posted January 18, 2010 I saw that link earlier, however It's easier to use <?php echo $var ?> than "define". I'm not satisfied with that, is there any other way to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997339 Share on other sites More sharing options...
al Posted January 18, 2010 Share Posted January 18, 2010 Create a languages folder /lang/en.php /lang/fr.php In each of the files: $lang[1] ="what ever text you need"; $lang[2] ="what ever text you need"; $lang[3] ="what ever text you need"; Then use a switch in the page headers to include the file that you want yourfile.php?lang=en $lang=['lang']; switch($lang){ case'en': include 'lang/en.php'; break; case'fr': include 'lang/fr.php'; break; } Then just go ahead and use the text in where you need it with: echo $lang[3]; Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997362 Share on other sites More sharing options...
laffin Posted January 18, 2010 Share Posted January 18, 2010 use sessions. this way, no matter which page they goto, u will have which language. I've seen a number of ways of doing this. 1) create a subdirectories for each language - prolly the easiest, but also means a lot of maintainance. 2) create a lang folder / with lang php files - intermediate, but maintainance is a lot easier, as you just need to edit the language files. there are other ways. but those are the most common. Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997381 Share on other sites More sharing options...
4554551n Posted January 18, 2010 Author Share Posted January 18, 2010 use sessions. this way, no matter which page they goto, u will have which language. I've seen a number of ways of doing this. 1) create a subdirectories for each language - prolly the easiest, but also means a lot of maintainance. 2) create a lang folder / with lang php files - intermediate, but maintainance is a lot easier, as you just need to edit the language files. there are other ways. but those are the most common. I'm trying to use, but so far nothing it would be better if u can show me some example. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997445 Share on other sites More sharing options...
4554551n Posted January 18, 2010 Author Share Posted January 18, 2010 Then use a switch in the page headers to include the file that you want yourfile.php?lang=en $lang=['lang']; Here I have unexpected error cause of [ ] what should I change? Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997479 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 Then use a switch in the page headers to include the file that you want yourfile.php?lang=en $lang=['lang']; Here I have unexpected error cause of [ ] what should I change? I believe he means $lang['en'] ="what ever text you need"; $lang['fr'] ="what ever text you need"; $lang['de'] ="what ever text you need";[ $lang = $lang['de']; //Assign to language 'de' switch($lang){ case'en': include 'lang/en.php'; break; case'fr': include 'lang/fr.php'; break; } Also, $HTTP_GET_VARS is a registered global, which has been deprecated for nine years. Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997491 Share on other sites More sharing options...
laffin Posted January 18, 2010 Share Posted January 18, 2010 go one step further oni-kun lang.en.php $langstrings =array( 'hello'='>'Hello', 'bye'=>'Good Bye', ); lang.sp.php $langstrings = array( 'hello'=>'Hola', 'bye'=>'Adios' ); lang.fr.php $langstrings = array( 'hello'=>'bonjour', 'bye'=>'au revoir' ); so main code can go something like this $lang='xx'; // give some goofy language $lang = file_exists("lang.{$lang}.php"?"lang.{$lang}.php":"lang.en.php"; // select out language file, if not exist use default en. include($lang); // load our language file echo "{$langstring['hello']}"; a simple method, with an intermediate level of maintainance. Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997543 Share on other sites More sharing options...
4554551n Posted January 18, 2010 Author Share Posted January 18, 2010 I don't know what is the problem, I put this into index.php <?php $lang= $lang[en]; switch($lang){ case'en': include 'lang/en.php'; break; case'de': include 'lang/de.php'; break; <html> some content </html> } ?> and I have <?php echo $lang[1];?> on proper place in index.php I have this in en.php <?php $lang[1] ="HOME"; $lang[2] ="ABOUT US"; $lang[3] ="CONTACT"; $lang[4] ="FAQ"; $lang[5] = "Here goes txt,Here goes txt,Here goes txt,Here goes txt, Here goes txt,Here goes txt,Here goes txt,Here goes txt, Here goes txt, Here goes txt,Here goes txt,Here goes txt, Here goes txt, Here goes txt,Here goes txt,Here goes txt"; ?> and there is no output, and also I don't have text anywhere... I forgot to say that I'm using Nusphere Phped 5.9, if that means anything.... Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997552 Share on other sites More sharing options...
4554551n Posted January 19, 2010 Author Share Posted January 19, 2010 <?php if($HTTP_GET_VARS['lang'] == 'de' || $HTTP_GET_VARS['lang'] == "") { include 'lang_de.php';} if($HTTP_GET_VARS['lang'] == 'de') { include 'lang_de.php';} if($HTTP_GET_VARS['lang'] == 'en') { include 'lang_en.php';} if($HTTP_GET_VARS['lang'] == 'hu') { include 'lang_hu.php';} global $lang1, $lang2, $lang3, $lang4, $txt4; ?> This works well, just as there is a problem when users surf the page, for example, and be part of the FAQ page and wants to change the language,this script automatically return to the home, and since the default language is de, it is not possible to surf in another language... How to change this? Quote Link to comment https://forums.phpfreaks.com/topic/188892-multilanguage-site-problem/#findComment-997965 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.