utils.py 782 B

123456789101112131415161718192021222324252627282930
  1. # pylint: disable=C,R,W
  2. import json
  3. import os
  4. # Global caching for JSON language packs
  5. ALL_LANGUAGE_PACKS = {'en': {}}
  6. DIR = os.path.dirname(os.path.abspath(__file__))
  7. def get_language_pack(locale):
  8. """Get/cache a language pack
  9. Returns the langugage pack from cache if it exists, caches otherwise
  10. >>> get_language_pack('fr')['Dashboards']
  11. "Tableaux de bords"
  12. """
  13. pack = ALL_LANGUAGE_PACKS.get(locale)
  14. if not pack:
  15. filename = DIR + '/{}/LC_MESSAGES/messages.json'.format(locale)
  16. try:
  17. with open(filename) as f:
  18. pack = json.load(f)
  19. ALL_LANGUAGE_PACKS[locale] = pack
  20. except Exception:
  21. # Assuming english, client side falls back on english
  22. pass
  23. return pack