dennisdiving Posted June 13, 2011 Share Posted June 13, 2011 My website is multilanguage, you can change your langluage yourself. In the head of the site ( English Spanish Portuguese Italian) I like to change this into the flag of the country. Does anyone know how to change the name into the en.png thanks Dennis HTM page <?php language_navigation(); ?> en.php $lang["available_language"] = array( "en" => "English", "es" => "Spanish", "pt" => "Portuguese", "it" => "Italian", ); config.php // The default language $default_lang = "en"; // The relative path to the lang folder $lang_folder = "lang"; //Get the language used by the browser $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $language = ""; // Check if the language file exists function use_lang($language) { global $lang_folder; if( is_file($lang_folder."/".$language.".php") ) { return true; } } if( isset($_GET['lang']) && use_lang($_GET['lang']) ) { $language = $_GET['lang']; } else if ( isset($browser_lang) && use_lang($browser_lang) ) { $language = $browser_lang; } else { $language = $default_lang; } // Include the right language file include($lang_folder."/".$language.".php"); // Helper function to echo the values of the $lang array function lang($key) { global $lang; echo $lang[$key]; } function language_navigation() { global $lang; $languages = $lang["available_language"]; foreach( $languages as $single_language ) { echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?lang=' . key($languages) . '">'. $single_language . '</a></li>'; next($languages); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/ Share on other sites More sharing options...
joel24 Posted June 14, 2011 Share Posted June 14, 2011 the line if( isset($_GET['lang']) && use_lang($_GET['lang']) ) checks to see if any language variables are set in the URL You need to pass the $_GET variables when a user clicks on the appropriate language flag $lang["available_language"] = array( "en" => "English", "es" => "Spanish", "pt" => "Portuguese", "it" => "Italian", ); //you'll have to set the src part to point to the folder where the flags are //you'll also have to change index.php to the page name, or look into using a $_SERVER variable to echo the page foreach ($lang AS $key=>$value) { echo "<a href='index.php?lang=$key'><img src='$key.png' alt='$value' /></a>"; } //foreach will return <a href='index.php?lang=en'><img src='en.png' alt='english' /></a> for the first Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1229424 Share on other sites More sharing options...
dennisdiving Posted June 14, 2011 Author Share Posted June 14, 2011 Maybe i am running in a circle: and i am little confused. I try so hard but i fail (stupid me). For this it is working, the flags are ok. (thanks) but i can not change the language for the site. It keeps looking at my explorer language and changing it back. Why can't the language stay whatever i want the language to be? global $lang; $languages = $lang["available_language"]; foreach( $languages as $available_language=>$key ) { echo "<a href='index.php?lang=$available_language'><img src='$key' alt='$image' /> "; next($languages); } } Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1229771 Share on other sites More sharing options...
joel24 Posted June 15, 2011 Share Posted June 15, 2011 you'll need to set the user's language choice into the session or a cookie have a look here to read up on sessions Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1229811 Share on other sites More sharing options...
dennisdiving Posted June 16, 2011 Author Share Posted June 16, 2011 Hello Joel, I have read all info about GET use $ But it dazzels me (maybe this is too much for a newbie) i need to read more. The problem was(is) clients in France are on my website and have the site in France (ok). But they are English poeple... they can not change the site completley in English only one page at the time. (for the imanges this is only a nicer look) Maybe if i figured it all out i will give a nice reply, for the whole multilanguage PHP script. Thank for now dennis Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1230261 Share on other sites More sharing options...
joel24 Posted June 16, 2011 Share Posted June 16, 2011 use a session to store the language for the duration of the user's visit to your site and then check if the session is set after you check the get variables (in case they want to change languages again) i.e. session_start(); if (isset($_GET['lang'])) { //add to session $_SESSION['lang'] = $_GET['lang']; } else if (isset($_SESSION['lang'])) { //language is set in session - do nothing. } else { //default language $_SESSION['lang']='en' } // Include the right language file include("lang/{$_SESSION['lang']}.php"); *EDIT* you need to ensure session_start(); is located at the top of each PHP script which you want to include the session variables. read up on sessions at the link I put above. Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1230396 Share on other sites More sharing options...
dennisdiving Posted June 18, 2011 Author Share Posted June 18, 2011 Hello Joel, All is working.... but i tested the use_lang($browser_lang) i can't get it right. Can you help me with the last point, oh by the way, i removed the $_SERVER['PHP_SELF'] With PHP 5 this is not working..... see what i did. top page of index.php <?php session_start(); include("config.php"); ?> <?php // The default language $default_lang = "en"; // The relative path to the lang folder $lang_folder = "lang"; //Get the language used by the browser $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $language = ""; // Check if the language file exists function use_lang($language) { global $lang_folder; if( is_file($lang_folder."/".$language.".php") ) { return true; } } if( isset($_GET['lang']) OR use_lang($_GET['lang']) ) { $language = $_GET['lang']; } else if ( isset($browser_lang) OR use_lang($browser_lang) ) { $language = $browser_lang; } else { $language = $default_lang; } // Include the right language file include($lang_folder."/".$language.".php"); // Helper function to echo the values of the $lang array function lang($key) { global $lang; echo $lang[$key]; } function language_navigation() { global $lang; $languages = $lang["available_language"]; foreach( $languages as $available_language => $key ) { echo "<a href=" . '?lang=' . $available_language . " ><img src='$key' /> "; next($languages); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1231456 Share on other sites More sharing options...
dennisdiving Posted June 18, 2011 Author Share Posted June 18, 2011 Hello Joel, All is working.... but i tested the use_lang($browser_lang) i can't get it right. Can you help me with the last point, oh by the way, i removed the $_SERVER['PHP_SELF'] With PHP 5 this is not working..... see what i did. top page of index.php <?php session_start(); include("config.php"); ?> <?php // The default language $default_lang = "en"; // The relative path to the lang folder $lang_folder = "lang"; //Get the language used by the browser $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $language = ""; // Check if the language file exists function use_lang($language) { global $lang_folder; if( is_file($lang_folder."/".$language.".php") ) { return true; } } if (isset($_GET['lang'])) { //add to session $_SESSION['lang'] = $_GET['lang']; } else if (isset($_SESSION['lang'])) { //language is set in session - do nothing. } else { //default language $_SESSION['lang']='en'; } // Include the right language file include("lang/{$_SESSION['lang']}.php"); // Helper function to echo the values of the $lang array function lang($key) { global $lang; echo $lang[$key]; } function language_navigation() { global $lang; $languages = $lang["available_language"]; foreach( $languages as $available_language => $key ) { echo "<a href=" . '?lang=' . $available_language . "><img src='$key'/></a> "; next($languages); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1231591 Share on other sites More sharing options...
joel24 Posted June 19, 2011 Share Posted June 19, 2011 your use_lang() function only determines whether the appropriate language file exists, i.e. en.php, fr.php etc what do you want it to do? Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1231609 Share on other sites More sharing options...
dennisdiving Posted June 19, 2011 Author Share Posted June 19, 2011 I want the user to have his own language to start (by checking his browser language) And second to make a choice to change the language to another language. (not only the page is is watching, but the whole site )(we fixed this it is ok now) ( for instance a Englishman behind a internetcafe-pc in France.. this is a france pc ) but he want the site in English. Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1231692 Share on other sites More sharing options...
joel24 Posted June 19, 2011 Share Posted June 19, 2011 $_SERVER["HTTP_ACCEPT_LANGUAGE"] will have the browser language $_SERVER variables on php.net Quote Link to comment https://forums.phpfreaks.com/topic/239296-select-language-in-an-array-add-image/#findComment-1231705 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.