xcoderx Posted July 11, 2009 Share Posted July 11, 2009 hi i want to add different languages to my site can someone tell me how to do it? i seen it some place long back they do it something like $lang['username'] = 'Username'; $lang['password'] = 'Password'; $lang['login'] = 'login'; $lang['register'] = 'Register'; $lang['features'] = 'Features'; $lang['terms'] = 'Terms And Conditions'; $lang['home'] = 'Home'; $lang['notmember'] = 'Not Member Yet?'; $lang['birthday'] = 'Birthday'; can someone tell me how to get it done? it should work when a user is not logged in and once logged in Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/ Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 $_SERVER['HTTP_ACCEPT_LANGUAGE'] Defines the languages the user "understands" and is indicated by a quality ranging from 0 to 1 (inclusive) where 1=Very Good and 0=Very Poor. For more information on http accept headers: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-873590 Share on other sites More sharing options...
xcoderx Posted July 11, 2009 Author Share Posted July 11, 2009 Thanx bro but is there any easy xplanation on how to add multiple language to website? Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-873782 Share on other sites More sharing options...
MadTechie Posted July 12, 2009 Share Posted July 12, 2009 Okay Just say on my site I have this <?php $LangCode = (!empty($_GET['lang']))?$_GET['lang']:"en"; //include language file $LangFile = "lang.".$LangCode.".php"; //ie lang.en.php if(file_exists($LangFile)) include_once $LangFile; //echo some stuff echo $lang['Welcome']; ?> Now for English (EN) i have this file a language file <?php $lang = array(); $lang['Welcome'] = "Welcome to my site"; $lang['username'] = 'Username'; ?> for spanish (ES) i have this file <?php $lang = array(); $lang['Welcome'] = "Bienvenido a mi sitio web"; $lang['username'] = 'Nombre de usuario'; ?> Now when I visit www.mysite.com?lang=es I get Bienvenido a mi sitio web but with out anything or with lang=en I get Welcome to my site Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-873804 Share on other sites More sharing options...
xcoderx Posted July 12, 2009 Author Share Posted July 12, 2009 Cheers awesome guidance thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-873910 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 <?php $LangCode = (!empty($_GET['lang']))?$_GET['lang']:"en"; //include language file $LangFile = "lang.".$LangCode.".php"; //ie lang.en.php if(file_exists($LangFile)) include_once $LangFile; //echo some stuff echo $lang['Welcome']; ?> What if i pass lang=alien? Then it would load no language at all.. require_once '/path/to/default/language.php'; // in case lang defines a non-supported language $LangFile = "lang.".$LangCode.".php"; //ie lang.en.php if(file_exists($LangFile)) require_once $LangFile; // overwrites the default language. Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874023 Share on other sites More sharing options...
xcoderx Posted July 12, 2009 Author Share Posted July 12, 2009 Eh nw me confused??? I use if empty or require default path to lang file? Wot if i store language in a dir as language how to cal it? Wil it work with and without login to site? Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874028 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 Wot if i store language in a dir as language how to cal it? $languageDirectory = '/path/to/languages/directory'; $languageFile = "lang.$lang.php"; require_once $languageDirectory . DIRECTORY_SEPARATOR . $languageFile; Wil it work with and without login to site? Login has nothing to do with it. Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874032 Share on other sites More sharing options...
xcoderx Posted July 12, 2009 Author Share Posted July 12, 2009 Supposing a registered user login and he had previously selected spanish language wil it not do any harm with session? And im lil bit confused using your code with madtechiez code, as in esembling both urz and his codes together. Help please. Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874040 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 $languageCode = (!empty($_GET['language']) && is_lang($_GET['language'])) ? $_GET['language'] : 'en'; $languagesDirectory = '/path/to/languages/directory'; $languagesFile = "lang.$langCode.php"; //ie lang.en.php require_once $languagesDirectory . DIRECTORY_SEPARATOR . 'lang.en.php'; // default language (has all the applications translations) if(file_exists($langFile)) require_once $languagesDirectory . DIRECTORY_SEPARATOR . $langFile; // overwrites the default language //echo some stuff echo $lang['Welcome'];// if not overwritten outputs english otherwise the chosen language function is_lang($lang) { return preg_match('/[a-z]{2}/', $lang); } Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874052 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 A much cleaner version: session_start(); $defaultLanguage = 'en'; $languagesDirectory = '/path/to/languages/directory'; language_load($defaultLanguage); if (!empty($_GET['language']) && language_is_valid_code($_GET['language']) && language_is_supported($_GET['language'])) { $_SESSION['language'] = $_GET['language']; language_load($_GET['language']); } function language_is_valid_code($language_code) { return preg_match('/[a-z]{2}/', $language_code); } function language_get_path($language) { global $languageDirectory; return implode(DIRECTORY_SEPARATOR, array($languageDirectory, "lang.$language.php")); } function language_is_supported($language) { global $languagesDirectory; return file_exists(language_get_path($language)); } function language_load($language) { require_once language_get_path($language); } Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874062 Share on other sites More sharing options...
MadTechie Posted July 12, 2009 Share Posted July 12, 2009 What if i pass lang=alien? Then it would load no language at all.. It was on a concept.. not a complete solution, also your "cleaner version" is slight over kill on the functions side! and would suite a OOP solution better, when you could add an simply else statement to mine! to fix the problem Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874137 Share on other sites More sharing options...
xcoderx Posted July 12, 2009 Author Share Posted July 12, 2009 Bros u r both great coders and very kind i hearthly thank u both fa givin me ur precious time and helping me, thanx alot Quote Link to comment https://forums.phpfreaks.com/topic/165607-solved-adding-languages-to-site-guidance-needed/#findComment-874227 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.