Debian Codegraph @

tools/i18n/build_pot.py

raw
1 import os, glob
2 import xml.etree.ElementTree as ElementTree
3 import shutil
4
5 pot_file = "darkradiant.pot"
6
7 if os.path.exists(pot_file):
8 print('Removing existing pot file: ' + pot_file)
9 os.remove(pot_file)
10
11 # xgettext needs to be in your path
12 xgettext_cmd = "xgettext";
13 xgettext_options = " --c++ --keyword=_ --keyword=N_ -o " + pot_file + " [email protected] ";
14
15 def invokeXGettext(targetfile):
16 norm_filename = os.path.normpath(targetfile);
17
18 if os.path.exists(pot_file):
19 cmdline = xgettext_cmd + xgettext_options + " -j " + norm_filename
20 else:
21 cmdline = xgettext_cmd + xgettext_options + norm_filename
22
23 #print(cmdline)
24 os.system(cmdline)
25
26 def scanCodeFiles(path, exclude_directories):
27 #print("Inspecting path: " + os.path.join(path, '*'))
28 for current_file in glob.glob( os.path.join(path, '*') ):
29 # Dive into directories
30 if os.path.isdir(current_file):
31 dir_normalised = current_file.replace('\\', '/')
32 if not dir_normalised in exclude_directories:
33 #print 'got a directory: ' + dir_normalised
34 scanCodeFiles(dir_normalised, exclude_directories)
35 else:
36 print('skipping directory: ' + dir_normalised)
37 else:
38 # Analyse files
39 ext = os.path.splitext(current_file)[1]
40
41 if ext in [".cpp", ".h"]:
42 print("Processing C++ file: " + current_file)
43 invokeXGettext(current_file)
44
45 def processXmlFile(path, nodeXPath, elementNames, attributes):
46 print("Processing XML file: " + path)
47
48 doc = ElementTree.parse(path)
49
50 elements = doc.findall(nodeXPath)
51
52 found_values = []
53
54 for element in elements:
55 if len(attributes) > 0:
56 for attr in attributes:
57 if element.attrib.get(attr):
58 found_values.append(element.attrib.get(attr))
59 else:
60 if len(elementNames) == 0:
61 # Get the actual value of the element
62 print(element.text)
63 found_values.append(element.text)
64 else:
65 for elementName in elementNames:
66 if element.tag == elementName and not element.text is None:
67 #print('Found ' + element.tag)
68 found_values.append(element.text)
69
70 # Now generate a dummy C++ file for xgettext to parse
71
72 if len(found_values) > 0:
73
74 dummy_file = "xml_file_content.cpp"
75
76 file = open(dummy_file, "w")
77
78 for value in found_values:
79 value = value.replace("\n", "\\n")
80 value = value.replace("\"", "\\\"")
81 file.write('_("' + value + '")' + "\n")
82
83 file.close()
84
85 invokeXGettext(dummy_file)
86
87 os.remove(dummy_file)
88
89 print("------- Scanning C++ Files ----------------");
90
91 build_root = '../../'
92
93 code_directories = ['radiant', 'plugins', 'libs']
94 exclude_directories = ['../../libs/pybind']
95
96 for directory in code_directories:
97 scanCodeFiles(os.path.join(build_root, directory), exclude_directories)
98
99 print("------- Scanning XML Files ----------------");
100
101 xml_directory = "install"
102 empty = []
103
104 # Add menu captions
105 processXmlFile(os.path.join(build_root, xml_directory, "menu.xml"), ".//*", empty, ["caption"])
106
107 # Add colour scheme names
108 processXmlFile(os.path.join(build_root, xml_directory, "colours.xml"), ".//descriptions/*", empty, ["value"])
109
110 # Add toolbar tooltips
111 processXmlFile(os.path.join(build_root, xml_directory, "user.xml"), ".//toolbutton", empty, ["tooltip"])
112 processXmlFile(os.path.join(build_root, xml_directory, "user.xml"), ".//toggletoolbutton", empty, ["tooltip"])
113
114 # Check UI files for translatable strings
115 for current_file in glob.glob( os.path.join(build_root, xml_directory, "ui", '*.xrc') ):
116 processXmlFile(current_file, ".//*", ['{http://www.wxwindows.org/wxxrc}label'], empty)
117
118 # Copy the POT file to the install folder
119 shutil.copy(pot_file, os.path.join(build_root, "install", "i18n"))