Debian Codegraph @

django/utils/translation/trans_real.py

raw
1 """Translation helper functions."""
2
3 import functools
4 import gettext as gettext_module
5 import os
6 import re
7 import sys
8 import warnings
9
10 from asgiref.local import Local
11
12 from django.apps import apps
13 from django.conf import settings
14 from django.conf.locale import LANG_INFO
15 from django.core.exceptions import AppRegistryNotReady
16 from django.core.signals import setting_changed
17 from django.dispatch import receiver
18 from django.utils.regex_helper import _lazy_re_compile
19 from django.utils.safestring import SafeData, mark_safe
20
21 from . import to_language, to_locale
22
23 # Translations are cached in a dictionary for every language.
24 # The active translations are stored by threadid to make them thread local.
25 _translations = {}
26 _active = Local()
27
28 # The default translation is based on the settings file.
29 _default = None
30
31 # magic gettext number to separate context from message
32 CONTEXT_SEPARATOR = "\x04"
33
34 # Maximum number of characters that will be parsed from the Accept-Language
35 # header or cookie to prevent possible denial of service or memory exhaustion
36 # attacks. About 10x longer than the longest value shown on MDN’s
37 # Accept-Language page.
38 LANGUAGE_CODE_MAX_LENGTH = 500
39
40 # Format of Accept-Language header values. From RFC 9110 Sections 12.4.2 and
41 # 12.5.4, and RFC 5646 Section 2.1.
42 accept_language_re = _lazy_re_compile(
43 r"""
44 # "en", "en-au", "x-y-z", "es-419", "*"
45 ([A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*|\*)
46 # Optional "q=1.00", "q=0.8"
47 (?:\s*;\s*q=(0(?:\.[0-9]{,3})?|1(?:\.0{,3})?))?
48 # Multiple accepts per header.
49 (?:\s*,\s*|$)
50 """,
51 re.VERBOSE,
52 )
53
54 language_code_re = _lazy_re_compile(
55 r"^[a-z]{1,8}(?:-[a-z0-9]{1,8})*(?:@[a-z0-9]{1,20})?$", re.IGNORECASE
56 )
57
58 language_code_prefix_re = _lazy_re_compile(r"^/(\w+([@-]\w+){0,2})(/|$)")
59
60
61 @receiver(setting_changed)
62 def reset_cache(*, setting, **kwargs):
63 """
64 Reset global state when LANGUAGES setting has been changed, as some
65 languages should no longer be accepted.
66 """
67 if setting in ("LANGUAGES", "LANGUAGE_CODE"):
68 check_for_language.cache_clear()
69 get_languages.cache_clear()
70 get_supported_language_variant.cache_clear()
71
72
73 class TranslationCatalog:
74 """
75 Simulate a dict for DjangoTranslation._catalog so as multiple catalogs
76 with different plural equations are kept separate.
77 """
78
79 def __init__(self, trans=None):
80 self._catalogs = [trans._catalog.copy()] if trans else [{}]
81 self._plurals = [trans.plural] if trans else [lambda n: int(n != 1)]
82
83 def __getitem__(self, key):
84 for cat in self._catalogs:
85 try:
86 return cat[key]
87 except KeyError:
88 pass
89 raise KeyError(key)
90
91 def __setitem__(self, key, value):
92 self._catalogs[0][key] = value
93
94 def __contains__(self, key):
95 return any(key in cat for cat in self._catalogs)
96
97 def items(self):
98 for cat in self._catalogs:
99 yield from cat.items()
100
101 def keys(self):
102 for cat in self._catalogs:
103 yield from cat.keys()
104
105 def update(self, trans):
106 # Merge if plural function is the same as the top catalog, else prepend.
107 if trans.plural.__code__ == self._plurals[0]:
108 self._catalogs[0].update(trans._catalog)
109 else:
110 self._catalogs.insert(0, trans._catalog.copy())
111 self._plurals.insert(0, trans.plural)
112
113 def get(self, key, default=None):
114 missing = object()
115 for cat in self._catalogs:
116 result = cat.get(key, missing)
117 if result is not missing:
118 return result
119 return default
120
121 def plural(self, msgid, num):
122 for cat, plural in zip(self._catalogs, self._plurals):
123 tmsg = cat.get((msgid, plural(num)))
124 if tmsg is not None:
125 return tmsg
126 raise KeyError
127
128
129 class DjangoTranslation(gettext_module.GNUTranslations):
130 """
131 Set up the GNUTranslations context with regard to output charset.
132
133 This translation object will be constructed out of multiple GNUTranslations
134 objects by merging their catalogs. It will construct an object for the
135 requested language and add a fallback to the default language, if it's
136 different from the requested language.
137 """
138
139 domain = "django"
140
141 def __init__(self, language, domain=None, localedirs=None):
142 """Create a GNUTranslations() using many locale directories"""
143 gettext_module.GNUTranslations.__init__(self)
144 if domain is not None:
145 self.domain = domain
146
147 self.__language = language
148 self.__to_language = to_language(language)
149 self.__locale = to_locale(language)
150 self._catalog = None
151 # If a language doesn't have a catalog, use the Germanic default for
152 # pluralization: anything except one is pluralized.
153 self.plural = lambda n: int(n != 1)
154
155 if self.domain == "django":
156 if localedirs is not None:
157 # A module-level cache is used for caching 'django' translations
158 warnings.warn(
159 "localedirs is ignored when domain is 'django'.", RuntimeWarning
160 )
161 localedirs = None
162 self._init_translation_catalog()
163
164 if localedirs:
165 for localedir in localedirs:
166 translation = self._new_gnu_trans(localedir)
167 self.merge(translation)
168 else:
169 self._add_installed_apps_translations()
170
171 self._add_local_translations()
172 if (
173 self.__language == settings.LANGUAGE_CODE
174 and self.domain == "django"
175 and self._catalog is None
176 ):
177 # default lang should have at least one translation file available.
178 raise OSError(
179 "No translation files found for default language %s."
180 % settings.LANGUAGE_CODE
181 )
182 self._add_fallback(localedirs)
183 if self._catalog is None:
184 # No catalogs found for this language, set an empty catalog.
185 self._catalog = TranslationCatalog()
186
187 def __repr__(self):
188 return "<DjangoTranslation lang:%s>" % self.__language
189
190 def _new_gnu_trans(self, localedir, use_null_fallback=True):
191 """
192 Return a mergeable gettext.GNUTranslations instance.
193
194 A convenience wrapper. By default gettext uses 'fallback=False'.
195 Using param `use_null_fallback` to avoid confusion with any other
196 references to 'fallback'.
197 """
198 return gettext_module.translation(
199 domain=self.domain,
200 localedir=localedir,
201 languages=[self.__locale],
202 fallback=use_null_fallback,
203 )
204
205 def _init_translation_catalog(self):
206 """Create a base catalog using global django translations."""
207 settingsfile = sys.modules[settings.__module__].__file__
208 localedir = os.path.join(os.path.dirname(settingsfile), "locale")
209 translation = self._new_gnu_trans(localedir)
210 self.merge(translation)
211
212 def _add_installed_apps_translations(self):
213 """Merge translations from each installed app."""
214 try:
215 app_configs = reversed(apps.get_app_configs())
216 except AppRegistryNotReady:
217 raise AppRegistryNotReady(
218 "The translation infrastructure cannot be initialized before the "
219 "apps registry is ready. Check that you don't make non-lazy "
220 "gettext calls at import time."
221 )
222 for app_config in app_configs:
223 localedir = os.path.join(app_config.path, "locale")
224 if os.path.exists(localedir):
225 translation = self._new_gnu_trans(localedir)
226 self.merge(translation)
227
228 def _add_local_translations(self):
229 """Merge translations defined in LOCALE_PATHS."""
230 for localedir in reversed(settings.LOCALE_PATHS):
231 translation = self._new_gnu_trans(localedir)
232 self.merge(translation)
233
234 def _add_fallback(self, localedirs=None):
235 """Set the GNUTranslations() fallback with the default language."""
236 # Don't set a fallback for the default language or any English variant
237 # (as it's empty, so it'll ALWAYS fall back to the default language)
238 if self.__language == settings.LANGUAGE_CODE or self.__language.startswith(
239 "en"
240 ):
241 return
242 if self.domain == "django":
243 # Get from cache
244 default_translation = translation(settings.LANGUAGE_CODE)
245 else:
246 default_translation = DjangoTranslation(
247 settings.LANGUAGE_CODE, domain=self.domain, localedirs=localedirs
248 )
249 self.add_fallback(default_translation)
250
251 def merge(self, other):
252 """Merge another translation into this catalog."""
253 if not getattr(other, "_catalog", None):
254 return # NullTranslations() has no _catalog
255 if self._catalog is None:
256 # Take plural and _info from first catalog found (generally Django's).
257 self.plural = other.plural
258 self._info = other._info.copy()
259 self._catalog = TranslationCatalog(other)
260 else:
261 self._catalog.update(other)
262 if other._fallback:
263 self.add_fallback(other._fallback)
264
265 def language(self):
266 """Return the translation language."""
267 return self.__language
268
269 def to_language(self):
270 """Return the translation language name."""
271 return self.__to_language
272
273 def ngettext(self, msgid1, msgid2, n):
274 try:
275 tmsg = self._catalog.plural(msgid1, n)
276 except KeyError:
277 if self._fallback:
278 return self._fallback.ngettext(msgid1, msgid2, n)
279 if n == 1:
280 tmsg = msgid1
281 else:
282 tmsg = msgid2
283 return tmsg
284
285
286 def translation(language):
287 """
288 Return a translation object in the default 'django' domain.
289 """
290 if language not in _translations:
291 _translations[language] = DjangoTranslation(language)
292 return _translations[language]
293
294
295 def activate(language):
296 """
297 Fetch the translation object for a given language and install it as the
298 current translation object for the current thread.
299 """
300 if not language:
301 return
302 _active.value = translation(language)
303
304
305 def deactivate():
306 """
307 Uninstall the active translation object so that further _() calls resolve
308 to the default translation object.
309 """
310 if hasattr(_active, "value"):
311 del _active.value
312
313
314 def deactivate_all():
315 """
316 Make the active translation object a NullTranslations() instance. This is
317 useful when we want delayed translations to appear as the original string
318 for some reason.
319 """
320 _active.value = gettext_module.NullTranslations()
321 _active.value.to_language = lambda *args: None
322
323
324 def get_language():
325 """Return the currently selected language."""
326 t = getattr(_active, "value", None)
327 if t is not None:
328 try:
329 return t.to_language()
330 except AttributeError:
331 pass
332 # If we don't have a real translation object, assume it's the default language.
333 return settings.LANGUAGE_CODE
334
335
336 def get_language_bidi():
337 """
338 Return selected language's BiDi layout.
339
340 * False = left-to-right layout
341 * True = right-to-left layout
342 """
343 lang = get_language()
344 if lang is None:
345 return False
346 else:
347 base_lang = get_language().split("-")[0]
348 return base_lang in settings.LANGUAGES_BIDI
349
350
351 def catalog():
352 """
353 Return the current active catalog for further processing.
354 This can be used if you need to modify the catalog or want to access the
355 whole message catalog instead of just translating one string.
356 """
357 global _default
358
359 t = getattr(_active, "value", None)
360 if t is not None:
361 return t
362 if _default is None:
363 _default = translation(settings.LANGUAGE_CODE)
364 return _default
365
366
367 def gettext(message):
368 """
369 Translate the 'message' string. It uses the current thread to find the
370 translation object to use. If no current translation is activated, the
371 message will be run through the default translation object.
372 """
373 global _default
374
375 eol_message = message.replace("\r\n", "\n").replace("\r", "\n")
376
377 if eol_message:
378 _default = _default or translation(settings.LANGUAGE_CODE)
379 translation_object = getattr(_active, "value", _default)
380
381 result = translation_object.gettext(eol_message)
382 else:
383 # Return an empty value of the corresponding type if an empty message
384 # is given, instead of metadata, which is the default gettext behavior.
385 result = type(message)("")
386
387 if isinstance(message, SafeData):
388 return mark_safe(result)
389
390 return result
391
392
393 def pgettext(context, message):
394 msg_with_ctxt = "%s%s%s" % (context, CONTEXT_SEPARATOR, message)
395 result = gettext(msg_with_ctxt)
396 if CONTEXT_SEPARATOR in result:
397 # Translation not found
398 result = message
399 elif isinstance(message, SafeData):
400 result = mark_safe(result)
401 return result
402
403
404 def gettext_noop(message):
405 """
406 Mark strings for translation but don't translate them now. This can be
407 used to store strings in global variables that should stay in the base
408 language (because they might be used externally) and will be translated
409 later.
410 """
411 return message
412
413
414 def do_ntranslate(singular, plural, number, translation_function):
415 global _default
416
417 t = getattr(_active, "value", None)
418 if t is not None:
419 return getattr(t, translation_function)(singular, plural, number)
420 if _default is None:
421 _default = translation(settings.LANGUAGE_CODE)
422 return getattr(_default, translation_function)(singular, plural, number)
423
424
425 def ngettext(singular, plural, number):
426 """
427 Return a string of the translation of either the singular or plural,
428 based on the number.
429 """
430 return do_ntranslate(singular, plural, number, "ngettext")
431
432
433 def npgettext(context, singular, plural, number):
434 msgs_with_ctxt = (
435 "%s%s%s" % (context, CONTEXT_SEPARATOR, singular),
436 "%s%s%s" % (context, CONTEXT_SEPARATOR, plural),
437 number,
438 )
439 result = ngettext(*msgs_with_ctxt)
440 if CONTEXT_SEPARATOR in result:
441 # Translation not found
442 result = ngettext(singular, plural, number)
443 return result
444
445
446 def all_locale_paths():
447 """
448 Return a list of paths to user-provides languages files.
449 """
450 globalpath = os.path.join(
451 os.path.dirname(sys.modules[settings.__module__].__file__), "locale"
452 )
453 app_paths = []
454 for app_config in apps.get_app_configs():
455 locale_path = os.path.join(app_config.path, "locale")
456 if os.path.exists(locale_path):
457 app_paths.append(locale_path)
458 return [globalpath, *settings.LOCALE_PATHS, *app_paths]
459
460
461 @functools.lru_cache(maxsize=1000)
462 def check_for_language(lang_code):
463 """
464 Check whether there is a global language file for the given language
465 code. This is used to decide whether a user-provided language is
466 available.
467
468 lru_cache should have a maxsize to prevent from memory exhaustion attacks,
469 as the provided language codes are taken from the HTTP request. See also
470 <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
471 """
472 # First, a quick check to make sure lang_code is well-formed (#21458)
473 if lang_code is None or not language_code_re.search(lang_code):
474 return False
475 return any(
476 gettext_module.find("django", path, [to_locale(lang_code)]) is not None
477 for path in all_locale_paths()
478 )
479
480
481 @functools.lru_cache
482 def get_languages():
483 """
484 Cache of settings.LANGUAGES in a dictionary for easy lookups by key.
485 Convert keys to lowercase as they should be treated as case-insensitive.
486 """
487 return {key.lower(): value for key, value in dict(settings.LANGUAGES).items()}
488
489
490 @functools.lru_cache(maxsize=1000)
491 def get_supported_language_variant(lang_code, strict=False):
492 """
493 Return the language code that's listed in supported languages, possibly
494 selecting a more generic variant. Raise LookupError if nothing is found.
495
496 If `strict` is False (the default), look for a country-specific variant
497 when neither the language code nor its generic variant is found.
498
499 The language code is truncated to a maximum length to avoid potential
500 denial of service attacks.
501
502 lru_cache should have a maxsize to prevent from memory exhaustion attacks,
503 as the provided language codes are taken from the HTTP request. See also
504 <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
505 """
506 if lang_code:
507 # Truncate the language code to a maximum length to avoid potential
508 # denial of service attacks.
509 if len(lang_code) > LANGUAGE_CODE_MAX_LENGTH:
510 if (
511 not strict
512 and (index := lang_code.rfind("-", 0, LANGUAGE_CODE_MAX_LENGTH)) > 0
513 ):
514 # There is a generic variant under the maximum length accepted length.
515 lang_code = lang_code[:index]
516 else:
517 raise LookupError(lang_code)
518 # If 'zh-hant-tw' is not supported, try special fallback or subsequent
519 # language codes i.e. 'zh-hant' and 'zh'.
520 possible_lang_codes = [lang_code]
521 try:
522 possible_lang_codes.extend(LANG_INFO[lang_code]["fallback"])
523 except KeyError:
524 pass
525 i = None
526 while (i := lang_code.rfind("-", 0, i)) > -1:
527 possible_lang_codes.append(lang_code[:i])
528 generic_lang_code = possible_lang_codes[-1]
529 supported_lang_codes = get_languages()
530
531 for code in possible_lang_codes:
532 if code.lower() in supported_lang_codes and check_for_language(code):
533 return code
534 if not strict:
535 # if fr-fr is not supported, try fr-ca.
536 for supported_code in supported_lang_codes:
537 if supported_code.startswith(generic_lang_code + "-"):
538 return supported_code
539 raise LookupError(lang_code)
540
541
542 def get_language_from_path(path, strict=False):
543 """
544 Return the language code if there's a valid language code found in `path`.
545
546 If `strict` is False (the default), look for a country-specific variant
547 when neither the language code nor its generic variant is found.
548 """
549 regex_match = language_code_prefix_re.match(path)
550 if not regex_match:
551 return None
552 lang_code = regex_match[1]
553 try:
554 return get_supported_language_variant(lang_code, strict=strict)
555 except LookupError:
556 return None
557
558
559 def get_language_from_request(request, check_path=False):
560 """
561 Analyze the request to find what language the user wants the system to
562 show. Only languages listed in settings.LANGUAGES are taken into account.
563 If the user requests a sublanguage where we have a main language, we send
564 out the main language.
565
566 If check_path is True, the URL path prefix will be checked for a language
567 code, otherwise this is skipped for backwards compatibility.
568 """
569 if check_path:
570 lang_code = get_language_from_path(request.path_info)
571 if lang_code is not None:
572 return lang_code
573
574 lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
575 if (
576 lang_code is not None
577 and lang_code in get_languages()
578 and check_for_language(lang_code)
579 ):
580 return lang_code
581
582 try:
583 return get_supported_language_variant(lang_code)
584 except LookupError:
585 pass
586
587 accept = request.META.get("HTTP_ACCEPT_LANGUAGE", "")
588 for accept_lang, unused in parse_accept_lang_header(accept):
589 if accept_lang == "*":
590 break
591
592 if not language_code_re.search(accept_lang):
593 continue
594
595 try:
596 return get_supported_language_variant(accept_lang)
597 except LookupError:
598 continue
599
600 try:
601 return get_supported_language_variant(settings.LANGUAGE_CODE)
602 except LookupError:
603 return settings.LANGUAGE_CODE
604
605
606 @functools.lru_cache(maxsize=1000)
607 def _parse_accept_lang_header(lang_string):
608 """
609 Parse the lang_string, which is the body of an HTTP Accept-Language
610 header, and return a tuple of (lang, q-value), ordered by 'q' values.
611
612 Return an empty tuple if there are any format errors in lang_string.
613 """
614 result = []
615 pieces = accept_language_re.split(lang_string.lower())
616 if pieces[-1]:
617 return ()
618 for i in range(0, len(pieces) - 1, 3):
619 first, lang, priority = pieces[i : i + 3]
620 if first:
621 return ()
622 if priority:
623 priority = float(priority)
624 else:
625 priority = 1.0
626 result.append((lang, priority))
627 result.sort(key=lambda k: k[1], reverse=True)
628 return tuple(result)
629
630
631 def parse_accept_lang_header(lang_string):
632 """
633 Parse the value of the Accept-Language header up to a maximum length.
634
635 The value of the header is truncated to a maximum length to avoid potential
636 denial of service and memory exhaustion attacks. Excessive memory could be
637 used if the raw value is very large as it would be cached due to the use of
638 functools.lru_cache() to avoid repetitive parsing of common header values.
639 """
640 # If the header value doesn't exceed the maximum allowed length, parse it.
641 if len(lang_string) <= LANGUAGE_CODE_MAX_LENGTH:
642 return _parse_accept_lang_header(lang_string)
643
644 # If there is at least one comma in the value, parse up to the last comma
645 # before the max length, skipping any truncated parts at the end of the
646 # header value.
647 if (index := lang_string.rfind(",", 0, LANGUAGE_CODE_MAX_LENGTH)) > 0:
648 return _parse_accept_lang_header(lang_string[:index])
649
650 # Don't attempt to parse if there is only one language-range value which is
651 # longer than the maximum allowed length and so truncated.
652 return ()