Multi Language Menus

Last edited by WigAnt:
Replaces old-style internal links with new pipe-split links.
Fri, 20 May 2016 07:38 UTC [diff]

See also:
 

MultiLanguageMenus is an extension of the menu solution described in WikkaMenus. Installation of this extension requires prior installation of WikkaMenus.

This extension provides a possibility to have a link on a page to change the language of menu items. The menu items and their corresponding language codes are stored in a table. A cookie is installed to check the language chosen by the user.

The multi language menu system:

Installation requires:

This extension has been designed for three languages, but it can easily be changed into a version for more languages.

Note:
There is actually an easier solution to this, as was pointed out to me by EmeraldIsland in the notes to this page. Using the original version of WikkaMenus, it requires merely one action (similar to the language action below), adding a small function to wikka.php to deal with a language cookie, using suffixes in the menuname corresponding to the language, and some recoding of the header action. Rather quick and easy, and doesn't require dealing with the database :)

The only reason to choose a more elaborate version as described in this document is for admin purposes. It provides a better overview of corresponding menus in different languages.


Extended wikka_menus Database Table


The database table wikka_menus needs to be extended. Here's the description:

CREATE TABLE `wikka_menus` (
  `name` varchar(20) NOT NULL default '',
  `lng1` char(2) NOT NULL default '',
  `lng2` char(2) NOT NULL default '',
  `lng3` char(2) NOT NULL default '',
  `content` varchar(255) NOT NULL default '',
  `content2` varchar(255) NOT NULL default '',
  `content3` varchar(255) NOT NULL default '',
  `css_class` varchar(20) NOT NULL default '',
UNIQUE KEY `name` (`name`)
) TYPE=MyISAM;


The field lng1 is the language code corresponding to the menu description in content, lng2 corresponds to content2 and lng3 to content3. Note that the content is not renamed with respect to WikkaMenus, it ensures that its {{menu}} action can still be used to change the menu in the default language.

The new action {{multilangmenu}} (see below) provides an interface to edit all the fields. The interface is similar to the one in WikkaMenus, the content fields are next to each other, so you can easily see how menus if different languages correspond.


Two New Actions


Language Switch and Redirection

This action installs a cookie in which the language code is stored. This code is typically a two-letter language code, like de or en.
The action takes two arguments. The first states the new language to be used for the menus, the second states to from which page the new language version will start.

The action {{language nl MyDutchPage}} will change menu language to Dutch (nl) and will redirect to MyDutchPage. It's typical use would be in a language link somewhere in the menus.

Grab the text and save as language.php in your action directory.

<?php
	if (empty($wikka_vars)) { $wikka_vars="en HomePage"; }
	$pieces = explode(" ", $wikka_vars, 2);
	setcookie("wikkalang", $pieces[0]);
	header("Location: wikka.php?wakka=".$pieces[1]."");
	exit;
?>


Menu editor

The following text is the code for the action {{multilangmenu}}. It shows a similar interface as in WikkaMenus but extended to fit the extra table entries. Grab the text and save as multilangmenu.php in your action directory. It's typical use would be to put {{multilangmenu}} on a single page.

Note that the original {{menu}} can still be used, but it will only show and update content.

<!-- Multi Language Menu Configuration Interface -->
<!-- Version 1.0, by Cornelis Wiebering -->
<!-- Based on Menu Configuration Interface 1.0 -->

<h2>Multilanguage Menu Configuration</h2>
<br />

