I couldn't find a forum more suitable for pnRender plugins, but if there is one, could the mods please just move this post? Thanks!
Due to the lack of ways to properly switch languages in Zikula, I finally tried my hands on a pnRender plugin. However, things do not go quite as they are supposed to. The code I wrote works under these conditions:
- base URL, ending in root directory, no index.php or anything else at the end
- any URL, no previous language switch at the end
Naturally, this means that it does not work right after another language switch occured. The (wrong) behaviour is like this:
- in case of base URL + index.php, it adds "&newlang" instead of "?newlang"
- for any other URL, each language switch adds another ampersand in front of "&newlang", so after a few consecutive switches it becomes "&&&&&newlang"
Here is the code:
Code
function smarty_function_pnswitchlang($params, &$smarty)
{
$assign = isset($params['assign']) ? $params['assign'] : null;
$newlang = isset($params['newlang']) ? $params['newlang'] : null;
$baseresult = htmlspecialchars(pnGetBaseURL());
$urlresult = htmlspecialchars(pnGetCurrentURL());
if ($baseresult == $urlresult) //we are at site's base url
{
$result = $urlresult.'index.php?newlang='.$newlang;
}
else
{
$badparam = 'newlang';
$hateyou = '&';
$strip = strpos($urlresult, $badparam);
if ($strip === false)
{
$result = $urlresult.'&newlang='.$newlang;
}
else
{
$urlresult = substr ($urlresult, 0, ($strip-1)); //stripping off newlang + 1 char in front of it
echo (substr ($urlresult, 0, -9) == $trap); //checking for index.php at end
if (substr ($urlresult, 0, -9) == $trap)
{
$result = $urlresult.'?newlang='.$newlang;
return $result;
}
elseif (substr ($urlresult, 0, -5) == $hateyou) //checking for & from previous lang change
{
$urlresult = substr ($urlresult, 0, -5);
$result = $urlresult.'&newlang='.$newlang;
}
else //no previous lang change, no base url+index.php
{
$result = $urlresult.'&newlang='.$newlang;
}
}
}
return $result;
}
{
$assign = isset($params['assign']) ? $params['assign'] : null;
$newlang = isset($params['newlang']) ? $params['newlang'] : null;
$baseresult = htmlspecialchars(pnGetBaseURL());
$urlresult = htmlspecialchars(pnGetCurrentURL());
if ($baseresult == $urlresult) //we are at site's base url
{
$result = $urlresult.'index.php?newlang='.$newlang;
}
else
{
$badparam = 'newlang';
$hateyou = '&';
$strip = strpos($urlresult, $badparam);
if ($strip === false)
{
$result = $urlresult.'&newlang='.$newlang;
}
else
{
$urlresult = substr ($urlresult, 0, ($strip-1)); //stripping off newlang + 1 char in front of it
echo (substr ($urlresult, 0, -9) == $trap); //checking for index.php at end
if (substr ($urlresult, 0, -9) == $trap)
{
$result = $urlresult.'?newlang='.$newlang;
return $result;
}
elseif (substr ($urlresult, 0, -5) == $hateyou) //checking for & from previous lang change
{
$urlresult = substr ($urlresult, 0, -5);
$result = $urlresult.'&newlang='.$newlang;
}
else //no previous lang change, no base url+index.php
{
$result = $urlresult.'&newlang='.$newlang;
}
}
}
return $result;
}
Unfortunately, pndebug does not show any of my variables, so I am rather walking in the dark. (Any way to display plugin variables?) Some of the conditions do not work, but I just cannot find out why. Any suggestions?
--
And out of the chaos sounded a voice and spoke: "Be happy and smile, for it could have come worse."
And I was happy and smiled - and it came worse...
