forked from OriginProtocol/origin-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_po_files.py
executable file
·50 lines (39 loc) · 1.77 KB
/
fix_po_files.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
#!/usr/local/bin/python
# Script to fix or detect various issues that come up in po files
import fnmatch
import os
import re
po_files = []
for root, dirnames, filenames in os.walk('.'):
for filename in fnmatch.filter(filenames, '*.po'):
pathname = os.path.join(root, filename)
# Get language code
try:
language_code = pathname.split("/")[1]
except:
continue
print ("%s:\t%s" % (language_code, pathname))
f = open(pathname,"r+")
d = f.readlines()
f.seek(0)
for line_number, line in enumerate(d, 1):
# Fix spaces in closing tags (Google Translator Toolkit does this)
# It changes `</strong>` into `</ Strong>`
if line.startswith("msgid") or line.startswith("msgstr"):
(line, subs) = re.subn(r'</ +', '</', line, flags=re.IGNORECASE)
if subs > 0:
print (" -FIXED %s: `</ ` space line %d" % (language_code, line_number))
# Fix lone `%` symbols -- they must be escaped as `%%`
if line.startswith("msgid") or line.startswith("msgstr"):
(line, subs) = re.subn(r'(?<!%)%(?!%)', '%%', line, flags=re.IGNORECASE)
if subs > 0:
print (" -FIXED %s: `%%` --> `%%%%` line %d" % (language_code, line_number))
# Fix invalid empty language specification
if line == '"Language: \\n"\n':
# Fix dumb issue "Language:" header is present, but
# Doesn't specify the language
print (" -FIXED %s: `Langage: header` line line %d" % (language_code, line_number))
line = '"Language: %s\\n"\n' % language_code
f.write(line)
f.truncate()
f.close()