How to select menu


I am using PHP Include on my website today to minimize the number of files that I have to edit when I change something in the menu.

I have now come up with the idea that the user should have the possibility to change the language of the menu since the website is multilingual.

So the menu is in Swedish today, if I add a link that says something like “View menu in English”, is it possible for me to change the menu file and display the English version om all the pages untill the user changes it back to Swedish?

Note, I hardly know any PHP (include i about it).

Kind Regards

I’m no expert, but I would try something along these lines.

// GET the language from the URL
$lang = preg_replace('#[^a-z]#i', '', $_GET['id']);  // filter everything but letters
    if ( $lang = "eng") {
        include  $_SERVER["DOCUMENT_ROOT"] . "/includes/menu_eng.php";
        $switch = '<a href="index.php?lang=swe" title="View in Swedish">View Menu in Swedish</a>' ;
    else {
        include  $_SERVER["DOCUMENT_ROOT"] . "/includes/menu_swe.php";
        $switch = '<a href="index.php?lang=eng" title="View in English">View in Menu English</a>' ;
    echo $switch ;
    <h1>Page Titile</h1>
    <p>Page Content.</p>

The links ($switch) set the language ($lang) variable in the URL. There are two menu files, the $lang variable chooses which to include.
This will default to Swedish, being in the else section, so you get Swedish if there is no variable (or something else) in the URL.
You may probably want to change your content according to language, it could be done in the same way.

if ( $lang = "eng" ) {
    include  $_SERVER["DOCUMENT_ROOT"] . "/includes/indexcontent_eng.php";
else {
    include  $_SERVER["DOCUMENT_ROOT"] . "/includes/indexcontent_swe.php";

Might be worth looking to the Google Translate plugin if you are looking to minimise code. After all having a separate file for each language results in more files and more conditionals than there needs to be.

Yeah same here. Check the google translate api ( :wink:

I fail to see the benefit of having an English menu if the page content is still going to be Svenska.

Google translate would be easy enough, but unless it has improved some (and I imagine it has since last I checked), the translation might be less than ideal - though probably good enough for users to “figure out” what it means.

Other than using Google translate, having a multiple language site is not a trivial undertaking. WordPress uses gettext which might be a viable approach if you really want to do it right and don’t mind the extra work involved.

Google Translate is an easy option and a useful resource, but the translations are far from perfect. Good enough for people to get the idea what you are saying. But if you want it to read well for your site visitors (why wouldn’t you?) it needs writing out properly in both languages. Yes it’s more files, or bigger files, more code, but to do it right, it’s what you need.
The above example with two menu include files is just one way to do it. You could have just one menu include and put all the ‘if’ statements in that, that may be a better option to keep the menus in sync, which is probably why you used an include in the first place.
The same could go for the page content, rather than a container file and two includes, it could all go in one file. First setting variables for both languages within if statements, then echoing them out.

if  ($lang == "eng"){
	$header1 = '<h1>Heading in English</h1>';
	$subhead1 = '<h2>Sub Head in English</h2>';
	$para1 = '<p>A load of page content in English.</p>';
	$subhead2 = '<h2>Another Sub Head in English</h2>';
	$para2 = '<p>A load more page content in English.</p>';
	$piccaption1 = '<p>A caption for a picture in English</p>'
else {
	$header1 = '<h1>Heading in Swedish</h1>';
	$subhead1 = '<h2>Sub Head in Swedish</h2>';
	$para1 = '<p>A load of page content in Swedish.</p>';
	$subhead2 = '<h2>Another Sub Head in Swedish</h2>';
	$para2 = '<p>A load more page content in Swedish.</p>';
	$piccaption1 = '<p>A caption for a picture in Swedish</p>'
include  $_SERVER["DOCUMENT_ROOT"] . "/includes/menu.php";
echo $header1 ;
echo $subhead1 ;
echo $para1 ;
<div class="picture">
	<img src="images/photo.jpg" alt="A Photo" />
	<?php echo $piccaption1 ; ?>
echo $subhead2 ;
echo $para2 ;