[Zope3-checkins] CVS: Zope3/src/zope/publisher - http.py:1.14

Barry Warsaw barry@wooz.org
Tue, 25 Mar 2003 09:32:20 -0500


Update of /cvs-repository/Zope3/src/zope/publisher
In directory cvs.zope.org:/tmp/cvs-serv19648/src/zope/publisher

Modified Files:
	http.py 
Log Message:
HTTPRequest:

    Add _locale to slots.

    __init__(): Add code to suck the preferred languages out of the
    http headers, and use that information to create a locale, which
    is attached to the request object.

    getLocale(): Method to return the _locale -- Note this will change
    soon.  locale should be a property.


=== Zope3/src/zope/publisher/http.py 1.13 => 1.14 ===
--- Zope3/src/zope/publisher/http.py:1.13	Sun Mar  2 13:09:02 2003
+++ Zope3/src/zope/publisher/http.py	Tue Mar 25 09:32:20 2003
@@ -32,6 +32,7 @@
 from zope.publisher.interfaces.http import IHTTPApplicationResponse
 from zope.i18n.interfaces import IUserPreferredCharsets
 from zope.i18n.interfaces import IUserPreferredCharsets
+from zope.i18n.locales import locales, LoadLocaleError
 
 from zope.component import queryAdapter
 from zope.exceptions import NotFoundError
@@ -281,11 +282,14 @@
         '_orig_env',      # The original environment
         '_endswithslash', # Does the given path end with /
         'method',         # The upper-cased request method (REQUEST_METHOD)
+        '_locale',        # The locale for the request
         )
 
     retry_max_count = 3    # How many times we're willing to retry
 
     def __init__(self, body_instream, outstream, environ, response=None):
+        # Import here to break import loops
+        from zope.publisher.browser import BrowserLanguages
 
         super(HTTPRequest, self).__init__(
             body_instream, outstream, environ, response)
@@ -308,7 +312,29 @@
         self.__setupURLBase()
 
         self.response.setCharsetUsingRequest(self)
+        langs = BrowserLanguages(self).getPreferredLanguages()
+        language = country = variant = None
+        for httplang in langs:
+            parts = httplang.split('-')
+            if parts:
+                language = parts.pop(0)
+            if parts:
+                country = parts.pop(0)
+            if parts:
+                variant = parts.pop(0)
+            try:
+                self._locale = locales.getLocale(language, country, variant)
+                return
+            except LoadLocaleError:
+                # Just try the next combination
+                pass
+        else:
+            # No combination gave us an existing locale, so use the default,
+            # which is guaranteed to exist
+            self._locale = locales.getLocale(None, None, None)
 
+    def getLocale(self):
+        return self._locale
 
     def __setupURLBase(self):
 
@@ -421,7 +447,6 @@
                 time.sleep(random.uniform(0, 2**(count)))
             return True
 
-
     def retry(self):
         'See IPublisherRequest'
         count = getattr(self, '_retry_count', 0)
@@ -437,7 +462,6 @@
         request.setPublication(self.publication)
         request._retry_count = self._retry_count
         return request
-
 
     def traverse(self, object):
         'See IPublisherRequest'