-
Notifications
You must be signed in to change notification settings - Fork 27
/
xliff-to-strings.py
executable file
·131 lines (114 loc) · 4.67 KB
/
xliff-to-strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
#
# xliff-export.py l10n-repository export-directory
#
# Convert the l10n repository from the following format:
#
# en/firefox-ios.xliff
# fr/firefox-ios.xliff
#
# To the following format:
#
# Client/en-US.lproj/Localizable.strings
# Client/fr.lproj/Localizable.strings
# ShareTo/en-US.lproj/ShareTo.strings
# ShareTo/fr.lproj/ShareTo.strings
# SendTo/en-US.lproj/SendTo.strings
# SendTo/fr.lproj/SendTo.strings
#
# For any Info.plist file in the xliff, we generate a InfoPlist.strings.
#
import glob
import os
import sys
from lxml import etree
NS = {'x':'urn:oasis:names:tc:xliff:document:1.2'}
# Files we are interested in. It would be nice to not hardcode this but I'm not totally sure how yet.
FILES = [
"Client/BookmarkPanel.strings",
"Client/ClearPrivateData.strings",
"Client/ErrorPages.strings",
"Client/HistoryPanel.strings",
"Client/Info.plist",
"Client/Intro.strings",
"Client/Localizable.strings",
"Client/PrivateBrowsing.strings",
"Client/Search.strings",
"Client/Shared.strings",
"Client/Storage.strings",
"Extensions/SendTo/Info.plist",
"Extensions/SendTo/SendTo.strings",
"Extensions/ShareTo/ShareTo.strings",
"Extensions/ViewLater/Info.plist",
"Shared/Localizable.strings",
]
# Because Xcode is unpredictable. See bug 1162510 - Sync.strings are not imported
FILENAME_OVERRIDES = {
"Shared/Supporting Files/Info.plist": "Shared/Localizable.strings",
"Shared/Supporting Files/Shared.strings": "Client/Shared.strings",
"Storage.strings": "Client/Storage.strings",
}
def export_xliff_file(file_node, export_path, target_language):
directory = os.path.dirname(export_path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(export_path, "w") as fp:
for trans_unit_node in file_node.xpath("x:body/x:trans-unit", namespaces=NS):
trans_unit_id = trans_unit_node.get("id")
targets = trans_unit_node.xpath("x:target", namespaces=NS)
if trans_unit_id is not None and len(targets) == 1 and targets[0].text is not None:
notes = trans_unit_node.xpath("x:note", namespaces=NS)
if len(notes) == 1:
line = u"/* %s */\n" % notes[0].text
fp.write(line.encode("utf8"))
source_text = trans_unit_id.replace('"', '\\"')
target_text = targets[0].text.replace('"', '\\"')
line = u"\"%s\" = \"%s\";\n\n" % (source_text, target_text)
fp.write(line.encode("utf8"))
# Export fails if the strings file is empty. Xcode probably checks
# on file length vs read error.
contents = open(export_path).read()
if len(contents) == 0:
os.remove(export_path)
def original_path(root, target, original):
dir,file = os.path.split(original)
if file == "Info.plist":
file = "InfoPlist.strings"
lproj = "%s.lproj" % target_language
path = dir + "/" + lproj + "/" + file
return path
if __name__ == "__main__":
import_root = sys.argv[1]
if not os.path.isdir(import_root):
print "import path does not exist or is not a directory"
sys.exit(1)
export_root = sys.argv[2]
if not os.path.isdir(export_root):
print "export path does not exist or is not a directory"
sys.exit(1)
for xliff_path in glob.glob(import_root + "/*/firefox-ios.xliff"):
print "Exporting", xliff_path
with open(xliff_path) as fp:
tree = etree.parse(fp)
root = tree.getroot()
# Make sure there are <file> nodes in this xliff file.
file_nodes = root.xpath("//x:file", namespaces=NS)
if len(file_nodes) == 0:
print " ERROR: No translated files. Skipping."
continue
# Take the target language from the first <file>. Not sure if that
# is a bug in the XLIFF, but in some files only the first node has
# the target-language set.
target_language = file_nodes[0].get('target-language')
if not target_language:
print " ERROR: Missing target-language. Skipping."
continue
# Export each <file> node as a separate strings file under the
# export root.
for file_node in file_nodes:
original = file_node.get('original')
original = FILENAME_OVERRIDES.get(original, original)
if original in FILES:
export_path = original_path(export_root, target_language, original)
print " Writing", export_path, target_language
export_xliff_file(file_node, export_path, target_language)