[Zope3-checkins] SVN: Zope3/trunk/src/zope/ let the determination of the request's locale by an adapter

Philipp von Weitershausen philikon at philikon.de
Fri Apr 1 09:18:15 EST 2005


Log message for revision 29779:
  let the determination of the request's locale by an adapter
  that is actually looked up instead of hard-wired. Problem is
  described in detail here:
    http://mail.zope.org/pipermail/zope3-dev/2005-March/014021.html
  

Changed:
  U   Zope3/trunk/src/zope/app/file/browser/file.py
  U   Zope3/trunk/src/zope/app/i18n/configure.zcml
  U   Zope3/trunk/src/zope/publisher/http.py
  U   Zope3/trunk/src/zope/publisher/tests/test_http.py

-=-
Modified: Zope3/trunk/src/zope/app/file/browser/file.py
===================================================================
--- Zope3/trunk/src/zope/app/file/browser/file.py	2005-04-01 13:57:59 UTC (rev 29778)
+++ Zope3/trunk/src/zope/app/file/browser/file.py	2005-04-01 14:18:14 UTC (rev 29779)
@@ -128,6 +128,18 @@
     >>> sio = StringIO.StringIO("some data")
     >>> sio.filename = 'abc.txt'
 
+    Before we instanciate the request, we need to make sure that the
+    ``IUserPreferredLanguages`` adapter exists, so that the request's
+    locale exists.  This is necessary because the ``update_object``
+    method uses the locale formatter for the status message:
+
+    >>> from zope.app.testing import ztapi
+    >>> from zope.publisher.browser import BrowserLanguages
+    >>> from zope.publisher.interfaces.http import IHTTPRequest
+    >>> from zope.i18n.interfaces import IUserPreferredLanguages
+    >>> ztapi.provideAdapter(IHTTPRequest, IUserPreferredLanguages,
+    ...                      BrowserLanguages)
+
     Let's make sure we can use the uploaded file name if one isn't
     specified by the user, and can use the content type when
     specified.

Modified: Zope3/trunk/src/zope/app/i18n/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/i18n/configure.zcml	2005-04-01 13:57:59 UTC (rev 29778)
+++ Zope3/trunk/src/zope/app/i18n/configure.zcml	2005-04-01 14:18:14 UTC (rev 29779)
@@ -8,7 +8,7 @@
       />
   
   <adapter factory="zope.publisher.browser.BrowserLanguages"
-      for="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.publisher.interfaces.http.IHTTPRequest"
       provides="zope.i18n.interfaces.IUserPreferredLanguages" 
       />
   

Modified: Zope3/trunk/src/zope/publisher/http.py
===================================================================
--- Zope3/trunk/src/zope/publisher/http.py	2005-04-01 13:57:59 UTC (rev 29778)
+++ Zope3/trunk/src/zope/publisher/http.py	2005-04-01 14:18:14 UTC (rev 29779)
@@ -33,6 +33,7 @@
 from zope.publisher.interfaces.http import IHTTPApplicationResponse
 from zope.publisher.interfaces.logginginfo import ILoggingInfo
 from zope.i18n.interfaces import IUserPreferredCharsets
+from zope.i18n.interfaces import IUserPreferredLanguages
 from zope.i18n.locales import locales, LoadLocaleError
 
 from zope.publisher.base import BaseRequest, BaseResponse
@@ -256,11 +257,13 @@
         self.__setupLocale()
 
     def __setupLocale(self):
-        # Import here to break import loops
-        from zope.publisher.browser import BrowserLanguages
+        self.response.setCharsetUsingRequest(self)
+        envadapter = IUserPreferredLanguages(self, None)
+        if envadapter is None:
+            self._locale = None
+            return
 
-        self.response.setCharsetUsingRequest(self)
-        langs = BrowserLanguages(self).getPreferredLanguages()
+        langs = envadapter.getPreferredLanguages()
         for httplang in langs:
             parts = (httplang.split('-') + [None, None])[:3]
             try:

Modified: Zope3/trunk/src/zope/publisher/tests/test_http.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/test_http.py	2005-04-01 13:57:59 UTC (rev 29778)
+++ Zope3/trunk/src/zope/publisher/tests/test_http.py	2005-04-01 14:18:14 UTC (rev 29779)
@@ -172,6 +172,14 @@
     def testRequestLocale(self):
         eq = self.assertEqual
         unless = self.failUnless
+
+        from zope.component import provideAdapter
+        from zope.publisher.browser import BrowserLanguages
+        from zope.publisher.interfaces.http import IHTTPRequest
+        from zope.i18n.interfaces import IUserPreferredLanguages
+        provideAdapter(BrowserLanguages, [IHTTPRequest],
+                       IUserPreferredLanguages)
+
         for httplang in ('it', 'it-ch', 'it-CH', 'IT', 'IT-CH', 'IT-ch'):
             req = self._createRequest({'HTTP_ACCEPT_LANGUAGE': httplang})
             locale = req.locale
@@ -211,6 +219,9 @@
         eq(locale.id.territory, None)
         eq(locale.id.variant, None)
 
+        from zope.component.testing import tearDown
+        tearDown()
+
     def testCookies(self):
         cookies = {
             'HTTP_COOKIE':



More information about the Zope3-Checkins mailing list