【问题标题】:OctoberCMS Translate plugin redirect with hashOctoberCMS 翻译插件重定向与哈希
【发布时间】:2020-06-01 00:58:29
【问题描述】:

我正在使用 OctoberCMS 翻译插件 (https://octobercms.com/plugin/rainlab-translate),它的工作方式符合我的预期。

但是,我有一个自定义要求,即在 url 中生成哈希(即http://localhost/ibis/whats-on/details#2020-sydney-international-whitewater-event - #2020-sydney-international-whitewater-event)。

现在的问题是,当我应该使用下面的代码进行重定向时,

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

我可以重定向,但是,我的 url 失去了它的哈希标签,它变成了类似于 http://localhost/ibis/fr/whats-on/details 的东西(fr 是我选择的法语)。

下面是我在布局的代码选项卡中将代码覆盖到onSwitchLocale 方法,该方法由翻译插件提供。

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);
}

如您所见,我正在尝试使用哈希完成 url 重定向,但我在这里收到错误提示

调用未定义的方法 RainLab\Translate\Classes\Translator::withPreservedQueryString()

并且无法继续此请求。我进一步研究发现withPreservedQueryString 具有受保护的方法,我尝试了各种方法来执行此方法但无法执行。

所以首先我需要完成这个,其次我想在我的网址中附加hash 标签。

有人可以从这里指导我如何实现这一目标吗?

谢谢

【问题讨论】:

    标签: php laravel octobercms octobercms-plugins


    【解决方案1】:

    我不确定$this-&gt;translator [ Translator::instance(); ] 有这个方法!它只是LocalePicker 组件的方法。

    这是那里实现的代码,所以使用它并定义你自己的方法

    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;
    }
    

    这样使用

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

    它应该可以工作,如果有任何疑问,请发表评论。

    【讨论】:

    • 感谢您的回复。我需要在我的translate 插件中添加withPreservedQueryString 吗?
    • 你可以在你定义function onSwitchLocale()的地方添加它
    • 它的说法Call to undefined method RainLab\Translate\Classes\Translator::makeLocaleUrlFromPage()" on line 27 of /var/www/html/ibis/storage/cms/cache/d1/b5/whatson.htm.php
    • 我在布局文件中添加了withPreservedQueryString 方法。图片截图 URL - ibb.co/fMTrsBW
    • 我还需要复制makeLocaleUrlFromPage方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 2014-10-25
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多