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

26 statements  

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 

6 

7__all__ = ("Translations", "translations") 

8 

9 

10def _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 

33class 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 

47translations = Translations()