Google specifies three scenarios for which rel=”alternate” hreflang=”x” is recommended:
- You translate only the template of your page, such as the navigation and footer, and keep the main content in a single language. This is common on pages that feature user-generated content, like a forum post.
- Your pages have broadly similar content within a single language, but the content has small regional variations. For example, you might have English-language content targeted at readers in the US, GB, and Ireland.
- Your site content is fully translated. For example, you have both German and English versions of each page.
Similarly, there are three means for which hreflang can be implemented. It can be tagged with the element within thesection of each page, expressed through the http header for non-html files, or within your XML sitemap. There is an obvious advantage to applying it within an xml sitemap for enterprise level sites, like the ones I tend to work on. Typically, it is much easier to get an updated xml sitemap uploaded than to apply new tagging to a myriad of pages. However, even when applied within an XML sitemap, it can a be tedious process for large websites. I created a quick python script to help make that process a little bit easier.
This is script is designed for a website where the alternate language site has an equal number of pages to the primary language. For example, there are the same number of pages for en-uk and en-ca as there are for en-us.