-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangpack.py
More file actions
64 lines (56 loc) · 2.1 KB
/
langpack.py
File metadata and controls
64 lines (56 loc) · 2.1 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import hashlib
import struct
# i18n() in embedded PHP computes MD5 sum of text and searches
# it in /var/sealpac/sealpac.slp
# That file is also stored gzipped in langpack part of flash
def gen_sealpac_from_hashed(md5dict, langcode=b'en'):
""" Generates binary content of sealpac.slp
md5dict:
keys have to be md5 (bytes, len 16)
values have to be utf-8 encoded translations
langcode:
language code: e.g. de, en, es, fr, it, pl, pt
"""
magic = b'\x05\xea\x19\xac'
header_size = 0x30
entry_size = 0x14
transl_block = b''
entries = b''
offset = header_size + len(md5dict)*entry_size
for md5, translation in sorted(md5dict.items()):
transl_block += translation + b'\0'
entries += md5 + struct.pack('>L', offset)
offset += len(translation) + 1
header = magic + struct.pack(">L", len(md5dict)) + (8*b'\0')
header += langcode[:15].ljust(16, b'\0')
body = entries + transl_block
h = hashlib.md5(body).digest()
header += h
return header + body
def gen_sealpac(dictionary, langcode=b'en'):
md5dict = {}
for original, translation in dictionary.items():
h = hashlib.md5(original.encode('utf-8')).digest()
md5dict[h] = translation.encode('utf-8')
return gen_sealpac_from_hashed(md5dict)
if __name__ == "__main__":
import sys
try:
infile, outfile = sys.argv[1], sys.argv[2]
try:
langcode = sys.argv[3].encode('ascii')
except IndexError:
langcode = 'en'
except Exception:
print("Generate langpack/sealpac (for i18n):\n" +
" %s <translations.txt> <outlangpack.lng> [langcode]\n" % sys.argv[0]+
"Each line in translations.txt should contain tab separated:\n" +
"<original>\t<translation>")
sys.exit(1)
with open(infile, 'r') as sfile:
data = sfile.read()
dictionary = dict( line.split('\t') for line in data.split('\n') if len(line) > 0 )
with open(outfile, 'xb') as ofile:
ofile.write(gen_sealpac(dictionary, langcode))