[web] only set "Vary: Accept-Language" when we translate something

Some requests never go through gettext, so their result doesn't depend on the selected language.

authorJulien Cristau <julien.cristau@logilab.fr>
changeset9229a6e0d714
branchdefault
phasedraft
hiddenno
parent revision#c5dcb7911dc7 fix: skip a wdoc test when doctuils is not available
child revision<not specified>
files modified by this revision
cubicweb/web/request.py
cubicweb/web/views/urlpublishing.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1448025959 -3600
# Fri Nov 20 14:25:59 2015 +0100
# Node ID 9229a6e0d714f2daf3804f8e7d6eeb83ff17d92d
# Parent c5dcb7911dc7f2a333594045306c0fd498d182dd
[web] only set "Vary: Accept-Language" when we translate something

Some requests never go through gettext, so their result doesn't depend
on the selected language.

diff --git a/cubicweb/web/request.py b/cubicweb/web/request.py
@@ -836,10 +836,26 @@
1          for lang in self.header_accept_language():
2              if lang in self.translations:
3                  return lang
4          return None
5 
6 +    def set_language_from_req(self, lang):
7 +        # The chosen language comes from the request's Accept-Language header:
8 +        # if we ever translate something, set Vary to indicate that our response
9 +        # depends on that request header.
10 +        self.set_language(lang)
11 +
12 +        def wrap(func, name):
13 +            def tr(*args, **kwargs):
14 +                self.headers_out.addHeader('Vary', 'Accept-Language')
15 +                setattr(self, name, func)
16 +                return func(*args, **kwargs)
17 +            return tr
18 +        self._ = wrap(self._, '_')
19 +        self.__ = wrap(self.__, '__')
20 +        self.pgettext = wrap(self.pgettext, 'pgettext')
21 +
22 
23  def _cnx_func(name):
24      def proxy(req, *args, **kwargs):
25          return getattr(req.cnx, name)(*args, **kwargs)
26      return proxy
diff --git a/cubicweb/web/views/urlpublishing.py b/cubicweb/web/views/urlpublishing.py
@@ -127,11 +127,14 @@
27                  parts = (self.default_method,)
28          elif language_mode in ('http-negotiation', 'url-prefix'):
29              # negotiated language
30              lang = req.negotiated_language()
31              if lang:
32 -                req.set_language(lang)
33 +                if language_mode == 'http-negotiation':
34 +                    req.set_language_from_req(lang)
35 +                else:
36 +                    req.set_language(lang)
37          if req.form.get('rql'):
38              if parts[0] in self.vreg['controllers']:
39                  return parts[0], None
40              return 'view', None
41          for evaluator in self.evaluators: