Zen Cart Logo
Forums / General Questions / Implementing hreflang in a multilanguage store

Implementing hreflang in a multilanguage store

Views: 4,499

Results 1 to 15 of 15
29 Sep 2014, 9:18 PM
#1
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Implementing hreflang in a multilanguage store

Does anybody know exactly how to implement the hreflang tags to a multilanguage store? I guess it should be added to html_header.php, but what would the code look like?
I have the CEON URI module installed also.

2 Oct 2014, 2:18 PM
#2
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Implementing hreflang in a multilanguage store

DML73:

Does anybody know exactly how to implement the hreflang tags to a multilanguage store? I guess it should be added to html_header.php, but what would the code look like?

http://www.w3schools.com/tags/att_a_hreflang.asp

2 Oct 2014, 4:26 PM
#3
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: Implementing hreflang in a multilanguage store

Thanks for the link, however I am familiar with the tags, I was more referring to what the php code would look like.

4 Oct 2014, 5:06 PM
#4
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Implementing hreflang in a multilanguage store

DML73:

Thanks for the link, however I am familiar with the tags, I was more referring to what the php code would look like.

Hmmmm......

example HTML

<a href="http://www.w3schools.com" hreflang="en">W3Schools</a>

Same example using PHP

<?php 
echo "<a href=\"http://www.w3schools.com\" hreflang=\"en\">W3Schools</a>" ; 
?>

I have a feeling that that this isn't quite what you are seeking though ???

Cheers
RodG

4 Oct 2014, 6:05 PM
#5
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: Implementing hreflang in a multilanguage store

It was something like that I was looking for. However the hreflang tag needs to be "assigned" to each zencart page; for multiple languages. I guess it couldn't be made with a few lines of code.

5 Oct 2014, 10:32 AM
#6
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: Implementing hreflang in a multilanguage store

DML73:

It was something like that I was looking for. However the hreflang tag needs to be "assigned" to each zencart page; for multiple languages

I still don't quite follow what you are trying to do or achieve, but I'm guessing that you want the hreflang="en" to be replaced with a variable?

If so then something like......

<?php 
$myVar = "en" ; 


echo "<a href=\"http://www.w3schools.com\" hreflang=\".$myVar.\">W3Schools</a>" ; 
?>

......should probably do the trick.

You will of course need to set the value of $myVar according to whatever condition it is you need it for though.

Perhaps if you could explain or demonstrate exactly what it is you are trying to do I/we may be able to offer more/better help.

Maybe this will also help?
https://support.google.com/webmasters/answer/189077?hl=en

Cheers
RodG

5 Oct 2014, 3:54 PM
#7
dml73 avatar

dml73

Zen Follower

Join Date:
Oct 2007
Posts:
413
Plugin Contributions:
0

Re: Implementing hreflang in a multilanguage store

Thanks, and sorry for not explaining clearly what it is Im trying to do. Here it is:

I have these two domains:

http://www.tronicore.co.uk/
http://www.tronicore.dk/

on the same server/same zencart installation. I am re-directing each language to the correct domain in htaccess.

The co.uk domain has all product and catagory pages like this example:
http://www.tronicore.co.uk/en/rs232-rs485-rs422-converters

and all .dk product and catagory pages like this example:
http://www.tronicore.dk/da/rs232-rs485-konvertere

The CEON URL module adds the /en/ and /da/ sub-folders, and also the re-written URL of course.

