Lately I have added comments to my company blog. We have it localized, so things turned out to be not so straight forward as one can think.

django-localeurl

This neat piece of code allows us to localize our URLs. You can find it in its bitbucket repository. So, in my blog I have four languages (english, catalan, spanish and french), each one with a different prefixed URL:

http://example.com/blog/post_slug/
http://example.com/ca/blog/post_slug/
http://example.com/es/blog/post_slug/
http://example.com/fr/blog/post_slug/

Note that the english version doesn't have any prefix actually; this is a default from django-localeurl but you can change it if you want.

disqus url

So we want to include, of course, comments to our blog, and we are going to use disqus, a well-known service that makes our life more enjoyable without having to carry the burden of spam comments, implementing all the ubiquitous facebook, twitter, and many more logging services, to name a few.

When using the universal JavaScript embed code, by default this script uses window.location to send the URL of the current page. As long as we are using localized URLs, if we don't modify this behavior then each localized URL would have its own comments. Hopefully, we can specify our URL with the disqus_url configuration variable (as seen in their documentation). To make myself, the idea here is send ALWAYS your "canonical" URL (i.e. the one that does not have any language prefix). So, in our previous example, we would want to use http://example.com/blog/post_slug/.

localized urls

But no so fast! The django-localeurl warns: The localeurl application replaces the ``urlresolvers.reverse`` function to return locale-specific URLs. So if we were using the good ol' {% url %} to specify the url in the disqus_url, we would end up sending four different URLs, depending of the localization the user browser would have at the moment of the page request. Again, we are blessed with a feature that will give us the way to override this and set our "canonical" URL to the script variable: the locale_url tag (documented here).

passing an absolute URL

We do not want to pass a relative URL to Disqus, so we will need to construct the absolute URL with the help of Django's Sites contrib app. In order to have a context variable that outputs the correct site domain, we can either add such variable only from the view that renders the blog post or we can write a simple context processor (don't forget to add it to the TEMPLATE_CONTEXT_PROCESSORS too):

from django.conf import settings
from django.contrib.sites.models import Site

def current_site(request):
    """Add the current site to the context"""
    try:
        current_site = Site.objects.get_current()
        return {'current_site': current_site}
    except Site.DoesNotExist:
        return {'current_site': ''}

So the final code looks like this:

var disqus_url = 'http://{{ current_site }}{% locale_url "en" show_post post.slug %}';

Note that we use "en" to specify we want the english version fo the URL, the canonical one that, in fact, does not have "en" prefix.