Lithium routes with different locales

I have an i18n route in Li3 that looks like this:

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

      

This way, when a user (or crawler) enters my site with a language prefix, the locale is used to generate every link on the site.

For SEO purposes, I need to create URLs in other locales like:

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

      

My monetary approach clones the current request, changes the locale, removes locale

from the array, persist

and uses $request->to('url', ['absolute' => true]);

.

But I cannot get rid of the locale.

Any suggestions for fixing this?

+3


source to share


1 answer


Finally, I figured it was extending the class HTML

:



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

      

+1


source







All Articles