LanguageParser.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import requests
  4. from xml.dom.minidom import parse
  5. import xml.dom.minidom
  6. class LanguageParser:
  7. def parser(self, filename):
  8. result = {}
  9. domTree = xml.dom.minidom.parse(filename)
  10. collection = domTree.documentElement
  11. contexts = collection.getElementsByTagName('context')
  12. for context in contexts:
  13. name = context.getElementsByTagName('name')[0]
  14. name = name.childNodes[0].data
  15. print('name is %s' % name)
  16. languages = {}
  17. messages = context.getElementsByTagName('message')
  18. for message in messages:
  19. language = {}
  20. source = message.getElementsByTagName('source')[0]
  21. translation = message.getElementsByTagName('translation')[0]
  22. language['source'] = source.childNodes[0].data
  23. if translation.childNodes.length > 0:
  24. language['translation'] = translation.childNodes[0].data
  25. else:
  26. language['translation'] = ''
  27. # 取出location
  28. locations = []
  29. location_tags = message.getElementsByTagName('location')
  30. for location_tag in location_tags:
  31. location = {}
  32. filename = location_tag.getAttribute('filename')
  33. line = location_tag.getAttribute('line')
  34. if filename:
  35. location['filename'] = filename
  36. else:
  37. location['filename'] = ''
  38. if line:
  39. location['line'] = line
  40. else:
  41. location['line'] = ''
  42. locations.append(location)
  43. if len(locations) == 0:
  44. locations.append({'filename': '', 'line': 0})
  45. language['locations'] = locations
  46. languages[language['source']] = language
  47. result[name] = {
  48. 'name': name,
  49. 'languages': languages
  50. }
  51. return result