extract_strings.py 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import sys
  2. import re
  3. pattern = re.compile(r"(translate|_)\((.*?)\)")
  4. lisp_template_begin = """(msgids
  5. """;
  6. lisp_template_end = ")"
  7. lisp_content = lisp_template_begin
  8. if(len(sys.argv) < 3):
  9. print("Usage: " + sys.argv[0] + " <filename> <output file>")
  10. else:
  11. filename = sys.argv[1]
  12. out_file = sys.argv[2]
  13. strings_found = False
  14. with open(filename, 'r') as f:
  15. s = f.read()
  16. matches = pattern.findall(s)
  17. if len(matches) > 0:
  18. strings_found = True
  19. for match in matches:
  20. lines = match[1].split("\\n")
  21. num_lines = len(lines)
  22. if num_lines == 1:
  23. lisp_content += " (msgid "
  24. lisp_content += "(_ " + match[1] + ")"
  25. lisp_content += ")\r\n"
  26. else:
  27. lisp_content += " (msgid (_ "
  28. for i, line in enumerate(lines):
  29. lisp_content += line
  30. if i < num_lines:
  31. if i == num_lines - 1:
  32. lisp_content += "))"
  33. lisp_content += "\n"
  34. lisp_content += lisp_template_end
  35. if strings_found:
  36. output_file = open(out_file, "w")
  37. output_file.write(lisp_content)
  38. output_file.close()