【问题标题】:Lithium routes with different locales不同地区的锂路线
【发布时间】:2015-07-06 06:30:05
【问题描述】:

我在 Li3 中有一条 i18n 路线,如下所示:

Router::connect('/{:locale:[a-z]{2}/{:args}', [], [
    'continue' => true,
    'persist' => ['locale'],
]);

这样,当用户(或爬虫)使用语言前缀进入我的网站时,语言环境将用于生成网站上的每个链接。

出于 SEO 的目的,我需要在其他语言环境中生成 URL,例如:

GET /en/profile/john-doe
Canonical URL: https://www.example.com/en/profile/john-doe
Link hreflang for es: https://www.example.com/es/profile/john-doe
Link hreflang for pt: https://www.example.com/pt/profile/john-doe

我的货币方法是克隆当前请求,更改语言环境,从persist 数组中删除locale,并使用$request->to('url', ['absolute' => true]);

但我无法摆脱语言环境。

关于如何解决这个问题的任何建议?

【问题讨论】:

  • 我很确定您需要执行'persist' => ['locale'],然后只需覆盖您在视图中传递给$this->url() 的参数中的语言环境。您可能不需要像在帮助程序中那样重新传递所有参数。
  • 再次感谢!是的,我已经在做persist => ['locale']。这只是糟糕的复制/粘贴。

标签: php internationalization lithium php-5.5


【解决方案1】:

我终于解决了扩展HTML helper 类:

use lithium\template\helper\Html as BaseHtml;

class Html extends BaseHtml
{
    /**
     * Returns href lang link for a locale for the current request.
     *
     * @param string $locale
     * @return string <link />
     */
    public function getHrefLinkForLocale($locale)
    {
        return $this->link(
            'Canonical URL for ' . $locale,
            compact('locale') + $this->getRequest()->params,
            [
                'absolute' => true,
                'rel' => 'alternate',
                'type' => 'alternate',
                'hreflang' => $locale,
            ]
        );
    }
}

【讨论】:

  • 我认为在这种情况下,一个助手可能是正确的方法,尽管我会创建一个单独的助手,而不是扩展Html
  • 谢谢@NateAbele。起初,我没有意识到我可以在请求参数中包含locale。这就是诀窍。
猜你喜欢
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2021-01-10
相关资源
最近更新 更多