Jump to content
MakeWebGames

[HELP] php language system


Glock1533

Recommended Posts

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';

?>

 

Link to comment
Share on other sites

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 by rockwood
Link to comment
Share on other sites

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 by Sim
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
} 

 

Link to comment
Share on other sites

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  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...