1- # $Id: client.py,v 1.216 2005-07-18 02:19:40 richard Exp $
1+ # $Id: client.py,v 1.217 2005-12-03 09:35:06 a1s Exp $
22
33"""WWW request handler (also used in the stand-alone server).
44"""
1515from roundup .cgi .exceptions import *
1616from roundup .cgi .form_parser import FormParser
1717from roundup .mailer import Mailer , MessageSendError
18+ from roundup .cgi import accept_language
1819
1920def initialiseSecurity (security ):
2021 '''Create some Permissions and Roles on the security object
@@ -196,13 +197,15 @@ def inner_main(self):
196197
197198 The most common requests are handled like so:
198199
199- 1. figure out who we are, defaulting to the "anonymous" user
200+ 1. look for charset and language preferences, set up user locale
201+ see determine_charset, determine_language
202+ 2. figure out who we are, defaulting to the "anonymous" user
200203 see determine_user
201- 2 . figure out what the request is for - the context
204+ 3 . figure out what the request is for - the context
202205 see determine_context
203- 3 . handle any requested action (item edit, search, ...)
206+ 4 . handle any requested action (item edit, search, ...)
204207 see handle_action
205- 4 . render a template, resulting in HTML output
208+ 5 . render a template, resulting in HTML output
206209
207210 In some situations, exceptions occur:
208211
@@ -225,6 +228,7 @@ def inner_main(self):
225228 self .error_message = []
226229 try :
227230 self .determine_charset ()
231+ self .determine_language ()
228232
229233 # make sure we're identified (even anonymously)
230234 self .determine_user ()
@@ -331,7 +335,6 @@ def determine_charset(self):
331335 If the charset is found, and differs from the storage charset,
332336 recode all form fields of type 'text/plain'
333337 """
334-
335338 # look for client charset
336339 charset_parameter = 0
337340 if self .form .has_key ('@charset' ):
@@ -386,9 +389,32 @@ def _decode_charref(matchobj):
386389 value = re_charref .sub (_decode_charref , value )
387390 field .value = encoder (value )[0 ]
388391
392+ def determine_language (self ):
393+ """Determine the language"""
394+ # look for language parameter
395+ # then for language cookie
396+ # last for the Accept-Language header
397+ if self .form .has_key ("@language" ):
398+ language = self .form ["@language" ].value
399+ if language .lower () == "none" :
400+ language = ""
401+ self .add_cookie ("roundup_language" , language )
402+ elif self .cookie .has_key ("roundup_language" ):
403+ language = self .cookie ["roundup_language" ].value
404+ elif self .instance .config ["WEB_USE_BROWSER_LANGUAGE" ]:
405+ hal = self .env ['HTTP_ACCEPT_LANGUAGE' ]
406+ language = accept_language .parse (hal )
407+ else :
408+ language = ""
409+
410+ self .language = language
411+ if language :
412+ self .setTranslator (TranslationService .get_translation (
413+ language ,
414+ tracker_home = self .instance .config ["TRACKER_HOME" ]))
415+
389416 def determine_user (self ):
390- ''' Determine who the user is
391- '''
417+ """Determine who the user is"""
392418 # determine the uid to use
393419 self .opendb ('admin' )
394420
@@ -460,13 +486,13 @@ def determine_user(self):
460486 self .opendb (self .user )
461487
462488 def opendb (self , username ):
463- ''' Open the database and set the current user.
489+ """ Open the database and set the current user.
464490
465491 Opens a database once. On subsequent calls only the user is set on
466492 the database object the instance.optimize is set. If we are in
467493 "Development Mode" (cf. roundup_server) then the database is always
468494 re-opened.
469- '''
495+ """
470496 # don't do anything if the db is open and the user has not changed
471497 if hasattr (self , 'db' ) and self .db .isCurrentUser (username ):
472498 return
0 commit comments