def getLanguage(self): request = get_request() if request is not None: browser_langs = BrowserLanguages(request) langs = browser_langs.getPreferredLanguages() try: return langs[0] except IndexError: return None else: return None
def getLanguage(self): try: request = get_request() except zope.security.interfaces.NoInteraction: request = None if request is not None: browser_langs = BrowserLanguages(request) langs = browser_langs.getPreferredLanguages() try: return langs[0] except IndexError: return None else: return None
def getLanguage(self): try: request = get_request() except zope.security.interfaces.NoInteraction: request=None if request is not None: browser_langs = BrowserLanguages(request) langs = browser_langs.getPreferredLanguages() try: return langs[0] except IndexError: return None else: return None
def test_conflicting_adapters(self): request = TestRequest("da, en, pt") not_compatible_browser_languages = BrowserLanguages(request) browser_languages = self.factory(request) self.assertRaises(NotCompatibleAdapterError, browser_languages.setPreferredLanguages, ["ru", "en"])
def get_locale(request): """ return locale based on HTTP request header ACCEPT_LANGUAGES. We need languages to get locale, and the locale on the request object gets this wrong (removes territory part of locale). This does essentially what ZPublisher.HTTPRequest does to load a locale, but with a fixed (predictable, correct) adapter. zope.publisher.browser.BrowserLangauges is an adapter with fixed behavior to correctly get languages. Other adapters in Plone packages (e.g. PTSLanguages) may interfere with ZPublisher.HTTPRequest.locales loading territory, so we prefer a fixed adapter rather than an adapter looked-up by registration via IUserPreferredLanguages. """ locale = None languages = BrowserLanguages(request).getPreferredLanguages() for lang in languages: parts = (lang.split('-') + [None, None])[:3] try: locale = locales.getLocale(*parts) break except LoadLocaleError: pass return locale
def factory(self, request): return BrowserLanguages(request)