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.

48 lines
1.1 KiB

  1. '''
  2. Created on Jan 25, 2014
  3. @author: Chris
  4. Provides Internationalization for all text within the program.
  5. '''
  6. import os
  7. import json
  8. import i18n_config
  9. __all__ = ['translate']
  10. _LANG = i18n_config.LANG
  11. _DEFAULT_DIR = os.path.join(os.path.dirname(__file__), 'languages')
  12. _DICTIONARY = None
  13. def get_path(language):
  14. ''' Returns the full path to the language file '''
  15. filename = language.lower() + '.json'
  16. lang_file_path = os.path.join(_DEFAULT_DIR, filename)
  17. if not os.path.exists(lang_file_path):
  18. raise IOError('Could not find {} language file'.format(language))
  19. return lang_file_path
  20. def load(filename):
  21. ''' Open and return the supplied json file '''
  22. global _DICTIONARY
  23. try:
  24. json_file = filename + '.json'
  25. with open(os.path.join(_DEFAULT_DIR, json_file), 'rb') as f:
  26. _DICTIONARY = json.load(f)
  27. except IOError:
  28. raise IOError('Language file not found. Make sure that your ',
  29. 'translation file is in the languages directory, ')
  30. def translate(key):
  31. return _DICTIONARY[key]
  32. if __name__ == '__main__':
  33. pass