Reputation: 1242
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
Upvotes: 0
Views: 679
Reputation: 9715
I am not sure $this->translator
[ Translator::instance(); ] has this method there ! its just method of LocalePicker
component.
This is implemented code there, so use this and define your own method
protected function withPreservedQueryString($pageUrl, $locale)
{
$page = $this->getPage();
$query = request()->query();
/**
* @event translate.localePicker.translateQuery
* Enables manipulating the URL query parameters
*
* You will have access to the page object, the old and new locale and the URL query parameters.
*
* Example usage:
*
* Event::listen('translate.localePicker.translateQuery', function($page, $params, $oldLocale, $newLocale) {
* if ($page->baseFileName == 'your-page-filename') {
* return YourModel::translateParams($params, $oldLocale, $newLocale);
* }
* });
*
*/
$translatedQuery = Event::fire('translate.localePicker.translateQuery',
[$page, $query, $this->oldLocale, $locale], true);
$query = http_build_query($translatedQuery ?: $query);
return $query ? $pageUrl . '?' . $query : $pageUrl;
}
Use it like this
$pageUrl = $this->
withPreservedQueryString( // <- this call
$this->translator->makeLocaleUrlFromPage($locale),
$locale
);
if ($this->property('forceUrl')) {
return Redirect::to($this->translator->getPathInLocale($pageUrl, $locale));
}
it should work, if any doubt please comment.
Upvotes: 1