<?php
if ($this->IsAdmin()) {
	switch ($_POST["operation"]) {
	case "Create Menu":
		if ($this->MenuExists($_POST["newname"])) {
			echo $this->Format("<<**Sorry!** --- A menu named \"".$_POST["newname"]."\" already exists. --- Please choose another name<<::c::--- --- ");
		} else {
			$this->CreateMenu($_POST["newname"], $_POST["css"]);
			echo $this->Format("<<**Thanks!** --- Menu \"".$_POST["newname"]."\" has been created<<::c:: --- ");
		}
		break;

	case "Delete Menu":
		echo $this->Format("<<**Confirmation required**<<::c:: --- Do you really want to delete **".$_POST["name"]."**? --- --- ");
		$formdelete = 	'<input type="hidden" name="name" value="'.$_POST["name"].'" />'.
			'<input type="submit" name="operation" value="Confirm Deletion" style="width: 120px" accesskey="s" />'.
			'<input type="button" value="Cancel" onClick="history.back();" style="width: 120px" /><p>&nbsp;</p>';
		print $this->FormOpen("","","post");
		print $formdelete;
		print $this->FormClose();
		break;

	case "Confirm Deletion":
		$this->DeleteMenu($_POST["name"]);
			echo $this->Format("<<**Thanks!** --- Menu \"".$_POST["name"]."\" has been deleted<<::c:: --- ");
		break;

	case "Rename Menu":	
		if ($this->MenuExists($_POST["newname"])) {
			echo $this->Format("<<**Sorry!** --- A menu named \"".$_POST["newname"]."\" already exists. --- Please choose another name<<::c:: --- --- ");
		} else {
			echo $this->Format("<<**Confirmation required**<<::c:: --- Do you really want to rename **".$_POST["name"]."** as **".$_POST["newname"]."**? --- --- ");
			$formrename =  '<input type="hidden" name="oldname" value="'.$_POST["name"].'" />'.
				'<input type="hidden" name="newname" value="'.$_POST["newname"].'" />'.
				'<input type="submit" name="operation" value="Confirm Rename" style="width: 120px" accesskey="s" />'.
				'<input type="button" value="Cancel" onClick="history.back();" style="width: 120px" /><p>&nbsp;</p>';  
			print $this->FormOpen("","","post");
			print $formrename;
			print $this->FormClose();
		}
		break;

	case "Confirm Rename":
		$this->RenameMenu($_POST["oldname"], $_POST["newname"]);
		echo $this->Format("<<**Thanks!** --- Menu has been renamed as \"".$_POST["newname"]."\"<<::c:: --- --- ");
		break;

	case "Update Menu":
		$this->SaveMenu($_POST["name"], $_POST["lng1"], $_POST["lng2"], $_POST["lng3"],
						$this->TrimMenu($_POST["content"]),
						$this->TrimMenu($_POST["content2"]),
						$this->TrimMenu($_POST["content3"]));
		echo $this->Format("<<**Menu configuration stored** --- Thanks for updating \"".$_POST["name"]."\"!<<::c:: --- --- ");
		break;
	}

	// load stored menus and print menu forms
	echo $this->Format('Please enter menu items on separate lines. --- You can either use //""CamelCase"" links// like ##""PageIndex""## --- or //forced links// like: ##""[[http://www.mydomain.com | External Link]]""## --- --- --- '); 
	$allmenus = $this->LoadAllMenus();
	foreach ($allmenus as $item) {
		$formarray[$item["name"]] = 'Menu name: <strong>'.$item["name"].'</strong><br />'.
			'<input type="hidden" name="name" value="'.$item["name"].'" />'.
			'<table><tr>'.
			'<td>language 1</td><td>language 2</td><td>language 3</td>'.
			'</tr><tr>'.
			'<td><input type="text" name="lng1" value="'.$item["lng1"].'"></td>'.
			'<td><input type="text" name="lng2" value="'.$item["lng2"].'"></td>'.
			'<td><input type="text" name="lng3" value="'.$item["lng3"].'"></td>'.
			'</tr><tr>'.
			'<td><textarea name="content" rows="6" cols="30">'.$item["content"].'</textarea></td>'.
			'<td><textarea name="content2" rows="6" cols="30">'.$item["content2"].'</textarea></td>'.
			'<td><textarea name="content3" rows="6" cols="30">'.$item["content3"].'</textarea></td>'.
			'</tr></table>'.
			'<input type="submit" name="operation" value="Update Menu" style="width: 120px" accesskey="s" />'.
			'<input type="submit" name="operation" value="Delete Menu" style="width: 120px" /><br />'.
			'<input type="text" name="newname" value="'.$item["name"].'" style="width: 120px">'.
			'<input type="submit" name="operation" value="Rename Menu" style="width: 120px" /><p>&nbsp;</p>';
		print $this->FormOpen("","","post"); 
		echo $formarray[$item["name"]];
		print($this->FormClose());
	}

	// "Create menu" form
	$newmenuform = '<table><tr>'.
		'<td>Menu name:</td><td><input type="text" name="newname" value="new_menu_name" style="width: 120px"></td></tr>'.
		'<tr><td>CSS class:</td><td> <input type="text" name="css" value="css_class" style="width: 120px"><td></tr></table>'.
		'<input type="submit" name="operation" value="Create Menu" style="width: 120px" /><br />';
	echo $this->Format("== Create a new menu ==");
	print $this->FormOpen("","","post");
	echo $newmenuform;
	print($this->FormClose());
	
} else {
	print("<em>Sorry, only Wikka Administrators can modify the Menu configuration.</em>");
}

