translation.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. import os
  3. BASE_PATH = os.path.abspath(os.path.dirname(__file__))
  4. ENV_LOCALE_DIR = 'PYTHON_VALIDATOR_LOCALE'
  5. ENV_LANGUAGES = 'PYTHON_VALIDATOR_LANGUAGES'
  6. DEFAULT_LOCALE_DIR = os.path.join(BASE_PATH, 'locale')
  7. DOMAIN = 'python-validator'
  8. domain = DOMAIN
  9. localedir = os.environ.get(ENV_LOCALE_DIR, DEFAULT_LOCALE_DIR)
  10. languages = os.environ.get(ENV_LANGUAGES)
  11. if languages is not None:
  12. try:
  13. languages = languages.split(',')
  14. except:
  15. languages = None
  16. try:
  17. # try using Django's translation tool
  18. from django.utils.translation import gettext, ngettext
  19. except ImportError as e:
  20. import gettext as _gettext
  21. translation = _gettext.translation(domain, localedir, languages=languages, fallback=True)
  22. def get_localedir():
  23. return localedir
  24. def gettext(s):
  25. return translation.gettext(s)
  26. def ngettext(singular, plural, n):
  27. return translation.ngettext(singular, plural, n)
  28. def lgettext(s):
  29. return translation.lgettext(s)
  30. def lngettext(singular, plural, n):
  31. return translation.lngettext(singular, plural, n)