def getLanguage(self, langs, env): envadapter = IUserPreferredLanguages(env) userlangs = envadapter.getPreferredLanguages() # Always add the template language to the available ones. This allows the # template language to be picked without the need for a message catalog # for every domain for it to be registered. langs = tuple(langs) + TEMPLATE_LANGUAGE global _cache, _bcache l_cache = _cache b_cache = _bcache notnormalized = [l for l in langs if l not in b_cache] if notnormalized: l_cache.update(normalize_langs(notnormalized)) b_cache.update(dict((v, k) for k, v in l_cache.items())) # Prioritize on the user preferred languages. Return the # first user preferred language that the object has available. #langs = normalize_langs(langs) for lang in userlangs: if lang in l_cache: return l_cache[lang] # If the user asked for a specific variation, but we don't # have it available we may serve the most generic one, # according to the spec (eg: user asks for ('en-us', # 'de'), but we don't have 'en-us', then 'en' is preferred # to 'de'). parts = lang.split('-') if len(parts) > 1 and parts[0] in l_cache: return l_cache.get(parts[0]) return None
def getAvailableLanguages(self): domain = TranslationContext().domain # keep in mind: this is the name of the .mo file as in default.mo translation_domain = queryUtility(ITranslationDomain, domain) if not translation_domain: return {} langs = translation_domain.getCatalogsInfo() self.purgeWeirdZopeI18nTestLang(langs) language_codes = normalize_langs(langs.keys()) languages = {} for code in language_codes: languages[code] = self.LANGUAGE_NAMES.get(code, code) return languages
def getLanguage(self, langs, request): if not langs: return None # get lang from cookie langs = normalize_langs(langs) self.purgeWeirdZopeI18nTestLang(langs) lang = self.getCookieLanguage(request) if lang and (lang in langs or self.getBaseLangName(lang) in langs): return lang # get lang from site default ?!? parents = getattr(request, 'PARENTS', None) app = parents[-1] if parents else None lang = getattr(app, 'default_language', None) if lang and lang in langs: return lang # get lang from browser settings return super(CustomNegotiator, self).getLanguage(langs, request)
def getLanguage(self, langs, env): envadapter = IUserPreferredLanguages(env) userlangs = envadapter.getPreferredLanguages() # Always add the template language to the available ones. This allows the # template language to be picked without the need for a message catalog # for every domain for it to be registered. langs = list(langs) + TEMPLATE_LANGUAGE # Prioritize on the user preferred languages. Return the # first user preferred language that the object has available. langs = normalize_langs(langs) for lang in userlangs: if lang in langs: return langs.get(lang) # If the user asked for a specific variation, but we don't # have it available we may serve the most generic one, # according to the spec (eg: user asks for ('en-us', # 'de'), but we don't have 'en-us', then 'en' is preferred # to 'de'). parts = lang.split('-') if len(parts) > 1 and parts[0] in langs: return langs.get(parts[0]) return None