?>


Changes in wikka.php



Grab the following code and put it in wikka.php above the code for the menus. The cookie will be know as wikkalang.

	// LANGUAGE COOKIES
	// For use with multi-language menus. Version 1.0, Written by Cornelis Wiebering

function SetLanguage($lng) {
	$this->SetPersistentCookie("wikkalang", $lng);
	return $lng;
}

function GetLanguage() {
	if ($lang = $this->GetCookie("wikkalang")) { return $lang; }
	else { return $this->SetLanguage("en"); }
}


Menu Functions

Grab the following code and put it in place of the menu functions code in wikka.php.

	// MENU FUNCTIONS
	// Edited to be able to handle multi-language menus. Version 1.0, Written by Cornelis Wiebering

function LoadMenu($name) {
	$content = $this->LoadSingle("SELECT * FROM ".$this->config["table_prefix"]."menus WHERE name = '".$name."'");
	return $content;
}

function LoadAllMenus() {
	$menurow = $this->LoadAll("SELECT * FROM ".$this->config["table_prefix"]."menus");
	return $menurow;
}

function SaveMenu($name, $lng1, $lng2, $lng3, $cont, $cont2, $cont3) {
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET lng1 ='".mysql_real_escape_string(trim(str_replace("\r", "", $lng1)))."' WHERE name = '".$name."' LIMIT 1");
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET lng2 ='".mysql_real_escape_string(trim(str_replace("\r", "", $lng2)))."' WHERE name = '".$name."' LIMIT 1");
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET lng3 ='".mysql_real_escape_string(trim(str_replace("\r", "", $lng3)))."' WHERE name = '".$name."' LIMIT 1");
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET content ='".mysql_real_escape_string(trim(str_replace("\r", "", $cont)))."' WHERE name = '".$name."' LIMIT 1");
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET content2 ='".mysql_real_escape_string(trim(str_replace("\r", "", $cont2)))."' WHERE name = '".$name."' LIMIT 1");
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET content3 ='".mysql_real_escape_string(trim(str_replace("\r", "", $cont3)))."' WHERE name = '".$name."' LIMIT 1");
}

function MenuExists($name) {
	if ($this->LoadMenu($name)){
		return true;
	}
}

function RenameMenu($oldname, $newname) {
	$this->Query("UPDATE ".$this->config["table_prefix"]."menus SET name = '".$newname."' WHERE name = '".$oldname."' LIMIT 1");
}

function DeleteMenu($name) {
	$this->Query("DELETE FROM ".$this->config["table_prefix"]."menus WHERE name = '".$name."' LIMIT 1");
}

function CreateMenu($name, $css_class) {
	$this->Query("INSERT INTO ".$this->config["table_prefix"]."menus SET name = '".mysql_real_escape_string($name)."', content = '', content2 = '', content3 = '', lng1 = '', lng2 = '', lng3 = '', css_class = '".mysql_real_escape_string($css_class)."'");
}

function TrimMenu($list) {
	foreach (explode("\n", $list) as $line) {
		$line = trim($line);
		$trimmed_list .= $line."\n";
	}
	return $trimmed_list;
}

function PrintMenu($name) {
	if ($menurow = $this->LoadMenu($name)) {
		$lang = $this->GetLanguage();
		$lng1 = $menurow["lng1"];
		$lng2 = $menurow["lng2"];
		$lng3 = $menurow["lng3"];
		$menu = "<ul class=\"".$menurow["css_class"]."\">";
		switch($lang) {
			case $lng1:
				foreach (explode("\n", $menurow["content"]) as $menuitem) { $menu .="<li>".$this->Format($menuitem)."</li>\n"; }
				break;
			case $lng2:
				foreach (explode("\n", $menurow["content2"]) as $menuitem) { $menu .="<li>".$this->Format($menuitem)."</li>\n"; }
				break;
			case $lng3:
				foreach (explode("\n", $menurow["content3"]) as $menuitem) { $menu .="<li>".$this->Format($menuitem)."</li>\n"; }
				break;
			default:
				foreach (explode("\n", $menurow["content"]) as $menuitem) { $menu .="<li>".$this->Format($menuitem)."</li>\n"; }
				break;
		}
	}
	$menu .= "</ul>\n";
	return $menu;
}



Notes

-- WigAnt


CategoryUserContributions
There are 2 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki