egturnkey Posted September 28, 2009 Share Posted September 28, 2009 Hello Friends, i need an simple example explain to me how this could be. - a database table has 2 lang ( english - french ) where english is the dafult - an php file that show the text stored in database with select form to switch btween explain more ------------ 1- database where i can store the english language and french language. 2- config.php // connect to the database. 3- index.php file that shown the varibles language in english which select form where i can switch off btween english/french. Problem may i face -------------------- if the index.php has a link for example ( contacts.php ) if i clicked on it will the language keeps as chosen in index.php !! can anyone help me even with a very very primtive example cause i really needs to understand it and example is the only way that helps me to understood the whole idea. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/175845-i-need-an-simple-example-of-multi-languages/ Share on other sites More sharing options...
TeNDoLLA Posted September 28, 2009 Share Posted September 28, 2009 Not sure if this is waht you meant but lets give it a try. I would probably have two tables for this. languages --------- lang_id INT UNSIGNED NOT NULL AUTO_INCREMENT language VARCHAR(255) NOT NULL translations -------- id lang_id (refers to languages table lang_id) word (some sort of constant that is same for every language) translation (and the translation is the one that changes) <?php // This is the variable from the form when the language was chosen. if (isset($_POST['language_id']) && intval($_POST['lang_id']) > 0) { $languageId = intval($_POST['language_id']) $output = array(); // Array used to store the translations and used to output them. // Get the language translations for chosen language from db. $sql = "SELECT t.word, t.translation FROM languages l JOIN translations t ON t.lang_id = l.lang_id WHERE l.language_id = $languageId" $result = mysql_query($sql); $translations = mysql_fetch_object($result); // Organize array for output. foreach ($translations as $entry) { $output[$entry->word] = $entry->translation; } } Now you should have all translations in array for the chosen language, you could store those for example to session variable to keep them there in every page when the users navigate through your site. And say you had in database a word (constant) called 'yes' then you would have the translation for word yes in array cell $output['yes'] (or use session variable instead of $output to keep it active on everypage). Then using the variables in your page you would use the "constant" called word to print the variables in places. This is some text and here is some word in chosen language <?php echo $output['some_word']; ?>. Some more text... Maybe even a title <h1><?php echo $output['title']; ?></h1> Ps. I wrote this example straight from head and did not test it but it was meant to give an example of one way that you can achieve multilangual pages. It might not be the best way. I have used this sometimes though. Anyone with better examples can post them here. Quote Link to comment https://forums.phpfreaks.com/topic/175845-i-need-an-simple-example-of-multi-languages/#findComment-926588 Share on other sites More sharing options...
Psycho Posted September 28, 2009 Share Posted September 28, 2009 What you are asking is about several different pieces of functionality, all of which would require different implementations based upon your needs. There are many ways to implement multiple languages and it is up to you to determine the best approach to what you need to accomplish. Here are the things I would consider when implementing such a functionality. How to determine what language to show the user. You can use session data, cookie data or a combination of both. You can also leverage the user's system to try and dynamically predetermine what language might be the right one for them. Here is a small script I have used to determine the user's language and set a variable to be used later in the script. It works by first checking if the user has selected a new language. If not then checking to see if they have a saved language in session data. If not then checking to see if they have a saved value in cookie data. If not, then go with the default. <?php //Create array of available languages // NOTE: Could programatically create this list based upon available language files or DB //Use ISO two letter abbreviations. $languageListAry = array ('en', 'sp', 'fr'); //Set default to first language in list $language = $languageListAry[0]; //Check if request was made to change the language via query string if (isset($_GET['language']) && in_array($_GET['language'], $languageListAry)) { $language = $_GET['language']; setcookie('language', $language, time()+3600*24*365); //Expires in 1 year $_SESSION['language'] = $language; //Check if there is a saved language value in the COOKIE } else if (isset($_COOKIE['language']) && in_array($_COOKIE['language'], $languageListAry)) { $language = $_COOKIE['language']; $_SESSION['language'] = $language; //Check if there is a saved language value in the SESSION (In case cookies are disabled) } else if (isset($_SESSION['language']) && in_array($_SESSION['language'], $languageListAry)) { $language = $_SESSION['language']; } ?> You will have to determine how you will change the content based upon the selected language and how you will store/retrieve that data. One way I have done this is by using placeholders in my content (e.g. "[WELCOME_MESSAGE]") and then build my pages into a variable instead of directly echo'ing the content. Once the entire page is built, I will replace all the placeholders with the appropriate text based upon the language. There are also ways to do this with output buffering. Depending upon how much content there is I might also categorize the replacement text according to the pages. Many times the content on a particular page will be somewhat dynamic (e.g. error messages), but I will have all the possible text for each page categorized. This is especially helpful when the text is in a database as you can have columns for the page, the placeholder name, and the actual content. You would just need to pull the content for the current page instead of all the text. The other option is to have different language files for each page with the appropriate references to each placeholder. Then just pull the appropriate language file and make the replacements. Quote Link to comment https://forums.phpfreaks.com/topic/175845-i-need-an-simple-example-of-multi-languages/#findComment-926604 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.