[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r14553: Patch from sjmurdoch with a little modification to the docum (check/trunk/cgi-bin)



Author: ioerror
Date: 2008-05-04 05:49:38 -0400 (Sun, 04 May 2008)
New Revision: 14553

Modified:
   check/trunk/cgi-bin/index.py
Log:
Patch from sjmurdoch with a little modification to the documentation and with support for fuzzy matching of locales.


Modified: check/trunk/cgi-bin/index.py
===================================================================
--- check/trunk/cgi-bin/index.py	2008-05-04 09:36:48 UTC (rev 14552)
+++ check/trunk/cgi-bin/index.py	2008-05-04 09:49:38 UTC (rev 14553)
@@ -76,31 +76,51 @@
     locale_descriptions = { 'en_US' : 'English', 'de' : 'Deutsch', 'es' : 'español','fa_IR' : 'fa_IR', 'ja': '(Nihogo)', 'pt_BR' : 'Português', 'pl' : 'polski', 'zh_CN' :'(Simplified Chinese)' }
     return locale_descriptions
 
-def parseLang(req):
-
-    user_supplied_lang = None
-    formSubmission=util.FieldStorage(req)
-    user_supplied_lang = formSubmission.getfirst("lang", None)
-
-    # These are the locales we're supporting currently
-    # If the user passes in a locale that matches it, great
+def getLocaleName(lang):
+    # If the user passes in a locale that matches what we support, good
     # However, anything that we don't support will result in us using a default locale
+    # 
+    # Important! lang is tainted data, don't forget this!
+    #
     
     default_locale = "en_US"
-    locale = default_locale
 
     # We'd really like these additional locales to be translated:
-    # locales = ( 'ar', 'fr', 'nl', 'pt-PT' )
+    # locales = ( 'ar', 'fr', 'nl', 'pt-PT', 'ru' )
 
+    # Make sure we have a valid language
+    if not lang:
+        return default_locale
+
     # This is to deal with Mozilla and Debian having different ideas about
     # what it means to be a locale
-    if user_supplied_lang:
-        user_supplied_lang = user_supplied_lang.replace("-", "_")
+    lang = lang.replace("-", "_")
 
+    # Do a case insensitive match
+    lang = lang.lower()
+
+    # Check for an exact match of language_country
     for item in getLocales().keys():
-        if item == user_supplied_lang:
-            locale = item 
+        if item.lower() == lang:
+            return item
 
+    # Check for just the language
+    lang = lang.split("_", 1)[0]
+    for item in getLocales().keys():
+        if item.lower().startswith(lang):
+            return item    
+
+    # Fall back to default
+    return default_locale
+    
+def parseLang(req):
+    user_supplied_lang = None
+    formSubmission=util.FieldStorage(req)
+    user_supplied_lang = formSubmission.getfirst("lang", None)
+
+    # Find the bast match for the requested language
+    locale = getLocaleName(user_supplied_lang)
+
     # i18n with Unicode!
     # Ensure you have properly installed TorCheck.{po,pot,mo} files
     lang = gettext.translation('TorCheck', languages=[locale])