Glock1533 Posted August 21, 2020 Share Posted August 21, 2020 hello guys, i'm trying to create a system where i have 2 different languages in php and so far i have this code <?php // Start a Session, You might start this somewhere else already. session_start(); //global $lang; // What languages do we support $available_langs = array('en','pt','es'); // Set our default language session $_SESSION['lang'] = 'pt'; if(isset($_GET['lang']) && $_GET['lang'] != ''){ // check if the language is one we support if(in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } } echo $lang['txt_TRADUCTION1']; echo '<br/>'; echo $lang['txt_TRADUCTION2']; echo '<br/>'; echo $lang['TESTANDO']; echo '<br/>'; echo $lang['txt_TRADUCTION4']; // Include active language include('teste/'.$_SESSION['lang'].'/'.$_SESSION['lang'].'.php'); ?> but he returns this error to me: Warning: include(teste/en/en.php): failed to open stream: No such file or directory in C:\xampp\htdocs\teste\index.php on line 33 Warning: include(): Failed opening 'teste/en/en.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\teste\index.php on line 33 and the files are in the proper folders en.php file that is inside the en / folder <?php $lang['txt_TRADUCTION1'] = 'Name'; $lang['txt_TRADUCTION2'] = 'Age'; $lang['txt_TRADUCTION3'] = 'Last Name'; $lang['txt_TRADUCTION4'] = 'Phone'; ?> Quote Link to comment Share on other sites More sharing options...
rockwood Posted August 21, 2020 Share Posted August 21, 2020 (edited) 2 hours ago, Glock1533 said: hello guys, i'm trying to create a system where i have 2 different languages in php and so far i have this code <?php // Start a Session, You might start this somewhere else already. session_start(); //global $lang; // What languages do we support $available_langs = array('en','pt','es'); // Set our default language session $_SESSION['lang'] = 'pt'; if(isset($_GET['lang']) && $_GET['lang'] != ''){ // check if the language is one we support if(in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } } echo $lang['txt_TRADUCTION1']; echo '<br/>'; echo $lang['txt_TRADUCTION2']; echo '<br/>'; echo $lang['TESTANDO']; echo '<br/>'; echo $lang['txt_TRADUCTION4']; // Include active language include('teste/'.$_SESSION['lang'].'/'.$_SESSION['lang'].'.php'); ?> <?php // Start a Session, You might start this somewhere else already. session_start(); //global $lang; // What languages do we support $available_langs = array('en','pt','es'); // Set our default language session $_SESSION['lang'] = 'pt'; if(isset($_GET['lang']) && $_GET['lang'] != ''){ // check if the language is one we support if(in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } } echo $lang['txt_TRADUCTION1']; echo '<br/>'; echo $lang['txt_TRADUCTION2']; echo '<br/>'; echo $lang['TESTANDO']; echo '<br/>'; echo $lang['txt_TRADUCTION4']; // Include active language include('teste/'.$_SESSION['lang'].'/'.$_SESSION['lang'].'.php'); ?> but he returns this error to me: Warning: include(teste/en/en.php): failed to open stream: No such file or directory in C:\xampp\htdocs\teste\index.php on line 33 Warning: include(): Failed opening 'teste/en/en.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\teste\index.php on line 33 Warning: include(teste/en/en.php): failed to open stream: No such file or directory in C:\xampp\htdocs\teste\index.php on line 33 Warning: include(): Failed opening 'teste/en/en.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\teste\index.php on line 33 and the files are in the proper folders en.php file that is inside the en / folder <?php $lang['txt_TRADUCTION1'] = 'Name'; $lang['txt_TRADUCTION2'] = 'Age'; $lang['txt_TRADUCTION3'] = 'Last Name'; $lang['txt_TRADUCTION4'] = 'Phone'; ?> <?php $lang['txt_TRADUCTION1'] = 'Name'; $lang['txt_TRADUCTION2'] = 'Age'; $lang['txt_TRADUCTION3'] = 'Last Name'; $lang['txt_TRADUCTION4'] = 'Phone'; ?> f(isset($_GET['lang']) && $_GET['lang'] != ''){ what are you trying to do here? basically wrong if(empty($_GET['lang'])){ exit from the code here } 2 hours ago, Glock1533 said: Edited August 21, 2020 by rockwood Quote Link to comment Share on other sites More sharing options...
Sim Posted August 21, 2020 Share Posted August 21, 2020 (edited) Try include en/en.php T looks like you are already inside the teste folder. Although it looks like a test, I still think your cars should be more descriptive then what you have $lang["name"] ="Name"; instead of $lang["txt_trasuvtion"]> and possibly create an empty lang file $lang["name"] = $lang["dob"] = $lang["nickname"] = Ect This will allow a translator to make a new language file fairly fast. Edited August 21, 2020 by Sim Quote Link to comment Share on other sites More sharing options...
rockwood Posted August 22, 2020 Share Posted August 22, 2020 14 hours ago, Glock1533 said: if(isset($_GET['lang']) && $_GET['lang'] != ''){ // check if the language is one we support if(in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } } no need isset and blank chk there you are chking same thing many times if(in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } this is enough Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted August 22, 2020 Share Posted August 22, 2020 17 hours ago, rockwood said: no need isset and blank chk there you are chking same thing many times if(in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } this is enough Depends on what's running before it, this snippet might result in an undefined index warning on lang. Simply verify its existence. if(array_key_exists('lang', $_GET) && in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } Quote Link to comment Share on other sites More sharing options...
rockwood Posted August 22, 2020 Share Posted August 22, 2020 2 hours ago, Magictallguy said: Depends on what's running before it, this snippet might result in an undefined index warning on lang. Simply verify its existence. if(array_key_exists('lang', $_GET) && in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } if(array_key_exists('lang', $_GET) && in_array($_GET['lang'], $available_langs)) { $_SESSION['lang'] = $_GET['lang']; // Set session } that means the structure having issue and the url not appropriate Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted August 22, 2020 Share Posted August 22, 2020 8 minutes ago, rockwood said: that means the structure having issue and the url not appropriate I agree - for the expected use of it; might not mean much to a user specifically searching for weakness. Quote Link to comment 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.