Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Duplicated language segment in meta-tag #302

Open
ragnarfrosti opened this issue Dec 6, 2017 · 0 comments
Open

Duplicated language segment in meta-tag #302

ragnarfrosti opened this issue Dec 6, 2017 · 0 comments
Labels

Comments

@ragnarfrosti
Copy link

ragnarfrosti commented Dec 6, 2017

We are using the latest version, 1.1.56, and found an issue when using the plugin on a site with multiple languages where there are no entries related to the URL. Here is an example how you can reproduce the issue:

site.com [English - default language]
site.com/es [Spanish]

In < head > we show the canonical and the alternative language meta tags.
All pages that have entries work like they should, but pages buildt straight from templates will get a duplicate language-segment in the meta tags if we visit the page from other languages than the default language.


Example from the /cart page that doesn’t have an entry, but only the templates/cart.twig template:

site.com/cart [English]

<head>
    <link rel="canonical" href="http://site.com/cart" />
    <link rel="alternate" href="http://site.com/cart" hreflang="x-default" />
    <link rel="alternate" hreflang="en" href="http://site.com/cart" />
    <link rel="alternate" hreflang="es" href="http://site.com/es/cart" />
</head>

Here, everything looks good. But if I visit the page in Spanish I get:

site.com/es/cart [Spanish]

<head>
    <link rel="canonical" href="http://site.com/es/cart" />
    <link rel="alternate" href="http://site.com/es/cart" hreflang="x-default" />
    <link rel="alternate" hreflang="en" href="http://site.com/es/cart" />
    <link rel="alternate" hreflang="es" href="http://site.com/es/es/cart" />
</head>

Here the Spanish language has two /es/ in the URL and the english has one /es/ as well


The problem seems to be from the getLocalizedUrls() method in SeomaticService.php (row 3037) where there is no entry for the URL so it goes to the ‘else’ condition:

$locales = craft()->i18n->getSiteLocales();

foreach ($locales as $locale)
{
    $localeId = $locale->getId();
    if (!in_array($localeId, $excludeLocales)) {
        $localizedUrls[$localeId] = UrlHelper::getSiteUrl($requestUri, null, null, $localeId);
    }
}

where $requestUri = craft()->request->getRequestUri(); includes the /es/ in the URI.

So when the siteUrl is appended in the function UrlHelper::getSiteUrl($requestUri, null, null, $localeId); the language segment (es) is added as well.

We’ve temporarly fixed the issue by changing the else condition to this:

$locales = craft()->i18n->getSiteLocales();

$queryString = craft()->request->getQueryStringWithoutPath();
$uriString = '/' . implode(craft()->request->getSegments(), '/');
$requestUri = $uriString . ($queryString ? '?' . $queryString : '');

foreach ($locales as $locale)
{
    $localeId = $locale->getId();
    if (!in_array($localeId, $excludeLocales)) {
        $localizedUrls[$localeId] = UrlHelper::getSiteUrl($requestUri, null, null, $localeId);
    }
}

Best Regards,
Ragnar

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants