Changing part of referral params (language)

recap of the story below
I have a multi-language URL structure like: domain.com/de/read/car
After changing the language (through the Language controller) vistors are sent back to the page they came from via $this->redirect($this->referer())
Problem is: if they came from domain.com/de/read/car they are sent back to that URL ( domain.com/de/read/car), but I would like that to be: domain.com/new-language/read/car
end recap

Hi peoples,

I am putting together a multilingual site. Most things are going pretty well, but there’s one thing that I can’t seem to figure out. I dont even know if it is possible at all (but I think it just might be).

Thing is: I have set the routing up to include the language as part of the URL:
domain.com/de/read/car (and domain.com/en/read/car etc).
I have also set up the routing, so that /read connects to the Articles controller.

I have a languagecotroller with a ‘change’ action to change the locale/language . It sets the new lanuage (I18n::setLocale) and stores it in session. After this action (after language has changed), I would like people to go back where they came from. So, I ended the action with: $this->redirect($this->referer); Works like a charm.

But the problem is: the referral could be something like:
domain.com/de/read/car, so after someone had changed the language to ‘en’, they are referred back to domain.com/de/read/car. And I would, of course, like that to be domain.com/en/articles/car

So, my question is: Is it possible to change that part of the referral? (and: how?)

What I have done so far, is: Tried to deconstruct the $this->referer data. As follows:
$refer_url = $this->referer(’/’, true);
$parse_url_params = Router::parse($refer_url);

when I now debug($parse_url_params), I get something like this:

Array
(
    [language] => de
    [pass] => Array
        (
            [0] => car
        )

    [controller] => Articles
    [action] => view
    [plugin] => 
    [_matchedRoute] => /:language/read/*
)

Looks good! What I did then is:

$refer_url = $this->referer(’/’, true);
$parse_url_params = Router::parse($refer_url);
$parse_url_params[‘language’]= ‘en’;
debug($parse_url_params);

Now I get:

Array
(
    [language] => en
    [pass] => Array
        (
            [0] => car
        )

    [controller] => Articles
    [action] => view
    [plugin] => 
    [_matchedRoute] => /:language/read/*
)

In my LanguageController, at the end of the ‘change’ action, I have changed this:
$this->redirect($this->referer);
to this:
$this->redirect($parse_url_params);

----Now to test it out:

When I am on this page/url:
https://localhost/site/de/read/car
and I change the language, I am referred to this:
https://localhost/site/en/read?pass[0]=car&_matchedRoute=%2F%3Alanguage%2Fread%2F*

So: I am almost there, I think. Since the language did indeed change. But the variable/param ‘car’ is not passed as it should. (and &_matchedRoute=%2F%3Alanguage%2Fread%2F* should not be visible at all).
But I can’t figure out to get this working. How to ‘reconstruct’ or ‘unparse’ or ‘rebuild’ the changed referral.

If anyone could help me out, I would greatly appriciate it :slight_smile:

Kind regards,
Elbert

After try-fail-try, I have implemented the following ‘solution’ (I think it might not be the correct way to do it, but it’s the only way I could figure out, maybe it helps someone)

in my LanguageController I have added:

    $refer_url = $this->referer('/', true);
    $parse_url_params = Router::parse($refer_url);
    $cntrlr = $parse_url_params['controller'];
    $ctn = $parse_url_params['action'];
    if (!empty($parse_url_params['pass'][0])) {
        $slug = $parse_url_params['pass'][0];
    } else {
        $slug = '';
    }


    if ($slug != '') {
        $this->redirect(array(
            'language' => $new_language,
            'controller' => $cntrlr,
            'action' => $ctn,
            $slug
        ));
    } elseif (!empty($ctn)) {

        $this->redirect(array(
            'language' => $new_language,
            'controller' => $cntrlr,
            'action' => $ctn

        ));
    } else {
        $this->redirect($this->referer());
    }