Last active
July 9, 2022 14:54
-
-
Save urlsangel/70f2b6a7fe46ee8a6133536a76caf48f to your computer and use it in GitHub Desktop.
Google-friendly sitemaps for multilingual Wagtail sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wagtail.models import Page | |
from translation.utils import get_translated_sitemap_urls | |
class ExtendedPage(Page): | |
class Meta: | |
abstract = True | |
def get_sitemap_urls(self, request): | |
page = self.localized | |
default_data = [{ | |
'location': page.full_url, | |
'lastmod': (page.last_published_at or page.latest_revision_created_at), | |
}] | |
return get_translated_sitemap_urls(page, request, default_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> | |
{% spaceless %} | |
{% for url in urlset %} | |
<url> | |
<loc>{{ url.location }}</loc> | |
{% if url.lastmod %} | |
<lastmod>{{ url.lastmod|date:'Y-m-d' }}</lastmod> | |
{% endif %} | |
{% for item in url.alternates %} | |
<xhtml:link rel="alternate" hreflang="{{ item.locale.language_code }}" href="{{ item.url }}"/> | |
{% endfor %} | |
</url> | |
{% endfor %} | |
{% endspaceless %} | |
</urlset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Amend these to the domains your translated site uses | |
TRANSLATED_DOMAINS = { | |
'en': 'www.girlsnotbrides.org', | |
'fr': 'www.fillespasepouses.org', | |
'es': 'www.girlsnotbrides.es', | |
} | |
def get_translatable_page_siblings(instance, only_live=True, include_self=False): | |
translations = instance.get_translations(inclusive=include_self) | |
if only_live: | |
translations = translations.live() | |
return translations | |
def get_translated_sitemap_urls(page, request, default_data): | |
domains = TRANSLATED_DOMAINS | |
host = request.get_host() | |
domain = None | |
lang = None | |
for k, v in domains.items(): | |
if v == host: | |
domain = v | |
lang = k | |
if domain and lang: | |
# return empty array if this page isn't the current language | |
if page.locale.language_code != lang: | |
return [] | |
# get default sitemap data for page, return if no data | |
data = next(iter(default_data)) | |
if not data: | |
return [] | |
# get self and siblings, add to data and return | |
siblings = get_translatable_page_siblings(page, only_live=True, include_self=True).select_related('locale') | |
data['alternates'] = siblings | |
return [data] | |
# return an empty array if none of the above | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To implement:
sitemap.xml
should be placed in the root of any included templates folder, e.gtemplates/sitemap.xml
ExtendedPage
should be used as an inheritedmixin
to all of your Wagtail pagesutils.py
can be placed anywhere, e.g.translation/utils.py