Debian Codegraph @

src/flask_wtf/i18n.py

raw symbols SCIP
1 from babel import support
2 from flask import current_app
3 from flask import request
4 from flask_babel import get_locale
5 from wtforms.i18n import messages_path
6
7 __all__ = ("Translations", "translations")
8
9
10 def _get_translations():
11 """Returns the correct gettext translations.
12 Copy from flask-babel with some modifications.
13 """
14
15 if not request:
16 return None
17
18 # babel should be in extensions for get_locale
19 if "babel" not in current_app.extensions:
20 return None
21
22 translations = getattr(request, "wtforms_translations", None)
23
24 if translations is None:
25 translations = support.Translations.load(
26 messages_path(), [get_locale()], domain="wtforms"
27 )
28 request.wtforms_translations = translations
29
30 return translations
31
32
33 class Translations:
34 def gettext(self, string):
35 t = _get_translations()
36 return string if t is None else t.ugettext(string)
37
38 def ngettext(self, singular, plural, n):
39 t = _get_translations()
40
41 if t is None:
42 return singular if n == 1 else plural
43
44 return t.ungettext(singular, plural, n)
45
46
47 translations = Translations()