Login

locale based on domain

Author:
zeeg
Posted:
June 8, 2007
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

This is something we're using over at Curse to keep things clean and simple for our users.

  • We needed a url for any language code (which the domain provides) vs a cookie
  • We needed a to only store 2 letter codes in the db for each language (thus the key doesn't always match the code)

This consists of two major modifications:

  • LANGUAGES in settings.py is a completely different format and would need changed based on your setup
  • the locale middleware has a couple absolute instances of where to point a user by default.
  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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
# settings.py

LANGUAGES = {
	'en': {
		'domain': 'curse.com',
		'name': 'English',
		'code': 'en',
	},
	'de': {
		'domain': 'de.curse.com',
		'name': 'German',
		'code': 'de',
	},
	'it': {
		'domain': 'curse.it',
		'name': 'Italian',
		'code': 'it',
	},
	'tw': {
		'domain': 'curse.tw',
		'name': 'Traditional Chinese',
		'code': 'zh-tw',
	},
	'cn': {
		'domain': 'cn.curse.com',
		'name': 'Simplified Chinese',
		'code': 'zh-cn',
	},
	'fr': {
		'domain': 'curse.fr',
		'name': 'French',
		'code': 'fr',
	},
	'es': {
		'domain': 'curse.es',
		'name': 'Spanish',
		'code': 'es',
	},
	'kr': {
		'domain': 'kr.curse.com',
		'name': 'Korean',
		'code': 'ko-kr',
	}
}

# middleware/locale.py

#from django.utils.cache import patch_vary_headers
from django.utils import translation
from django.http import HttpResponseRedirect,HttpResponse
from django.conf import settings

from cursesite.middleware import threadlocals
from cursesite.core.translation import utils as cursetranslation

class LocaleMiddleware(object):
	def validate_language(self, request):
		if hasattr(settings, 'OVERRIDE_IL8N_LANG'):
			lang_code = settings.OVERRIDE_IL8N_LANG
			self.set_lang(request, lang_code)
			return

		# LB check request
		if request.path.split('/')[0] == "donotremove.html":
			return HttpResponse("check")	
			
		host = request.META.get('HTTP_HOST').split('.')
		
		# Ex: uri = curse-gaming.com
		if request.META['HTTP_HOST'] in settings.LANGUAGES_BY_DOMAIN:
			return HttpResponseRedirect('http://www.%s%s' % (request.META['HTTP_HOST'], request.path))
		
		portal = host[0]
		domain = '.'.join(host[1:])
		if domain not in settings.LANGUAGES_BY_DOMAIN:
			return HttpResponseRedirect('http://%s.curse.com%s' % (portal, request.path))
		lang_code = settings.LANGUAGES_BY_DOMAIN[domain]

		# Set the language	
		self.set_lang(request, lang_code)
		settings.WEBSITE_DOMAIN = settings.LANGUAGES[lang_code]['domain']
		settings.SESSION_COOKIE_DOMAIN = settings.WEBSITE_DOMAIN

	def set_lang(self, request, lang_code):
		real_lang_code = settings.LANGUAGES[lang_code]['code']

		translation.activate(real_lang_code)
		request.LANGUAGE_CODE = lang_code
		threadlocals._thread_locals.lang = request.LANGUAGE_CODE
		cursetranslation.activate(lang_code)

	def process_request(self, request):
		#language = translation.get_language_from_request(request)
		return self.validate_language(request)

	def process_response(self, request, response):
		#patch_vary_headers(response, ('Accept-Language',))
		response['Content-Language'] = request.LANGUAGE_CODE
		translation.deactivate()
		return response

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.