Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/flask_wtf/i18n.py: 69%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from babel import support
2from flask import current_app
3from flask import request
4from flask_babel import get_locale
5from wtforms.i18n import messages_path
7__all__ = ("Translations", "translations")
10def _get_translations():
11 """Returns the correct gettext translations.
12 Copy from flask-babel with some modifications.
13 """
15 if not request:
16 return None
18 # babel should be in extensions for get_locale
19 if "babel" not in current_app.extensions:
20 return None
22 translations = getattr(request, "wtforms_translations", None)
24 if translations is None:
25 translations = support.Translations.load(
26 messages_path(), [get_locale()], domain="wtforms"
27 )
28 request.wtforms_translations = translations
30 return translations
33class Translations:
34 def gettext(self, string):
35 t = _get_translations()
36 return string if t is None else t.ugettext(string)
38 def ngettext(self, singular, plural, n):
39 t = _get_translations()
41 if t is None:
42 return singular if n == 1 else plural
44 return t.ungettext(singular, plural, n)
47translations = Translations()