-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalization.py
More file actions
75 lines (71 loc) · 2.48 KB
/
Copy pathlocalization.py
File metadata and controls
75 lines (71 loc) · 2.48 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
65
66
67
68
69
70
71
72
73
74
75
import re
import os
import locale
import cPickle
import conf
from util.unicode import Encode
reTrans = re.compile(r'^([ :=\.]*)(.+?)([ :=\.]*$)')
def compileTranslator(txtPath,pklPath):
"""Compiles specified txtFile into pklFile."""
reSource = re.compile(r'^=== ')
reValue = re.compile(r'^>>>>\s*$')
#--Scan text file
translator = {}
def addTranslation(key,value):
key,value = key[:-1],value[:-1]
if key and value:
key = reTrans.match(key).group(2)
value = reTrans.match(value).group(2)
translator[key] = value
key,value,mode = '','',0
textFile = file(txtPath)
for line in textFile:
#--Begin key input?
if reSource.match(line):
addTranslation(key,value)
key,value,mode = '','',1
#--Begin value input?
elif reValue.match(line):
mode = 2
elif mode == 1:
key += line
elif mode == 2:
value += line
addTranslation(key,value) #--In case missed last pair
textFile.close()
#--Write translator to pickle
filePath = pklPath
tempPath = filePath+'.tmp'
cPickle.dump(translator,open(tempPath,'w'))
if os.path.exists(filePath): os.remove(filePath)
os.rename(tempPath,filePath)
#--Do translator test and set
if locale.getlocale() == (None,None):
locale.setlocale(locale.LC_ALL,'')
language = locale.getlocale()[0].split('_',1)[0]
if language.lower() == 'german': language = 'de' #--Hack for German speakers who aren't 'DE'.
languagePkl, languageTxt = (os.path.join('data',language+ext) for ext in ('.pkl','.txt'))
#--Recompile pkl file?
if os.path.exists(languageTxt) and (
not os.path.exists(languagePkl) or (
os.path.getmtime(languageTxt) > os.path.getmtime(languagePkl)
)
):
compileTranslator(languageTxt,languagePkl)
#--Use dictionary from pickle as translator
if os.path.exists(languagePkl):
pklFile = open(languagePkl)
reEscQuote = re.compile(r"\\'")
_translator = cPickle.load(pklFile)
pklFile.close()
def _(text,encode=True):
text = Encode(text,'mbcs')
if encode: text = reEscQuote.sub("'",text.encode('string_escape'))
head,core,tail = reTrans.match(text).groups()
if core and core in _translator:
text = head+_translator[core]+tail
if encode: text = text.decode('string_escape')
if conf.useUnicode: text = unicode(text,'mbcs')
return text
else:
def _(text,encode=True): return text