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.

46 lines
1.0 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. def get_path(language):
  13. ''' Returns the full path to the language file '''
  14. filename = language + '.json'
  15. lang_file_path = os.path.join(_DEFAULT_DIR, filename)
  16. if not os.path.exists(lang_file_path):
  17. raise IOError('Could not find {} language file'.format(language))
  18. return lang_file_path
  19. def load(filepath):
  20. ''' Open and return the supplied json file '''
  21. try:
  22. with open(filepath.lower(), 'rb') as f:
  23. return json.load(f)
  24. except IOError:
  25. raise IOError('Language file not found. Make sure that your ',
  26. 'translation file is in the languages directory, ')
  27. _DICTIONARY = load(get_path(_LANG))
  28. def translate(key):
  29. return _DICTIONARY[key]
  30. if __name__ == '__main__':
  31. pass