PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/django/utils/translation/trans_real.py

https://github.com/andnils/django
Python | 739 lines | 667 code | 25 blank | 47 comment | 34 complexity | fab6644baadfd667b3cab1c5f22c8af4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """Translation helper functions."""
  2. from __future__ import unicode_literals
  3. from collections import OrderedDict
  4. import os
  5. import re
  6. import sys
  7. import gettext as gettext_module
  8. from threading import local
  9. import warnings
  10. from django.apps import apps
  11. from django.dispatch import receiver
  12. from django.test.signals import setting_changed
  13. from django.utils.deprecation import RemovedInDjango19Warning
  14. from django.utils.encoding import force_str, force_text
  15. from django.utils._os import upath
  16. from django.utils.safestring import mark_safe, SafeData
  17. from django.utils import six, lru_cache
  18. from django.utils.six import StringIO
  19. from django.utils.translation import TranslatorCommentWarning, trim_whitespace, LANGUAGE_SESSION_KEY
  20. # Translations are cached in a dictionary for every language.
  21. # The active translations are stored by threadid to make them thread local.
  22. _translations = {}
  23. _active = local()
  24. # The default translation is based on the settings file.
  25. _default = None
  26. # This is a cache of settings.LANGUAGES in an OrderedDict for easy lookups by
  27. # key
  28. _supported = None
  29. # magic gettext number to separate context from message
  30. CONTEXT_SEPARATOR = "\x04"
  31. # Format of Accept-Language header values. From RFC 2616, section 14.4 and 3.9
  32. # and RFC 3066, section 2.1
  33. accept_language_re = re.compile(r'''
  34. ([A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*|\*) # "en", "en-au", "x-y-z", "es-419", "*"
  35. (?:\s*;\s*q=(0(?:\.\d{,3})?|1(?:.0{,3})?))? # Optional "q=1.00", "q=0.8"
  36. (?:\s*,\s*|$) # Multiple accepts per header.
  37. ''', re.VERBOSE)
  38. language_code_re = re.compile(r'^[a-z]{1,8}(?:-[a-z0-9]{1,8})*$', re.IGNORECASE)
  39. language_code_prefix_re = re.compile(r'^/([\w-]+)(/|$)')
  40. # some browsers use deprecated locales. refs #18419
  41. _BROWSERS_DEPRECATED_LOCALES = {
  42. 'zh-cn': 'zh-hans',
  43. 'zh-tw': 'zh-hant',
  44. }
  45. _DJANGO_DEPRECATED_LOCALES = _BROWSERS_DEPRECATED_LOCALES
  46. @receiver(setting_changed)
  47. def reset_cache(**kwargs):
  48. """
  49. Reset global state when LANGUAGES setting has been changed, as some
  50. languages should no longer be accepted.
  51. """
  52. if kwargs['setting'] in ('LANGUAGES', 'LANGUAGE_CODE'):
  53. global _supported
  54. _supported = None
  55. check_for_language.cache_clear()
  56. get_supported_language_variant.cache_clear()
  57. def to_locale(language, to_lower=False):
  58. """
  59. Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
  60. True, the last component is lower-cased (en_us).
  61. """
  62. p = language.find('-')
  63. if p >= 0:
  64. if to_lower:
  65. return language[:p].lower() + '_' + language[p + 1:].lower()
  66. else:
  67. # Get correct locale for sr-latn
  68. if len(language[p + 1:]) > 2:
  69. return language[:p].lower() + '_' + language[p + 1].upper() + language[p + 2:].lower()
  70. return language[:p].lower() + '_' + language[p + 1:].upper()
  71. else:
  72. return language.lower()
  73. def to_language(locale):
  74. """Turns a locale name (en_US) into a language name (en-us)."""
  75. p = locale.find('_')
  76. if p >= 0:
  77. return locale[:p].lower() + '-' + locale[p + 1:].lower()
  78. else:
  79. return locale.lower()
  80. class DjangoTranslation(gettext_module.GNUTranslations):
  81. """
  82. This class sets up the GNUTranslations context with regard to output
  83. charset.
  84. """
  85. def __init__(self, *args, **kw):
  86. gettext_module.GNUTranslations.__init__(self, *args, **kw)
  87. self.set_output_charset('utf-8')
  88. self.__language = '??'
  89. def merge(self, other):
  90. self._catalog.update(other._catalog)
  91. def set_language(self, language):
  92. self.__language = language
  93. self.__to_language = to_language(language)
  94. def language(self):
  95. return self.__language
  96. def to_language(self):
  97. return self.__to_language
  98. def __repr__(self):
  99. return "<DjangoTranslation lang:%s>" % self.__language
  100. def translation(language):
  101. """
  102. Returns a translation object.
  103. This translation object will be constructed out of multiple GNUTranslations
  104. objects by merging their catalogs. It will construct a object for the
  105. requested language and add a fallback to the default language, if it's
  106. different from the requested language.
  107. """
  108. global _translations
  109. t = _translations.get(language, None)
  110. if t is not None:
  111. return t
  112. from django.conf import settings
  113. globalpath = os.path.join(os.path.dirname(upath(sys.modules[settings.__module__].__file__)), 'locale')
  114. def _fetch(lang, fallback=None):
  115. global _translations
  116. res = _translations.get(lang, None)
  117. if res is not None:
  118. return res
  119. loc = to_locale(lang)
  120. def _translation(path):
  121. try:
  122. t = gettext_module.translation('django', path, [loc], DjangoTranslation)
  123. t.set_language(lang)
  124. return t
  125. except IOError:
  126. return None
  127. res = _translation(globalpath)
  128. # We want to ensure that, for example, "en-gb" and "en-us" don't share
  129. # the same translation object (thus, merging en-us with a local update
  130. # doesn't affect en-gb), even though they will both use the core "en"
  131. # translation. So we have to subvert Python's internal gettext caching.
  132. base_lang = lambda x: x.split('-', 1)[0]
  133. if base_lang(lang) in [base_lang(trans) for trans in list(_translations)]:
  134. res._info = res._info.copy()
  135. res._catalog = res._catalog.copy()
  136. def _merge(path):
  137. t = _translation(path)
  138. if t is not None:
  139. if res is None:
  140. return t
  141. else:
  142. res.merge(t)
  143. return res
  144. for app_config in reversed(list(apps.get_app_configs())):
  145. apppath = os.path.join(app_config.path, 'locale')
  146. if os.path.isdir(apppath):
  147. res = _merge(apppath)
  148. for localepath in reversed(settings.LOCALE_PATHS):
  149. if os.path.isdir(localepath):
  150. res = _merge(localepath)
  151. if res is None:
  152. if fallback is not None:
  153. res = fallback
  154. else:
  155. return gettext_module.NullTranslations()
  156. _translations[lang] = res
  157. return res
  158. default_translation = _fetch(settings.LANGUAGE_CODE)
  159. current_translation = _fetch(language, fallback=default_translation)
  160. return current_translation
  161. def activate(language):
  162. """
  163. Fetches the translation object for a given language and installs it as the
  164. current translation object for the current thread.
  165. """
  166. if language in _DJANGO_DEPRECATED_LOCALES:
  167. msg = ("The use of the language code '%s' is deprecated. "
  168. "Please use the '%s' translation instead.")
  169. warnings.warn(msg % (language, _DJANGO_DEPRECATED_LOCALES[language]),
  170. RemovedInDjango19Warning, stacklevel=2)
  171. _active.value = translation(language)
  172. def deactivate():
  173. """
  174. Deinstalls the currently active translation object so that further _ calls
  175. will resolve against the default translation object, again.
  176. """
  177. if hasattr(_active, "value"):
  178. del _active.value
  179. def deactivate_all():
  180. """
  181. Makes the active translation object a NullTranslations() instance. This is
  182. useful when we want delayed translations to appear as the original string
  183. for some reason.
  184. """
  185. _active.value = gettext_module.NullTranslations()
  186. def get_language():
  187. """Returns the currently selected language."""
  188. t = getattr(_active, "value", None)
  189. if t is not None:
  190. try:
  191. return t.to_language()
  192. except AttributeError:
  193. pass
  194. # If we don't have a real translation object, assume it's the default language.
  195. from django.conf import settings
  196. return settings.LANGUAGE_CODE
  197. def get_language_bidi():
  198. """
  199. Returns selected language's BiDi layout.
  200. * False = left-to-right layout
  201. * True = right-to-left layout
  202. """
  203. from django.conf import settings
  204. base_lang = get_language().split('-')[0]
  205. return base_lang in settings.LANGUAGES_BIDI
  206. def catalog():
  207. """
  208. Returns the current active catalog for further processing.
  209. This can be used if you need to modify the catalog or want to access the
  210. whole message catalog instead of just translating one string.
  211. """
  212. global _default
  213. t = getattr(_active, "value", None)
  214. if t is not None:
  215. return t
  216. if _default is None:
  217. from django.conf import settings
  218. _default = translation(settings.LANGUAGE_CODE)
  219. return _default
  220. def do_translate(message, translation_function):
  221. """
  222. Translates 'message' using the given 'translation_function' name -- which
  223. will be either gettext or ugettext. It uses the current thread to find the
  224. translation object to use. If no current translation is activated, the
  225. message will be run through the default translation object.
  226. """
  227. global _default
  228. # str() is allowing a bytestring message to remain bytestring on Python 2
  229. eol_message = message.replace(str('\r\n'), str('\n')).replace(str('\r'), str('\n'))
  230. t = getattr(_active, "value", None)
  231. if t is not None:
  232. result = getattr(t, translation_function)(eol_message)
  233. else:
  234. if _default is None:
  235. from django.conf import settings
  236. _default = translation(settings.LANGUAGE_CODE)
  237. result = getattr(_default, translation_function)(eol_message)
  238. if isinstance(message, SafeData):
  239. return mark_safe(result)
  240. return result
  241. def gettext(message):
  242. """
  243. Returns a string of the translation of the message.
  244. Returns a string on Python 3 and an UTF-8-encoded bytestring on Python 2.
  245. """
  246. return do_translate(message, 'gettext')
  247. if six.PY3:
  248. ugettext = gettext
  249. else:
  250. def ugettext(message):
  251. return do_translate(message, 'ugettext')
  252. def pgettext(context, message):
  253. msg_with_ctxt = "%s%s%s" % (context, CONTEXT_SEPARATOR, message)
  254. result = ugettext(msg_with_ctxt)
  255. if CONTEXT_SEPARATOR in result:
  256. # Translation not found
  257. result = message
  258. return result
  259. def gettext_noop(message):
  260. """
  261. Marks strings for translation but doesn't translate them now. This can be
  262. used to store strings in global variables that should stay in the base
  263. language (because they might be used externally) and will be translated
  264. later.
  265. """
  266. return message
  267. def do_ntranslate(singular, plural, number, translation_function):
  268. global _default
  269. t = getattr(_active, "value", None)
  270. if t is not None:
  271. return getattr(t, translation_function)(singular, plural, number)
  272. if _default is None:
  273. from django.conf import settings
  274. _default = translation(settings.LANGUAGE_CODE)
  275. return getattr(_default, translation_function)(singular, plural, number)
  276. def ngettext(singular, plural, number):
  277. """
  278. Returns a string of the translation of either the singular or plural,
  279. based on the number.
  280. Returns a string on Python 3 and an UTF-8-encoded bytestring on Python 2.
  281. """
  282. return do_ntranslate(singular, plural, number, 'ngettext')
  283. if six.PY3:
  284. ungettext = ngettext
  285. else:
  286. def ungettext(singular, plural, number):
  287. """
  288. Returns a unicode strings of the translation of either the singular or
  289. plural, based on the number.
  290. """
  291. return do_ntranslate(singular, plural, number, 'ungettext')
  292. def npgettext(context, singular, plural, number):
  293. msgs_with_ctxt = ("%s%s%s" % (context, CONTEXT_SEPARATOR, singular),
  294. "%s%s%s" % (context, CONTEXT_SEPARATOR, plural),
  295. number)
  296. result = ungettext(*msgs_with_ctxt)
  297. if CONTEXT_SEPARATOR in result:
  298. # Translation not found
  299. result = ungettext(singular, plural, number)
  300. return result
  301. def all_locale_paths():
  302. """
  303. Returns a list of paths to user-provides languages files.
  304. """
  305. from django.conf import settings
  306. globalpath = os.path.join(
  307. os.path.dirname(upath(sys.modules[settings.__module__].__file__)), 'locale')
  308. return [globalpath] + list(settings.LOCALE_PATHS)
  309. @lru_cache.lru_cache()
  310. def check_for_language(lang_code):
  311. """
  312. Checks whether there is a global language file for the given language
  313. code. This is used to decide whether a user-provided language is
  314. available.
  315. """
  316. # First, a quick check to make sure lang_code is well-formed (#21458)
  317. if not language_code_re.search(lang_code):
  318. return False
  319. for path in all_locale_paths():
  320. if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:
  321. return True
  322. return False
  323. @lru_cache.lru_cache(maxsize=1000)
  324. def get_supported_language_variant(lang_code, strict=False):
  325. """
  326. Returns the language-code that's listed in supported languages, possibly
  327. selecting a more generic variant. Raises LookupError if nothing found.
  328. If `strict` is False (the default), the function will look for an alternative
  329. country-specific variant when the currently checked is not found.
  330. lru_cache should have a maxsize to prevent from memory exhaustion attacks,
  331. as the provided language codes are taken from the HTTP request. See also
  332. <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
  333. """
  334. global _supported
  335. if _supported is None:
  336. from django.conf import settings
  337. _supported = OrderedDict(settings.LANGUAGES)
  338. if lang_code:
  339. # some browsers use deprecated language codes -- #18419
  340. replacement = _BROWSERS_DEPRECATED_LOCALES.get(lang_code)
  341. if lang_code not in _supported and replacement in _supported:
  342. return replacement
  343. # if fr-ca is not supported, try fr.
  344. generic_lang_code = lang_code.split('-')[0]
  345. for code in (lang_code, generic_lang_code):
  346. if code in _supported and check_for_language(code):
  347. return code
  348. if not strict:
  349. # if fr-fr is not supported, try fr-ca.
  350. for supported_code in _supported:
  351. if supported_code.startswith(generic_lang_code + '-'):
  352. return supported_code
  353. raise LookupError(lang_code)
  354. def get_language_from_path(path, strict=False):
  355. """
  356. Returns the language-code if there is a valid language-code
  357. found in the `path`.
  358. If `strict` is False (the default), the function will look for an alternative
  359. country-specific variant when the currently checked is not found.
  360. """
  361. regex_match = language_code_prefix_re.match(path)
  362. if not regex_match:
  363. return None
  364. lang_code = regex_match.group(1)
  365. try:
  366. return get_supported_language_variant(lang_code, strict=strict)
  367. except LookupError:
  368. return None
  369. def get_language_from_request(request, check_path=False):
  370. """
  371. Analyzes the request to find what language the user wants the system to
  372. show. Only languages listed in settings.LANGUAGES are taken into account.
  373. If the user requests a sublanguage where we have a main language, we send
  374. out the main language.
  375. If check_path is True, the URL path prefix will be checked for a language
  376. code, otherwise this is skipped for backwards compatibility.
  377. """
  378. from django.conf import settings
  379. global _supported
  380. if _supported is None:
  381. _supported = OrderedDict(settings.LANGUAGES)
  382. if check_path:
  383. lang_code = get_language_from_path(request.path_info)
  384. if lang_code is not None:
  385. return lang_code
  386. if hasattr(request, 'session'):
  387. lang_code = request.session.get(LANGUAGE_SESSION_KEY)
  388. if lang_code in _supported and lang_code is not None and check_for_language(lang_code):
  389. return lang_code
  390. lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
  391. try:
  392. return get_supported_language_variant(lang_code)
  393. except LookupError:
  394. pass
  395. accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
  396. for accept_lang, unused in parse_accept_lang_header(accept):
  397. if accept_lang == '*':
  398. break
  399. if not language_code_re.search(accept_lang):
  400. continue
  401. try:
  402. return get_supported_language_variant(accept_lang)
  403. except LookupError:
  404. continue
  405. try:
  406. return get_supported_language_variant(settings.LANGUAGE_CODE)
  407. except LookupError:
  408. return settings.LANGUAGE_CODE
  409. dot_re = re.compile(r'\S')
  410. def blankout(src, char):
  411. """
  412. Changes every non-whitespace character to the given char.
  413. Used in the templatize function.
  414. """
  415. return dot_re.sub(char, src)
  416. context_re = re.compile(r"""^\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?'))\s*""")
  417. inline_re = re.compile(r"""^\s*trans\s+((?:"[^"]*?")|(?:'[^']*?'))(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?\s*""")
  418. block_re = re.compile(r"""^\s*blocktrans(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?(?:\s+|$)""")
  419. endblock_re = re.compile(r"""^\s*endblocktrans$""")
  420. plural_re = re.compile(r"""^\s*plural$""")
  421. constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""")
  422. one_percent_re = re.compile(r"""(?<!%)%(?!%)""")
  423. def templatize(src, origin=None):
  424. """
  425. Turns a Django template into something that is understood by xgettext. It
  426. does so by translating the Django translation tags into standard gettext
  427. function invocations.
  428. """
  429. from django.conf import settings
  430. from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK,
  431. TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK)
  432. src = force_text(src, settings.FILE_CHARSET)
  433. out = StringIO()
  434. message_context = None
  435. intrans = False
  436. inplural = False
  437. trimmed = False
  438. singular = []
  439. plural = []
  440. incomment = False
  441. comment = []
  442. lineno_comment_map = {}
  443. comment_lineno_cache = None
  444. def join_tokens(tokens, trim=False):
  445. message = ''.join(tokens)
  446. if trim:
  447. message = trim_whitespace(message)
  448. return message
  449. for t in Lexer(src, origin).tokenize():
  450. if incomment:
  451. if t.token_type == TOKEN_BLOCK and t.contents == 'endcomment':
  452. content = ''.join(comment)
  453. translators_comment_start = None
  454. for lineno, line in enumerate(content.splitlines(True)):
  455. if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
  456. translators_comment_start = lineno
  457. for lineno, line in enumerate(content.splitlines(True)):
  458. if translators_comment_start is not None and lineno >= translators_comment_start:
  459. out.write(' # %s' % line)
  460. else:
  461. out.write(' #\n')
  462. incomment = False
  463. comment = []
  464. else:
  465. comment.append(t.contents)
  466. elif intrans:
  467. if t.token_type == TOKEN_BLOCK:
  468. endbmatch = endblock_re.match(t.contents)
  469. pluralmatch = plural_re.match(t.contents)
  470. if endbmatch:
  471. if inplural:
  472. if message_context:
  473. out.write(' npgettext(%r, %r, %r,count) ' % (
  474. message_context,
  475. join_tokens(singular, trimmed),
  476. join_tokens(plural, trimmed)))
  477. else:
  478. out.write(' ngettext(%r, %r, count) ' % (
  479. join_tokens(singular, trimmed),
  480. join_tokens(plural, trimmed)))
  481. for part in singular:
  482. out.write(blankout(part, 'S'))
  483. for part in plural:
  484. out.write(blankout(part, 'P'))
  485. else:
  486. if message_context:
  487. out.write(' pgettext(%r, %r) ' % (
  488. message_context,
  489. join_tokens(singular, trimmed)))
  490. else:
  491. out.write(' gettext(%r) ' % join_tokens(singular,
  492. trimmed))
  493. for part in singular:
  494. out.write(blankout(part, 'S'))
  495. message_context = None
  496. intrans = False
  497. inplural = False
  498. singular = []
  499. plural = []
  500. elif pluralmatch:
  501. inplural = True
  502. else:
  503. filemsg = ''
  504. if origin:
  505. filemsg = 'file %s, ' % origin
  506. raise SyntaxError("Translation blocks must not include other block tags: %s (%sline %d)" % (t.contents, filemsg, t.lineno))
  507. elif t.token_type == TOKEN_VAR:
  508. if inplural:
  509. plural.append('%%(%s)s' % t.contents)
  510. else:
  511. singular.append('%%(%s)s' % t.contents)
  512. elif t.token_type == TOKEN_TEXT:
  513. contents = one_percent_re.sub('%%', t.contents)
  514. if inplural:
  515. plural.append(contents)
  516. else:
  517. singular.append(contents)
  518. else:
  519. # Handle comment tokens (`{# ... #}`) plus other constructs on
  520. # the same line:
  521. if comment_lineno_cache is not None:
  522. cur_lineno = t.lineno + t.contents.count('\n')
  523. if comment_lineno_cache == cur_lineno:
  524. if t.token_type != TOKEN_COMMENT:
  525. for c in lineno_comment_map[comment_lineno_cache]:
  526. filemsg = ''
  527. if origin:
  528. filemsg = 'file %s, ' % origin
  529. warn_msg = ("The translator-targeted comment '%s' "
  530. "(%sline %d) was ignored, because it wasn't the last item "
  531. "on the line.") % (c, filemsg, comment_lineno_cache)
  532. warnings.warn(warn_msg, TranslatorCommentWarning)
  533. lineno_comment_map[comment_lineno_cache] = []
  534. else:
  535. out.write('# %s' % ' | '.join(lineno_comment_map[comment_lineno_cache]))
  536. comment_lineno_cache = None
  537. if t.token_type == TOKEN_BLOCK:
  538. imatch = inline_re.match(t.contents)
  539. bmatch = block_re.match(t.contents)
  540. cmatches = constant_re.findall(t.contents)
  541. if imatch:
  542. g = imatch.group(1)
  543. if g[0] == '"':
  544. g = g.strip('"')
  545. elif g[0] == "'":
  546. g = g.strip("'")
  547. g = one_percent_re.sub('%%', g)
  548. if imatch.group(2):
  549. # A context is provided
  550. context_match = context_re.match(imatch.group(2))
  551. message_context = context_match.group(1)
  552. if message_context[0] == '"':
  553. message_context = message_context.strip('"')
  554. elif message_context[0] == "'":
  555. message_context = message_context.strip("'")
  556. out.write(' pgettext(%r, %r) ' % (message_context, g))
  557. message_context = None
  558. else:
  559. out.write(' gettext(%r) ' % g)
  560. elif bmatch:
  561. for fmatch in constant_re.findall(t.contents):
  562. out.write(' _(%s) ' % fmatch)
  563. if bmatch.group(1):
  564. # A context is provided
  565. context_match = context_re.match(bmatch.group(1))
  566. message_context = context_match.group(1)
  567. if message_context[0] == '"':
  568. message_context = message_context.strip('"')
  569. elif message_context[0] == "'":
  570. message_context = message_context.strip("'")
  571. intrans = True
  572. inplural = False
  573. trimmed = 'trimmed' in t.split_contents()
  574. singular = []
  575. plural = []
  576. elif cmatches:
  577. for cmatch in cmatches:
  578. out.write(' _(%s) ' % cmatch)
  579. elif t.contents == 'comment':
  580. incomment = True
  581. else:
  582. out.write(blankout(t.contents, 'B'))
  583. elif t.token_type == TOKEN_VAR:
  584. parts = t.contents.split('|')
  585. cmatch = constant_re.match(parts[0])
  586. if cmatch:
  587. out.write(' _(%s) ' % cmatch.group(1))
  588. for p in parts[1:]:
  589. if p.find(':_(') >= 0:
  590. out.write(' %s ' % p.split(':', 1)[1])
  591. else:
  592. out.write(blankout(p, 'F'))
  593. elif t.token_type == TOKEN_COMMENT:
  594. if t.contents.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
  595. lineno_comment_map.setdefault(t.lineno,
  596. []).append(t.contents)
  597. comment_lineno_cache = t.lineno
  598. else:
  599. out.write(blankout(t.contents, 'X'))
  600. return force_str(out.getvalue())
  601. def parse_accept_lang_header(lang_string):
  602. """
  603. Parses the lang_string, which is the body of an HTTP Accept-Language
  604. header, and returns a list of (lang, q-value), ordered by 'q' values.
  605. Any format errors in lang_string results in an empty list being returned.
  606. """
  607. result = []
  608. pieces = accept_language_re.split(lang_string.lower())
  609. if pieces[-1]:
  610. return []
  611. for i in range(0, len(pieces) - 1, 3):
  612. first, lang, priority = pieces[i:i + 3]
  613. if first:
  614. return []
  615. if priority:
  616. try:
  617. priority = float(priority)
  618. except ValueError:
  619. return []
  620. if not priority: # if priority is 0.0 at this point make it 1.0
  621. priority = 1.0
  622. result.append((lang, priority))
  623. result.sort(key=lambda k: k[1], reverse=True)
  624. return result