You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
821 B

  1. '''
  2. Created on Jan 25, 2014
  3. @author: Chris
  4. Provides Internationalization for all text within the program.
  5. '''
  6. import io
  7. import os
  8. import json
  9. __all__ = ['load', '_']
  10. _DICTIONARY = None
  11. def load(language_dir, filename):
  12. ''' Open and return the supplied json file '''
  13. global _DICTIONARY
  14. try:
  15. json_file = filename + '.json'
  16. with io.open(os.path.join(language_dir, json_file), 'r', encoding='utf-8') as f:
  17. _DICTIONARY = json.load(f)
  18. except IOError:
  19. raise IOError('{0} Language file not found at location {1}. '
  20. 'Make sure that your translation file is in the '
  21. 'listed language directory'.format(filename.title(), language_dir))
  22. def translate(key):
  23. return _DICTIONARY.get(key, key)
  24. def _(key):
  25. return translate(key)