예제 #1
0
    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
예제 #2
0
    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())
예제 #3
0
    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())
예제 #4
0
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()
예제 #5
0
 def traverse(self, name, ignored):
     self.request.shiftNameToApplication()
     languages = IModifiableUserPreferredLanguages(self.request)
     languages.setPreferredLanguages([name])
     return self.context
예제 #6
0
 def traverse(self, name, ignored):
     self.request.shiftNameToApplication()
     languages = IModifiableUserPreferredLanguages(self.request)
     languages.setPreferredLanguages([name])
     return self.context
예제 #7
0
 def traverse(self, name, ignored):
     self.request.shiftNameToApplication()
     languages = IModifiableUserPreferredLanguages(self.request)
     languages.setPreferredLanguages([name])  # pylint:disable=too-many-function-args
     return self.context
예제 #8
0
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))