Fork me on GitHub

solved: trouble w/ pnRender plugin to switch lang  Bottom

  • Hi,

    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 = '&amp';
            $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...
  • Looking at the code above now, I see that it is a bad idea to start coding when you're on the move. I solved the problem, the plugin seems to work fine so far, and if I don't encounter any further problems, then it may be time to beautify the code and release it.

    --
    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...
  • Sorry for the delay, but this plugin was written shortly before I moved. Now I have settled in somehow and here is the plugin. Extract and put the file into the pnRender/plugins directory. Usage example is in the file.

    Have fun...

    --
    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...

This list is based on users active over the last 60 minutes.