Ticket #27395: sitemap_hreflang.diff

File sitemap_hreflang.diff, 2.3 KB (added by Hovi, 8 years ago)

Path

  • django/contrib/sitemaps/__init__.py

    diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py
    index 8d779ca..35fb159 100644
    a b class Sitemap(object):  
    126126                'changefreq': self.__get('changefreq', item),
    127127                'priority': str(priority if priority is not None else ''),
    128128            }
     129            # possibly different parameter name here, cause some people prefer using
     130            # different method of entering them (http header, html page)
     131            if getattr(self, 'i18n', False):
     132                links = []
     133                current_lang_code = translation.get_language()
     134                for lang_code, lang_name in settings.LANGUAGES:
     135                    translation.activate(lang_code)
     136                    href = "%s://%s%s" % (protocol, domain, self.__get('location', item))
     137                    links.append({
     138                        "hreflang": lang_code,
     139                        "href": href
     140                    })
     141                translation.activate(current_lang_code)
     142                url_info["xhtml_links"] = links
    129143            urls.append(url_info)
    130144        if all_items_lastmod and latest_lastmod:
    131145            self.latest_lastmod = latest_lastmod
  • django/contrib/sitemaps/templates/sitemap.xml

    diff --git a/django/contrib/sitemaps/templates/sitemap.xml b/django/contrib/sitemaps/templates/sitemap.xml
    index 30ca3c0..0cb85fe 100644
    a b  
    11<?xml version="1.0" encoding="UTF-8"?>
    2 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     2<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
    33{% spaceless %}
    44{% for url in urlset %}
    55  <url>
     
    77    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
    88    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
    99    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
     10    {% if url.xhtml_links %}
     11        {% for link in url.xhtml_links %}
     12            <xhtml:link rel="alternate" hreflang="{{ link.hreflang }}" href="{{ link.href }}" />
     13        {% endfor %}
     14    {% endif %}
    1015   </url>
    1116{% endfor %}
    1217{% endspaceless %}
Back to Top