[Zope3-checkins] CVS: Zope3/src/zope/i18n - negotiator.py:1.6.48.1

Sidnei da Silva sidnei at x3ng.com.br
Fri Feb 27 12:37:08 EST 2004


Update of /cvs-repository/Zope3/src/zope/i18n
In directory cvs.zope.org:/tmp/cvs-serv31559

Modified Files:
      Tag: runyaga-sip-branch
	negotiator.py 
Log Message:
Improve negotiator to cope with the fact that the list of language names coming from the translation service may not match the same formatting of the languages coming from the user preferences, so we normalize them before comparing.


=== Zope3/src/zope/i18n/negotiator.py 1.6 => 1.6.48.1 ===
--- Zope3/src/zope/i18n/negotiator.py:1.6	Fri Jun  6 15:29:09 2003
+++ Zope3/src/zope/i18n/negotiator.py	Fri Feb 27 12:36:37 2004
@@ -21,6 +21,21 @@
 from zope.component import getAdapter
 from zope.interface import implements
 
+def normalize_lang(lang):
+    lang = lang.strip().lower()
+    lang = lang.replace('_', '-')
+    lang = lang.replace(' ', '')
+    return lang
+
+def normalize_langs(langs):
+    # Make a mapping from normalized->original
+    # so we keep can match the normalized lang
+    # and return the original string.
+    n_langs = {}
+    for l in langs:
+        n_langs[normalize_lang(l)] = l
+    return n_langs
+
 class Negotiator:
 
     implements(INegotiator)
@@ -30,9 +45,10 @@
         userlangs = envadapter.getPreferredLanguages()
         # Prioritize on the user preferred languages.  Return the first user
         # preferred language that the object has available.
+        langs = normalize_langs(langs)
         for lang in userlangs:
             if lang in langs:
-                return lang
+                return langs.get(lang)
         return None
 
 




More information about the Zope3-Checkins mailing list