extract_strings.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # SuperTux
  3. # Copyright (C) 2018 Tobias Markus <tobbi.bugs@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import sys
  18. import re
  19. pattern = re.compile(r"(translate|_)\((.*?)\)")
  20. lisp_template_begin = """(msgids
  21. """;
  22. lisp_template_end = ")"
  23. lisp_content = lisp_template_begin
  24. if(len(sys.argv) < 3):
  25. print("Usage: " + sys.argv[0] + " <filename> <output file>")
  26. else:
  27. filename = sys.argv[1]
  28. out_file = sys.argv[2]
  29. strings_found = False
  30. with open(filename, 'r') as f:
  31. s = f.read()
  32. matches = pattern.findall(s)
  33. if len(matches) > 0:
  34. strings_found = True
  35. for match in matches:
  36. lines = match[1].split("\\n")
  37. num_lines = len(lines)
  38. if num_lines == 1:
  39. lisp_content += " (msgid "
  40. lisp_content += "(_ " + match[1] + ")"
  41. lisp_content += ")\r\n"
  42. else:
  43. lisp_content += " (msgid (_ "
  44. for i, line in enumerate(lines):
  45. lisp_content += line
  46. if i < num_lines:
  47. if i == num_lines - 1:
  48. lisp_content += "))"
  49. lisp_content += "\n"
  50. lisp_content += lisp_template_end
  51. if strings_found:
  52. output_file = open(out_file, "w")
  53. output_file.write(lisp_content)
  54. output_file.close()