Login

Localized URLs (www-en)

Author:
zeeg
Posted:
February 25, 2007
Language:
Python
Version:
Pre .96
Score:
2 (after 2 ratings)

An example on how we changed our localization middleware to use www-en.<domain> instead of it being hidden in the cookie.

This also changes zh-cn to cn, and zh-tw to tw in the URLs.

This is only a base snippet and you will most likely need to modify it to fit your needs.

 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
from django.utils.cache import patch_vary_headers
from django.utils import translation
from django.http import HttpResponseRedirect
from django.conf.settings import DEBUG
from django.utils.translation import gettext_lazy as _

LANGUAGES = (
	('de', _('German')),
	('en', _('English')),
	('fr', _('French')),
	('es', _('Spanish')),
	('zh-cn', _('Simplified Chinese')),
	('zh-tw', _('Traditional Chinese')),
	('ko', _('Korean')),
)
LANGUAGE_MAP = {
	'zh-cn': 'cn',
	'zh-tw': 'tw',
}
LANGUAGES_DICT = dict(LANGUAGES_SHORT)

class LocaleMiddleware(object):
	def validate_language(self, request, language):
		# If DEBUG mode is enabled just set the language to en so we dont have to worry about redirects on our runserver
		if DEBUG:
			translation.activate('en')
			return

		host = request.META.get('HTTP_HOST').split('.')
		
		# If only the domain is requested redirect to www.<domain>
		if len(host) == 2:
			return HttpResponseRedirect('http://www.%s' % request.META['HTTP_HOST'])

		host_path = host[0].split('-')
		lang_code = len(host_path) >= 2 and '-'.join(host_path[1:]) or None
		# Validate the language selected
		if not lang_code or len(lang_code) > 2:
			if lang_code in LANGUAGE_MAP:
				lang_code = LANGUAGE_MAP[language]
			elif lang_code not in LANGUAGES_DICT:
				lang_code = 'en'
			return HttpResponseRedirect('http://%s-%s.%s%s' % (host_path[0], lang_code, '.'.join(host[-2:]), request.path))

		# If the language is CN or TW set it to the proper language. We could probably just reverse our LANGUAGE_MAP
		# dictionary to do this automatically
		if lang_code == 'cn':
			real_lang_code = 'zh-cn'
		elif lang_code == 'tw':
			real_lang_code = 'zh-tw'
		else:
			real_lang_code = lang_code

		# Store this in the session if it's not already set
		if translation.check_for_language(real_lang_code):
			if hasattr(request, 'session') and request.session.get('django_language', None) != real_lang_code:
				request.session['django_language'] = real_lang_code
		
		request.LANGUAGE_CODE = lang_code
		language = translation.get_language_from_request(request)
		if not language:
			language = 'en'
		translation.activate(language)
		# If for some reason the language we activated doesn't match our real language requested we redirect to the language activated
		if language != real_lang_code:
			return HttpResponseRedirect('http://%s-%s.%s%s' % (host_path[0], language, '.'.join(host[-2:]), request.path))

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

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

More like this

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

Comments

Please login first before commenting.