Skip to content

Commit

Permalink
Sitemap hreflang syntax invalid for regional language variants fix (#…
Browse files Browse the repository at this point in the history
…5638)

Sitemap hreflang syntax invalid for regional language variants fix
  • Loading branch information
humitos authored May 13, 2019
2 parents 87079ef + bbf3334 commit e0ea4a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion readthedocs/core/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,17 @@ def priorities_generator():
priorities = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
yield from itertools.chain(priorities, itertools.repeat(0.1))

def hreflang_formatter(lang):
"""
sitemap hreflang should follow correct format.
Use hyphen instead of underscore in language and country value.
ref: https://en.wikipedia.org/wiki/Hreflang#Common_Mistakes
"""
if '_' in lang:
return lang.replace("_", "-")
return lang

def changefreqs_generator():
"""
Generator returning ``changefreq`` needed by sitemap.xml.
Expand Down Expand Up @@ -407,7 +418,7 @@ def changefreqs_generator():
private=False,
)
element['languages'].append({
'hreflang': translation.language,
'hreflang': hreflang_formatter(translation.language),
'href': href,
})

Expand Down
10 changes: 10 additions & 0 deletions readthedocs/rtd_tests/tests/test_doc_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ def test_sitemap_xml(self):
main_language_project=self.public,
language='translation-es'
)
# sitemap hreflang should follow correct format.
# ref: https://en.wikipedia.org/wiki/Hreflang#Common_Mistakes
hreflang_test_translation_project = fixture.get(
Project,
main_language_project=self.public,
language='zh_CN'
)
response = self.client.get(
reverse('sitemap_xml'),
HTTP_HOST='public.readthedocs.io',
Expand Down Expand Up @@ -283,3 +290,6 @@ def test_sitemap_xml(self):
private=False,
),
)
# hreflang should use hyphen instead of underscore
# in language and country value. (zh_CN should be zh-CN)
self.assertContains(response, 'zh-CN')

0 comments on commit e0ea4a5

Please sign in to comment.