codekingpro/portable-devtools
114k
1import os2 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 cover11 locale_path = "/usr/share/locale"12 return locale_path13 14 15def get_builtin_gnu_translations(languages=None):16 """17 Get a gettext.GNUTranslations object pointing at the18 included translation files.19 20 :param languages:21 A list of languages to try, in order. If omitted or None, then22 gettext will try to use locale information from the environment.23 """24 import gettext25 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 use44 ugettext/ungettext.45 """46 47 def __init__(self, translations):48 self.translations = translations49 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 valid62 translations provider can be found.63 """64 65 def gettext(self, string):66 return string67 68 def ngettext(self, singular, plural, n):69 if n == 1:70 return singular71 72 return plural73 