etrader Posted September 11, 2011 Share Posted September 11, 2011 I have a function to read terms from an array stored in language file as function _language($term) { include('fr.php'); if(!empty($lang[$term])) { $lterm=$lang[$term]; } else { $lterm = $term;} return $lterm; } The problem is that I get the language code from url as ?lang=en or ?lang=fr; but I cannot bring this string into the function. How I can tell the function to read appropriate language file (fr.php or en.php or es.php) upon choosing language in ?lang=XX Quote Link to comment https://forums.phpfreaks.com/topic/246892-function-to-load-different-language-files/ Share on other sites More sharing options...
voip03 Posted September 11, 2011 Share Posted September 11, 2011 use $_GET['lang'] <?php function _language($term) { include('fr.php'); if(!empty($lang[$term])) {$lterm=$lang[$term];} else { $lterm = $term;} return $lterm; } echo language( $_GET['lang']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246892-function-to-load-different-language-files/#findComment-1267930 Share on other sites More sharing options...
etrader Posted September 11, 2011 Author Share Posted September 11, 2011 Thanks but you did not get my problem. I get language by $_GET; but how to load corresponding language file. $language = $_GET['lang']; Now I need something like this within the function include("$language.php"); My problem is how to run the function for each language (e.g. 50 languages). Quote Link to comment https://forums.phpfreaks.com/topic/246892-function-to-load-different-language-files/#findComment-1267973 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2011 Share Posted September 11, 2011 Assuming that you want all references to the language file to occur through that function, you should probably use a class, but you could (probably, this is untested) do this using a function and a static variable to prevent the code from including the language file every time the function is called. On the first call to the function, you would detect that the static variable holding the $lang array has not been initialized and then validate the $_GET (the $_GET array is a super global and is automatically available inside of functions) variable and include the correct language file, assigning the $lang array to the function's statically defined variable. Quote Link to comment https://forums.phpfreaks.com/topic/246892-function-to-load-different-language-files/#findComment-1267981 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.