[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Nuxeo-localizer] user prefered language
From: |
Olivier Nibart |
Subject: |
[Nuxeo-localizer] user prefered language |
Date: |
Fri, 15 Nov 2002 10:59:58 +0100 |
Hi all,
First of all, I hope I'm not boring you with the same-newbie-question, but I
didn't find any answer in the archives.
I want the first language shown on a web page to be the user prefered one.
But I found that it works well only if the langages list set at browser
level is set with only one language.
For example, with a default language set on Localizer as EN, if I set my
browser with FR as the first language, and EN as the second one, Localizer
will use EN.
I found that all the qualities of the languages in the AcceptLanguage
instance where set to 1.0, thus loosing the user prefs sorting.
I wanted also any complex language (ie fr-be or en-us) to be simplified (fr
or en), but this is specific as others may want to go to that level of
localisation.
So I made a script, added to the accept_method list, wich I give you for
comments or suggestions (I'm also new to python...) :
-----------------------------------
import string
request = container.REQUEST
user_languages = accept_language.parse(request['HTTP_ACCEPT_LANGUAGE'])
available_languages = container.get_available_languages()
default_languages = container.get_default_language()
# clean up language ID (ie fr-be or nl-be)
for lang in user_languages.keys():
if (len(lang) > 2) and (lang[2] == '-'):
clean_lang = lang[:2]
user_languages[clean_lang] = user_languages[lang]
del user_languages[lang]
# choose the lang to use
# - if q > 1.0 (via accept_path or accept_cookies), use that q
# - if q<= 1.0 use q found in HTTP_ACCEPT_LANGUAGE
language, quality = None, 0.0
for lang in available_languages:
q = accept_language.get_quality(lang)
if (q <= 1.0) and (user_languages.has_key(lang)):
q = user_languages[lang] if q > quality:
language, quality = lang, q
# if a lang as been determined, set it's q to 4.0 to be sure it'll be the
one used
if language != None:
accept_language.set(language, 4.0)
# set SELECTED_LANGUAGE, as we use it later
selected_language = container.get_selected_language()
request.set('SELECTED_LANGUAGE', selected_language)
-----------------------------------
Did I miss something, or was it the right to do it ?
Anyway, at least for non-US people, this product is **very** valuable,
thanks a lot David !
Olivier Nibart
Amtoys - Belgium
- [Nuxeo-localizer] user prefered language,
Olivier Nibart <=