File size: 2,467 Bytes
b6130a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
Translate a .strings file from one language to another.
'''

from __future__ import with_statement

import re, sys, time
import codecs, locale
import PyGlang

k_langPathRegEx = re.compile('.*/([^\.]+)\.lproj.+$')
k_valueRegEx = re.compile('"([^"]*)"(\s*=\s*)"([^"]*)";', re.UNICODE)

def DetectEncoding(filepath):
    '''
    Try to detect the file's encoding.
    If its not utf-16 assume it's utf-8, this should work for ascii
    files becuase the first 128 characters are the same...
    '''
    
    with open(filepath, 'r') as f:
        firstBytes = f.read(2)

        if firstBytes == codecs.BOM_UTF16_BE:
            return 'utf_16_be'
        elif firstBytes == codecs.BOM_UTF16_LE:
            return 'utf_16_le'

    #use sig just encase there is a BOM in the file
    return 'utf_8_sig'

def LangFromPath(filepath):
    '''Get the languages from a filepath'''
    pathMatch = k_langPathRegEx.match(filepath)
    if pathMatch:
        return pathMatch.group(1)
    
def Translate(fromFilepath, toFilepath, utf8=False):
    '''
    Read a .strings file and localize it for the language of another .strings file.
    The language of each file is determined by the what 'lproj' directory they reside in.
    '''
    
    #detect encoding of output for printing
    language, output_encoding = locale.getdefaultlocale()
    
    #detect the encodeing of the file
    fromFileEncoding = 'utf_8' if utf8 else DetectEncoding(fromFilepath)
    
    #get the languages
    fromLang = LangFromPath(fromFilepath)
    toLang = LangFromPath(toFilepath)
    
    #regular expression
    def transValue(regExMatch):
        value = regExMatch.group(3)
        transText = PyGlang.Translate(value, fromLang=fromLang, toLang=toLang, encoding=fromFileEncoding)
        #TODO: only write this in command line mode
        print '%s > %s' % (value.encode(output_encoding), transText.encode(output_encoding))
        return '"%s"%s"%s";' % (regExMatch.group(1), regExMatch.group(2), transText)
    
    #read the file
    with codecs.open(fromFilepath, 'r', fromFileEncoding) as fromFile:
        with codecs.open(toFilepath, 'w', fromFileEncoding) as toFile:
            for eachLine in fromFile:
                toFile.write(k_valueRegEx.sub(transValue, eachLine))
   
if __name__ == '__main__':
    #TODO: add more robust options
    startTime = time.time()
    Translate(sys.argv[1], sys.argv[2], True)
    print 'Translated in %.2f seconds' % (time.time()-startTime)