Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 134 lines (107 sloc) 3.36 kb
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
1 #!/usr/bin/python
2
f097b7d @scrawl Use english string for untranslated msgs when converting to .xml
scrawl authored
3 import sys, time, os
4
5 fallback_file = "core_language_english_tag.xml"
f1c8dca @scrawl .pot header
scrawl authored
6
7 header = "# SOME DESCRIPTIVE TITLE.\n\
8 # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER\n\
9 # This file is distributed under the same license as the PACKAGE package.\n\
10 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n\
11 #\n\
12 #, fuzzy\n\
13 msgid \"\"\n\
14 msgstr \"\"\n\
15 \"Project-Id-Version: PACKAGE VERSION\"\n\
16 \"Report-Msgid-Bugs-To: \"\n\
17 \"POT-Creation-Date: " + time.strftime("%Y-%m-%d %H:%M%z") + "\"\n\
18 \"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\"\n\
19 \"Last-Translator: FULL NAME <EMAIL@ADDRESS>\"\n\
20 \"Language-Team: LANGUAGE <LL@li.org>\"\n\
21 \"Language: \"\n\
22 \"MIME-Version: 1.0\"\n\
23 \"Content-Type: text/plain; charset=CHARSET\"\n\
24 \"Content-Transfer-Encoding: 8bit\"\n"
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
25
e79cac4 @scrawl Added "ignore list" for tags like GameVersion or PageURL that should not...
scrawl authored
26 ignore_tags = ["GameVersion", "PageURL"]
27
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
28 def usage():
29 print "Usage: " + sys.argv[0] + " someFile.xml someFile.pot"
30 print "Usage: " + sys.argv[0] + " someFile.po someFile.xml"
31
32 if (len(sys.argv)) != 3:
33 usage()
34 sys.exit(0)
35
36 file1 = sys.argv[1]
37 file2 = sys.argv[2]
38
39 f1 = open(file1, 'r')
40
41 f2 = open(file2, 'w')
42
43 msgs = {}
44 # msgs = {"foo":"bar", "asdf":"ghij"}
45
46 if file1.endswith(".xml") and file2.endswith(".pot"):
47 #xml2po
48
49 # read xml
50 for line in f1:
51 if line.strip().startswith("<Tag name="):
52 msgid = line.split("=\"")[1].split("\"")[0]
53 msgstr = line.split(">")[1].split("<")[0]
e79cac4 @scrawl Added "ignore list" for tags like GameVersion or PageURL that should not...
scrawl authored
54 # ignore
55 if msgid not in ignore_tags:
56 msgs[msgid] = msgstr
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
57
f1c8dca @scrawl .pot header
scrawl authored
58 # write pot
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
59
60 # header
61 # TODO
62
63 # translations
f1c8dca @scrawl .pot header
scrawl authored
64 result = header + "\n"
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
65 for mid, mstr in msgs.items():
a8ba61e @scrawl Write english text in msgid, and tag in msgctxt
scrawl authored
66 #result += "# " + mid + "\n"
67 result += "msgctxt \"" + mid + "\"\n"
68 result += "msgid \"" + mstr + "\"\n"
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
69 result += "msgstr \"\"\n"
70 result += "\n"
71
72 f2.write(result)
73
74 elif file1.endswith(".po") and file2.endswith(".xml"):
75 #po2xml
76
77 msgid = ""
78 msgstr = ""
79 last = "msgstr"
80 # read po
81 for line in f1:
82 if line.strip().startswith("\""):
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
83 if last == "msgctxt":
f097b7d @scrawl Use english string for untranslated msgs when converting to .xml
scrawl authored
84 #msgid += line.strip().split("\"")[1].replace("\\n", "\n")
85 pass
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
86 elif last == "msgstr":
87 msgstr += line.strip().split("\"")[1].replace("\\n", "\n")
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
88 if line.strip().startswith("msgctxt"):
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
89 # add previous msgstr
90 if msgid != "":
91 msgs[msgid] = msgstr
92
93 msgid = line.split("\"")[1].replace("\\n", "\n")
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
94 last = "msgctxt"
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
95 if line.strip().startswith("msgstr"):
96 msgstr =line.split("\"")[1].replace("\\n", "\n")
97 last = "msgstr"
5f61f65 @scrawl Fixed reading .po bug (last line not read)
scrawl authored
98 # add last line
99 msgs[msgid] = msgstr
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
100
101 # write xml
102 result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
103 <MyGUI>\n"
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
104
f097b7d @scrawl Use english string for untranslated msgs when converting to .xml
scrawl authored
105 # read english xml for fallback when untranslated
106 msgs_f = {}
107 f_f = open(os.path.join(os.path.dirname(file2), fallback_file))
108 for line in f_f:
109 if line.strip().startswith("<Tag name="):
110 msgid = line.split("=\"")[1].split("\"")[0]
111 msgstr = line.split(">")[1].split("<")[0]
112 msgs_f[msgid] = msgstr
113
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
114 for mid, mstr in msgs.items():
a9ca2df @scrawl New language xmls (german 97% translated), fixed bug in xml_po_parser th...
scrawl authored
115 if mid in msgs_f:
e79cac4 @scrawl Added "ignore list" for tags like GameVersion or PageURL that should not...
scrawl authored
116 if mstr.strip() != "" and mid not in ignore_tags:
a9ca2df @scrawl New language xmls (german 97% translated), fixed bug in xml_po_parser th...
scrawl authored
117 result += "\t<Tag name=\"" + mid + "\">" + mstr + "</Tag>\n"
118 else:
e79cac4 @scrawl Added "ignore list" for tags like GameVersion or PageURL that should not...
scrawl authored
119 # if untranslated or not to be translated (ignore), use english string
a9ca2df @scrawl New language xmls (german 97% translated), fixed bug in xml_po_parser th...
scrawl authored
120 result += "\t<Tag name=\"" + mid + "\">" + msgs_f[mid] + "</Tag>\n"
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
121
c31bdec @scrawl For ignored tags, use the english string in generated .xml
scrawl authored
122 # use english string for ignored
123 for ignored in ignore_tags:
124 result += "\t<Tag name=\"" + ignored + "\">" + msgs_f[ignored] + "</Tag>\n"
125
bddb0fd @scrawl xml/po converter: .po to .xml conversion with msgctxt as tag
scrawl authored
126 result += "\n</MyGUI>"
127 f2.write(result)
2839c39 @scrawl Added xml_po_parser.py
scrawl authored
128
129 else:
130 usage()
131
132 f1.close()
133 f2.close()
Something went wrong with that request. Please try again.