0

I'm trying to set the language of a wordpress website dynamically.

I'm using following code :

<?php    
$lang = 'en';

if(isset($_GET['$lang'])){
    $lang = $_GET['$lang'];
}
if($lang=='en'){
    include('en.php');
}
else{
    include('nl.php');
}

?>

With en.php and nl.php being files in the same folder containing associative arrays with values I'd like to echo through the page.

However, if I upload the en.php file on the server, everything starting from that code is considered null and non existing. I've set a comment right before the code, it's shown in the browser debug tools, but nothing after except the closing tags.

An example of the html :

<section id="welcome" class="container">
            <h1 class="menu titreone"><?php echo $dictionary['WELCOME_TITLE'];?></h1>   
            <p><?php echo $dictionary['WELCOME_TEXT'];?></p>
        </section>

(where, obviously, Welcome_title and Welcome_text are part of the array)

Would anyone be kind enough to point my mistake? I'm not really used to php/wordpress, so it might be very simple, but the bad news is that I've no idea what to look after.

Thank you

DoctorPrisme
  • 93
  • 11
  • what do you mean by "starting from that code is considered null and non existing", do you mean all php code after the include is not executed, even in the main file, or is the code in the included file only ignored? Do you have an error displayed (and error logs activated)? Concerning the HTML example, can you confirm it is actually placed in a php file where your php code that includes the files is placed before? – Kaddath Jan 29 '18 at 16:48
  • @Kaddath : Actually, not only the php but even the html code is ignored starting from that php balise, in the header, footer and main file (header and footer are external to the main body, and included via php. Footer isn't even shown in the debug tool, guess it's no longer rendered) The html example is part of index.php. The php code I've shown is part of header.php. – DoctorPrisme Jan 29 '18 at 16:56
  • From what you say, it really looks like a PHP error. Note that combined with HTML, this error can be hidden in your page (behind a div for example, or in the HTML's `header` section). So even if your error logs are on, always check the original page source in your console (Debug tab in Firefox, Source in Chrome, or even directly in the network response) – Kaddath Jan 29 '18 at 17:06
  • How can I activate the logs? :o – DoctorPrisme Jan 29 '18 at 18:45
  • to display errors refer [to this question](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display), but keep in mind that `ini_set` and `error_reporting` must be placed at the very beginning of the PHP code. – Kaddath Jan 30 '18 at 08:29

1 Answers1

0

You can refer this code

<?php
    // include language configuration file based on selected language
    $lang = "en";
    if(isset($_GET['lang'])){ 
        $lang = $_GET['lang']; 
    } 
    require_once($lang.".php");
?>

If your issue will not fix from this then you can refer this link

http://phppot.com/php/multi-language-support-to-website-using-php/

Note: en.php and nl.php should be on same directory

Jasbir
  • 398
  • 5
  • 14
  • Hi, thanks for your proposed answer. I'll try this in a few moments; However if the file is named "en.php", why should I require "lang".$lang.".php" ? Shouldn't it be .$lang.".php" ? (Sorry if my question is stupid, again, not really used to php) – DoctorPrisme Jan 30 '18 at 08:44
  • Even worse, I get a blank page with nothing shown. I put your code in the top of the header, and I only have the html/head/body tags with nothing inside. – DoctorPrisme Jan 30 '18 at 09:23