Now I would like the hreflang tags to be assigned to all product and catagory pages in accordance to the link you mentioned (https://support.google.com/webmasters/answer/189077?hl=en)

So for example the URL:
http://www.tronicore.co.uk/en/rs232-rs485-rs422-converters

should contain:

<link rel="alternate" hreflang="en" href="http://www.tronicore.co.uk/en/rs232-rs485-rs422-converters" />

and the URL:
http://www.tronicore.dk/da/rs232-rs485-konvertere

should contain:

<link rel="alternate" hreflang="da" href="http://www.tronicore.dk/da/rs232-rs485-konvertere" />

All other product and category pages should follow this pattern.

10 Oct 2016, 9:31 AM
#8
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

Re: Implementing hreflang in a multilanguage store

Zen Cart 1.55a
in templates/YOUR_TEMPLATE/common/html_header.php
we have

<?php
  // BOF hreflang for multilingual sites
  if (!isset($lng) || (isset($lng) && !is_object($lng))) {
    $lng = new language;
  }
  reset($lng->catalog_languages);
  while (list($key, $value) = each($lng->catalog_languages)) {
    if ($value['id'] == $_SESSION['languages_id']) continue;
    echo '<link rel="alternate" href="' . ($this_is_home_page ? zen_href_link(FILENAME_DEFAULT, 'language=' . $key, $request_type) : $canonicalLink . '&language=' . $key) . '" hreflang="' . $key . '" />' . "\n";
  }
  // EOF hreflang for multilingual sites
?>

This produces a hreflang for each of the OTHER languages, but not that of this actual page.
ie if you offer english and spanish on your site, the english page generates:

<link rel="alternate" href="https://www.YOURSITE.com/somepage?language=es" hreflang="es">

From what I read here,
Concise
https://www.semrush.com/blog/7-common-hreflang-mistakes-and-how-to-fix-them/
Detailed
https://moz.com/blog/using-the-correct-hreflang-tag-a-new-generator-tool

this is incomplete. There should also be a link to the self-same page:

<link rel="alternate" href="https://www.YOURSITE.com/somepage?language=es" hreflang="es"> <link rel="alternate" href="https://www.YOURSITE.com/somepage?language=en" hreflang="en">

I looked into this after receiving warning emails from Google Search Console,and this was the reason.

So, this link requires commenting out:

<?php // BOF hreflang for multilingual sites if (!isset($lng) || (isset($lng) && !is_object($lng))) { $lng = new language; } reset($lng->catalog_languages); while (list($key, $value) = each($lng->catalog_languages)) { //if ($value['id'] == $_SESSION['languages_id']) continue; echo '<link rel="alternate" href="' . ($this_is_home_page ? zen_href_link(FILENAME_DEFAULT, 'language=' . $key, $request_type) : $canonicalLink . '&language=' . $key) . '" hreflang="' . $key . '" />' . "\n"; } // EOF hreflang for multilingual sites ?>
11 Oct 2016, 5:17 PM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Implementing hreflang in a multilanguage store

Question:
You suggested commenting out:```
[I]//if ($value['id'] == $_SESSION['languages_id']) continue;[/I]


... But on a site with only one language, that will cause the <link rel=alternate hreflang=foo> to appear. Is that desirable when there's no other language, and the language is already specified in the <html> tag?
12 Oct 2016, 9:16 AM
#10
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

Re: Implementing hreflang in a multilanguage store

Is that desirable when there's no other language, and the language is already specified in the <html> tag?
No. That's me not thinking outside my box.

13 Oct 2016, 8:35 AM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Implementing hreflang in a multilanguage store

After reading a number of sources, it seems best to skip if only 1 language, or display for all languages if more than 1.

So, proposing this: https://github.com/zencart/zencart/pull/1314

1 Jan 2017, 6:49 PM
#12
enzo_ita avatar

enzo_ita

Zen Follower

Join Date:
Jul 2009
Posts:
402
Plugin Contributions:
0

Re: Implementing hreflang in a multilanguage store

DrByte:

After reading a number of sources, it seems best to skip if only 1 language, or display for all languages if more than 1.

So, proposing this: https://github.com/zencart/zencart/pull/1314

Hi,
I have implemented the mod proposed so the html_header section becomes

// BOF hreflang for multilingual sites

if (isset($language_list) || $language_list = $lng->get_available_languages() && sizeof($language_list) > 1) {
     foreach($language_list as $key=>$value) {
echo '    <link rel="alternate" href="' . ($this_is_home_page ? zen_href_link(FILENAME_DEFAULT, 'language=' . $value['code'], $request_type) : $canonicalLink . (strpos($canonicalLink, '?') ? '&' : '?') . 'language=' . $value['code']) . '" hreflang="' . $value['code'] . '" />' . "\n";
     }
   }
   // EOF hreflang for multilingual sites

But I get a blank page and the following error log

PHP Fatal error: Call to a member function get_available_languages() on a non-object in /home2/xxxxx/public_html/includes/templates/responsive_classic/common/html_header.php on line 71

I have noticed that in github the file lines to modified start with 48 while in my file the lines were ```php
// BOF hreflang for multilingual sites


I would like to understand this.
Thanks.

Happy New Year from Italy

enzo
1 Jan 2017, 10:36 PM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Implementing hreflang in a multilanguage store

enzo-ita:

PHP Fatal error: Call to a member function get_available_languages() on a non-object in /home2/xxxxx/public_html/includes/templates/responsive_classic/common/html_header.php on line 71

I have noticed that in github the file lines to modified start with 48 while in my file the lines were ```php
// BOF hreflang for multilingual sites

> 
> I would like to understand this.
That's because the change posted is for v1.6.0.

For your v1.5 site, do what torvista posted above to get it to output the tags for all languages. And since you're supporting multiple languages already you won't need the other part of the change I mentioned in v160.
2 Jan 2017, 10:05 AM
#14
enzo_ita avatar

enzo_ita

Zen Follower

Join Date:
Jul 2009
Posts:
402
Plugin Contributions:
0

Re: Implementing hreflang in a multilanguage store

DrByte:

That's because the change posted is for v1.6.0.

For your v1.5 site, do what torvista posted above to get it to output the tags for all languages. And since you're supporting multiple languages already you won't need the other part of the change I mentioned in v160.
Thanks. I di it and it works. For my other site that is mono language I do not need to touch the html_header.php right?
Ciao ciao

6 Apr 2017, 12:42 PM
#15
bramf avatar

bramf

New Zenner

Join Date:
Apr 2017
Location:
Netherlands
Posts:
20
Plugin Contributions:
0

Re: Implementing hreflang in a multilanguage store

Is the code in html_header already corrected in 1.55e (I copied the code from zen-cart 1.55e) or should I still apply the fix described in this forum ( I have 3 languages installed)?