I needed a function for a web width multilanguage support. I saw the plugin: Support for multilanguage sites, Internationalization (I18N), but it wasn't i what i needed.
The web of test: http://cumbe.no-ip.biz/gs203/.
The function:
This function has its source in the function of getsimple: get_navigation() .
I have used the customfield plugin, too.
To use the function lang_navigation($currentpage), it is necesary:
I think that i don't have left anything.
Pleased test it.
As always, i'm sorry for my english.
Regards.
The web of test: http://cumbe.no-ip.biz/gs203/.
The function:
Code:
//****************************************************//
//** FUNCTION: lang_navigation()
//**
//** Returns the main menu of the site with
//** the language selected in /data/other/lang
//***************************************************//
function lang_navigation($currentpage) {
if (@$_GET['lang'] !=''){
file_put_contents(GSDATAOTHERPATH."/lang", @$_GET['lang'] );
}
global $PRETTYURLS;
global $SITEURL;
global $LANG;
$file_lang = trim(file_get_contents(GSDATAOTHERPATH."/lang"));
if ($PRETTYURLS == 1){
$n = 1;
} else {
$n = 0;
}
if ($file_lang != substr($LANG,0,2)){
$fl='-'.$file_lang;
} else {
$fl='';
}
$menu = '';
$path = GSDATAPAGESPATH;
$dir_handle = @opendir($path) or die("Unable to open $path");
$filenames = array();
while ($filename = readdir($dir_handle)) {
$filenames[] = $filename;
}
$count="0";
$pagesArray = array();
if (count($filenames) != 0) {
foreach ($filenames as $file) {
if ($file == "." || $file == ".." || is_dir(GSDATAPAGESPATH . $file) || $file == ".htaccess" ) {
// not a page data file
} else {
$data = getXML(GSDATAPAGESPATH . $file);
if ($data->private != 'Y') {
$pagesArray[$count]['menuStatus'] = $data->menuStatus;
$pagesArray[$count]['menuOrder'] = $data->menuOrder;
$pagesArray[$count]['menu'] = stripslashes(htmlspecialchars_decode($data->menu, ENT_QUOTES));
$pagesArray[$count]['url'] = $data->url;
$pagesArray[$count]['title'] = stripslashes(htmlspecialchars_decode($data->title, ENT_QUOTES));
$pagesArray[$count]['parent'] = $data->parent;
$pagesArray[$count]['translate'] = $data->translate;
$pagesArray[$count]['labellang'] = stripslashes(htmlspecialchars_decode($data->labellang, ENT_QUOTES));
$pagesArray[$count]['orderlang'] = $data->orderlang;
$count++;
}
}
}
}
$pagesSorted = subval_sort($pagesArray,'orderlang');
if (count($pagesSorted) != 0) {
foreach ($pagesSorted as $page) {
$sel = ''; $classes = '';
$url_nav = $page['url'];
if (($page['menuStatus'] == 'Y' && $fl == '') || ($page['menuStatus'] != 'Y' && substr($url_nav, -3,3) == $fl && $page['parent'] =='')) {
if ("$currentpage" == "$url_nav") { $classes = "current ". $page['parent'] ." ". $url_nav; } else { $classes = trim($page['parent'] ." ". $url_nav); }
if ($page['menu'] == '') { $page['menu'] = $page['title']; }
if ($page['title'] == '') { $page['title'] = $page['menu']; }
if ($url_nav == 'index'){
if ($page['translate'] == 'y') {
if ($PRETTYURLS == 1) {
$menu .= '<li class="'. $classes .'" ><a href="'.$SITEURL.'index" title="'. strip_quotes($page['title']) .'">'. $page['labellang'].'</a></li>'."\n";
} else {
$menu .= '<li class="'. $classes .'" ><a href="'.$SITEURL.'index.php?id=index" title="'. strip_quotes($page['title']) .'">'. $page['labellang'].'</a></li>'."\n";
}
} else { //not translate
$menu .= '<li class="'. $classes .'" ><a href="'.$SITEURL.'" title="'. strip_quotes($page['title']) .'">'. $page['labellang'].'</a></li>'."\n";
}
} else { //not is index
$fu= find_url($page['url'],$page['parent']);
$menu .= '<li class="'. $classes .'" ><a href="'. $fu.' " title="'. strip_quotes($page['title']) .'">'.$page['labellang'].'</a></li>'."\n";
}
}
}
}
closedir($dir_handle);
echo exec_filter('menuitems',$menu);
}
This function has its source in the function of getsimple: get_navigation() .
I have used the customfield plugin, too.
To use the function lang_navigation($currentpage), it is necesary:
- a file called lang in /data/other
- 3 fields in data/other/customfields.xml
Code:
<item>
<desc>translate</desc>
<label>Translate(y/n)</label>
<type>text</type>
</item>
<item>
<desc>labellang</desc>
<label>Label_Lang</label>
<type>text</type>
</item>
<item>
<desc>orderlang</desc>
<label>Order_Lang</label>
<type>text</type>
</item>
- Pages of language of admin:
- only must to have enable 'Add to Menu'.
- Field 'translate' = n (lowercase)
- Field 'Label_lang' : is the caption that will appears in the menu.
- Field 'Order_lang' : is the order that we will appears in the menu.
- The other pages with other language:
- NO enable 'Add to Menu'.
- the name of page, must have the same slug/URL that original but finished in: dash + two letters for code language (-de for example to deutsch, -es for spanish, -it for italy, -en for england....).
- Field 'translate' = y (lowercase)
- Field 'Label_lang' : is the caption that will appears in the menu.
- Field 'Order_lang' : is the order that we will appears in the menu.
- What we do is call the language in which appear all pages translated. To call to language, for example, in header:
- a href="/index.php?lang=es" , for the language of admin
- a href="/index.php?id=index-en&lang=en", for language england
- a href="/index.php?id=index-de&lang=de", for language deutsch
- Works with 'Fancy URLs' too.
- PD: i forgot to call of function in the template. Tou must replace:
<?php get_navigation(return_page_slug()); ?>
by:
<?php lang_navigation(return_page_slug()); ?>
- The function lang_navigation($currentpage), must be in functions.php of you theme; or could be in a plugin too...
I think that i don't have left anything.
Pleased test it.
As always, i'm sorry for my english.
Regards.