Ticket #750: translation.2.py.diff
File translation.2.py.diff, 3.3 KB (added by , 19 years ago) |
---|
-
django/utils/translation.py
253 253 gettext_lazy = lazy(gettext, str) 254 254 ngettext_lazy = lazy(ngettext, str) 255 255 256 def check_for_language(lang _code):256 def check_for_language(lang, supported_langs, globalpath): 257 257 """ 258 258 Checks whether there is a global language file for the given language code. 259 259 This is used to decide whether a user-provided language is available. 260 260 """ 261 from django.conf import settings 262 globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') 263 if gettext_module.find('django', globalpath, [to_locale(lang_code)]) is not None: 264 return True 265 else: 266 return False 261 return ((lang in supported_langs or (lang.find('_')>=0 and lang.split('_')[0] in supported_langs)) 262 and gettext_module.find('django', globalpath, [to_locale(lang)]) is not None) 267 263 268 264 def get_language_from_request(request): 269 265 """ … … 273 269 from django.conf import settings 274 270 globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') 275 271 272 supported_langs = [a for (a,b) in settings.LANGUAGES] 273 # take into account language normalization 274 sublangs = filter(lambda a:a.find('_')>=0, supported_langs) 275 supported_langs += [a.split('_')[0] for a in sublangs] 276 276 277 if hasattr(request, 'session'): 277 278 lang_code = request.session.get('django_language', None) 278 if lang_code is not None and check_for_language(lang_code ):279 if lang_code is not None and check_for_language(lang_code, supported_langs, globalpath): 279 280 return lang_code 280 281 281 282 lang_code = request.COOKIES.get('django_language', None) 282 if lang_code is not None and check_for_language(lang_code ):283 if lang_code is not None and check_for_language(lang_code, supported_langs, globalpath): 283 284 return lang_code 284 285 285 286 accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None) … … 306 307 langs.sort(lambda a,b: -1*cmp(a[1], b[1])) 307 308 308 309 for lang, order in langs: 309 langfile = gettext_module.find('django', globalpath, [to_locale(lang)]) 310 if langfile: 311 # reconstruct the actual language from the language 312 # filename, because otherwise we might incorrectly 313 # report de_DE if we only have de available, but 314 # did find de_DE because of language normalization 315 lang = langfile[len(globalpath):].split(os.path.sep)[1] 316 _accepted[accept] = lang 317 return lang 310 if lang in supported_langs or (lang.find('_')>=0 and lang.split('_')[0] in supported_langs): 311 langfile = gettext_module.find('django', globalpath, [to_locale(lang)]) 312 if langfile: 313 # reconstruct the actual language from the language 314 # filename, because otherwise we might incorrectly 315 # report de_DE if we only have de available, but 316 # did find de_DE because of language normalization 317 lang = langfile[len(globalpath):].split(os.path.sep)[1] 318 _accepted[accept] = lang 319 return lang 318 320 319 321 return settings.LANGUAGE_CODE 320 322