Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python 3.
- import traceback, sys, re
- translations = r"""
- ZeroDivisionError: division by zero
- DK: division med 0
- FR: division par zéro
- ES: división por cero
- AttributeError: '(\w+)' object has no attribute '(\w+)'
- DK: '\1' objektet har ingen attribut '\2'
- ES: No hay '\1' attribute '\2'
- DE: '\1' hat keiner Attribute beim Name '\2'
- FR: '\1' n'a pas d'attribut '\2'
- """
- def _parse_messages():
- messages = []
- pattern = None
- translate_map = None
- for linx in translations.split('\n'):
- lin = linx.rstrip()
- if lin != '':
- if pattern is None:
- pattern = lin
- translate_map = {}
- messages.append((pattern, translate_map))
- else:
- lang, msg = lin.split(':', 1)
- msg = msg.lstrip()
- translate_map[lang] = msg
- else:
- pattern = None
- translate_map = None
- return messages
- messages = _parse_messages()
- del _parse_messages
- def translate_line(line, language):
- if language is None:
- return line
- for pattern, translate_map in messages:
- if re.match(pattern, line) is not None and language in translate_map:
- return line.split(':',1)[0] + ': ' + re.sub(pattern, translate_map[language], line)
- else:
- return line
- def translate(text, language):
- return '\n'.join(translate_line(line, language) for line in text.split('\n'))
- def install_excepthook(language):
- def excepthook(type, value, tb):
- # print(traceback.format_exception(type, value, tb))
- print(translate('\n'.join(traceback.format_exception(type, value, tb)), language), file=sys.stderr)
- sys.excepthook = excepthook
- def print_translated_tb(language):
- print(translate(traceback.format_exc(), language), file=sys.stderr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement