def getPreferredLanguages(self): # If the default locale negotiater can get a value, # that means we had a parameter or one of the cookies # (because of the subscriber that gets us here). negotiated = default_locale_negotiator(self.request) if negotiated: return [negotiated] # Here is where we would check for something during traversal, # but we don't actually support that at this time because it # relies on implementation details # Is there a non-default user preference? Right now we know # what a default is due to implementation details above. We also # know for sure that we *have* a remote use, otherwise we wouldn't # be here remote_user = IPrincipal(self.request, None) remote_user_langs = IUserPreferredLanguages(remote_user) if remote_user_langs is not EnglishUserPreferredLanguagesImpl: return remote_user_langs.getPreferredLanguages() # pylint:disable=too-many-function-args # Ok, see what the HTTP request can come up with. Note that we're # going to the Zope interface so that we don't get into an infinite # loop browser_request = IBrowserRequest(self.request) browser_langs = IModifiableUserPreferredLanguages(browser_request) return browser_langs.getPreferredLanguages() # pylint:disable=too-many-function-args
def test_adapter(self): request = self.request browser_languages = IModifiableUserPreferredLanguages(request) self.failUnlessEqual(["en"], browser_languages.getPreferredLanguages()) ob = object() ob2 = lang(ob, request).traverse('ru', ()) self.failUnless(ob is ob2) self.failUnless(request.shifted) self.failUnlessEqual(["ru"], browser_languages.getPreferredLanguages())
def test_adapter(self): request = self.request browser_languages = IModifiableUserPreferredLanguages(request) self.assertEqual(["en"], browser_languages.getPreferredLanguages()) ob = object() ob2 = lang(ob, request).traverse('ru', ()) self.assertTrue(ob is ob2) self.assertTrue(request.shifted) self.assertEqual(["ru"], browser_languages.getPreferredLanguages())
def languageSelector(context, event): configlet = getUtility(ISiteLanguage) languages = IModifiableUserPreferredLanguages(event.request, None) if languages is not None: if not configlet.usebrowser: languages.setPreferredLanguages([configlet.language]) else: try: del languages._getLanguagesData()['overridden'] except KeyError: pass try: del languages._getLanguagesData()['cached'] except KeyError: pass event.request.setupLocale()
def traverse(self, name, ignored): self.request.shiftNameToApplication() languages = IModifiableUserPreferredLanguages(self.request) languages.setPreferredLanguages([name]) return self.context
def traverse(self, name, ignored): self.request.shiftNameToApplication() languages = IModifiableUserPreferredLanguages(self.request) languages.setPreferredLanguages([name]) # pylint:disable=too-many-function-args return self.context
def PyramidBrowserPreferredLanguages(request): # we implement IUserPreferredLanguages on the Pyramid object, but # return an IModifiableUserPreferredLanguages on the Zope object. # This prevents an infinite loop return IModifiableUserPreferredLanguages(PyramidZopeRequestProxy(request))