Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/wtforms/i18n.py: 46%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:32 +0000

1import os 

2 

3 

4def messages_path(): 

5 """ 

6 Determine the path to the 'messages' directory as best possible. 

7 """ 

8 module_path = os.path.abspath(__file__) 

9 locale_path = os.path.join(os.path.dirname(module_path), "locale") 

10 if not os.path.exists(locale_path): # pragma: no cover 

11 locale_path = "/usr/share/locale" 

12 return locale_path 

13 

14 

15def get_builtin_gnu_translations(languages=None): 

16 """ 

17 Get a gettext.GNUTranslations object pointing at the 

18 included translation files. 

19 

20 :param languages: 

21 A list of languages to try, in order. If omitted or None, then 

22 gettext will try to use locale information from the environment. 

23 """ 

24 import gettext 

25 

26 return gettext.translation("wtforms", messages_path(), languages) 

27 

28 

29def get_translations(languages=None, getter=get_builtin_gnu_translations): 

30 """ 

31 Get a WTForms translation object which wraps a low-level translations object. 

32 

33 :param languages: 

34 A sequence of languages to try, in order. 

35 :param getter: 

36 A single-argument callable which returns a low-level translations object. 

37 """ 

38 return getter(languages) 

39 

40 

41class DefaultTranslations: 

42 """ 

43 A WTForms translations object to wrap translations objects which use 

44 ugettext/ungettext. 

45 """ 

46 

47 def __init__(self, translations): 

48 self.translations = translations 

49 

50 def gettext(self, string): 

51 return self.translations.ugettext(string) 

52 

53 def ngettext(self, singular, plural, n): 

54 return self.translations.ungettext(singular, plural, n) 

55 

56 

57class DummyTranslations: 

58 """ 

59 A translations object which simply returns unmodified strings. 

60 

61 This is typically used when translations are disabled or if no valid 

62 translations provider can be found. 

63 """ 

64 

65 def gettext(self, string): 

66 return string 

67 

68 def ngettext(self, singular, plural, n): 

69 if n == 1: 

70 return singular 

71 

72 return plural