Sessionhandling

Alan Kennedy alanmk at hotmail.com
Mon Sep 23 16:11:21 EDT 2002


storedmails at gmx.net wrote:
> I'm trying to program a web application with python. This tool has to
> provide diffrent languages. The user has to choose the language at the
> beginning. So i have to save the information which language the user
> chooses. I think this is a perfect example for using sessions.

The following is not related to sessions.

Are you aware that a users language preference is encoded in every
request that they make to a web server, through the "Accept-Language"
HTTP header?

Example formats for this header are (from my server logs)

Accept-Language: de
Accept-Language: en,fr-CA
Accept-Language: en-us, en;q=0.50
Accept-Language: en-us,de;q=0.7,fr;q=0.3
Accept-Language: en-us,fr-be;q=0.5
Accept-Language: en-us,zh-tw;q=0.5
Accept-Language: es-mx, en;q=0.5
Accept-Language: fr

So if you access the Accept-Language header (code below), and do some
simple string manipulation, you may be able to solve your problem
without sessions and without making the user explicitly select a
language (they've already done this in their browser).

Sample code-----------------------------------
#! /path/to/python

import os
import string

try:
    langpref = os.environ['HTTP_ACCEPT_LANGUAGE']
    langs = string.split(langpref, ',')
    firstpref = langs[0]
    secondpref = langs[1]
    thirdpref = langs[2]
    # Dealing with quality values (e.g. 'q=0.3') is left as an
exercise for the reader :-)
except:
    firstpref = 'en-ie'
-----------------------------------------------

Maybe might make your life a little simpler....

HTH,

Alan.



More information about the Python-list mailing list