Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 45 lines (36 sloc) 1.56 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
from django.utils import translation
from django.conf import settings

LANGUAGES = getattr(settings, 'LANGUAGES', [])
MIDDLEWARE_CLASSES = getattr(settings, 'MIDDLEWARE_CLASSES', ())
MULTILINGUAL_URL = \
    'cms.middleware.multilingual.MultilingualURLMiddleware' \
    in MIDDLEWARE_CLASSES

def GetMultilanguageSitemapClass(sitemap, language):
    """Wrap a Sitemap class within a language-aware class"""
    class InnerClass(sitemap):
        language = None

        def __init__(self, *args, **kwargs):
            self.language = language
            super(InnerClass, self).__init__(*args, **kwargs)

        def items(self, *args, **kwargs):
            translation.activate(self.language)
            result = super(InnerClass, self).items(*args, **kwargs)
            translation.deactivate()
            return result

        def location(self, *args, **kwargs):
            translation.activate(self.language)
            url = super(InnerClass, self).location(*args, **kwargs)
            translation.deactivate()
            return '/%s%s' % (self.language, url)

    return InnerClass

def MakeMultilanguageSitemap(sitemaps):
    """Takes a sitemap dict and modify it to hold the same sitemap classes
for every configured language"""
    if not MULTILINGUAL_URL:
        return sitemaps

    for name, sitemap in sitemaps.items():
        del sitemaps[name]
        for lang in LANGUAGES:
            new_name = '%s_%s' % (name, lang[0])
            sitemaps[new_name] = GetMultilanguageSitemapClass(sitemap, lang[0])

    return sitemaps
Something went wrong with that request. Please try again.