mittul
mittul

I am using OctoberCMS Translate plugin (https://octobercms.com/plugin/rainlab-translate) and its working as per my expectations.

However, I have one custom requirement in which I am generating hash in url (i.e. http://localhost/ibis/whats-on/details#2020-sydney-international-whitewater-event - #2020-sydney-international-whitewater-event).

Now the thing is, when I am supposed to redirect using below code,

 {% for code, name in locales %}
                           <a class="dropdown-item" href="#" data-request="onSwitchLocale" data-request-data="locale: '{{ code }}'">
                              {{ name |upper }}
                           </a>

I am able to redirect, however, my url looses its hash tag and it becomes something like http://localhost/ibis/fr/whats-on/details (fr is my selected french language).

Here below is my overwrite code in my layout's code tab to onSwitchLocale method which is provided by Translate plugin.

use RainLab\Translate\Models\Locale as LocaleModel;
use RainLab\Translate\Classes\Translator;
use October\Rain\Router\Router as RainRouter;

function onSwitchLocale()
{
    $this->translator = Translator::instance();
    $locale = post('locale');

    if (!$locale = post('locale')) {
            return;
    }
    $this->translator->setLocale($locale);

    $pageUrl = $this->translator->withPreservedQueryString($this->translator->makeLocaleUrlFromPage($locale), $locale);
    if ($this->property('forceUrl')) {
        return Redirect::to($this->translator->getPathInLocale($pageUrl, $locale));
    }

    return Redirect::to($pageUrl);
}

As you can see I am trying to accomplish url redirect with hash, but I am getting error here saying

Call to undefined method RainLab\Translate\Classes\Translator::withPreservedQueryString()

And unable to proceed this request. I researched further and found withPreservedQueryString has protected method and I tried various ways to execute this method but unable to do so.

So first I need to accomplish this and second I want to append that hash tag in my url.

Can someone guide me from here on how can I achieve this ?

Thanks

Last updated

mjauvin
mjauvin

The query string would do you no good, the #2020-sydney-international-whitewater-event is part of the URL, not the query string.

1-2 of 2