~automne-team/automne/trunk

« back to all changes in this revision

Viewing changes to automne/phpMyAdmin/scripts/update-from-po

  • Committer: sebastien-pauchet
  • Date: 2012-02-15 16:47:40 UTC
  • mfrom: (363.2.105 4.2)
  • Revision ID: seb@automne-cms.org-20120215164740-xrk26iafkvztwv6s
Merge stable branch 4.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
import polib
4
 
import sys
5
 
import os
6
 
import codecs
7
 
 
8
 
CODE2LANG = {
9
 
    'af': 'afrikaans',
10
 
    'ar': 'arabic',
11
 
    'az': 'azerbaijani',
12
 
    'bn': 'bangla',
13
 
    'be': 'belarusian_cyrillic',
14
 
    'be@latin': 'belarusian_latin',
15
 
    'bg': 'bulgarian',
16
 
    'bs': 'bosnian',
17
 
    'ca': 'catalan',
18
 
    'cs': 'czech',
19
 
    'da': 'danish',
20
 
    'de': 'german',
21
 
    'el': 'greek',
22
 
    'en': 'english',
23
 
    'en_GB': 'english-gb',
24
 
    'es': 'spanish',
25
 
    'et': 'estonian',
26
 
    'eu': 'basque',
27
 
    'fa': 'persian',
28
 
    'fi': 'finnish',
29
 
    'fr': 'french',
30
 
    'gl': 'galician',
31
 
    'he': 'hebrew',
32
 
    'hi': 'hindi',
33
 
    'hr': 'croatian',
34
 
    'hu': 'hungarian',
35
 
    'id': 'indonesian',
36
 
    'it': 'italian',
37
 
    'ja': 'japanese',
38
 
    'ko': 'korean',
39
 
    'ka': 'georgian',
40
 
    'lt': 'lithuanian',
41
 
    'lv': 'latvian',
42
 
    'mk': 'macedonian_cyrillic',
43
 
    'mn': 'mongolian',
44
 
    'ms': 'malay',
45
 
    'nl': 'dutch',
46
 
    'nb': 'norwegian',
47
 
    'pl': 'polish',
48
 
    'pt_BR': 'brazilian_portuguese',
49
 
    'pt': 'portuguese',
50
 
    'ro': 'romanian',
51
 
    'ru': 'russian',
52
 
    'si': 'sinhala',
53
 
    'sk': 'slovak',
54
 
    'sl': 'slovenian',
55
 
    'sq': 'albanian',
56
 
    'sr@latin': 'serbian_latin',
57
 
    'sr': 'serbian_cyrillic',
58
 
    'sv': 'swedish',
59
 
    'th': 'thai',
60
 
    'tr': 'turkish',
61
 
    'tt': 'tatarish',
62
 
    'uk': 'ukrainian',
63
 
    'zh_TW': 'chinese_traditional',
64
 
    'zh_CN': 'chinese_simplified',
65
 
    'uz': 'uzbek_cyrillic',
66
 
    'uz@latin': 'uzbek_latin',
67
 
}
68
 
 
69
 
if len(sys.argv) < 2:
70
 
    print 'Usage: update-from-po PO_FILES'
71
 
    sys.exit(1)
72
 
 
73
 
f = file('lang/english-utf-8.inc.php', 'r')
74
 
langmap = {}
75
 
for line in f:
76
 
    line = line.strip()
77
 
    if line[:4] == '$str':
78
 
        parts = line.split(' = ')
79
 
        langmap[parts[1].strip(';').strip('\'')] = parts[0].strip('$')
80
 
 
81
 
for pofile_full in sys.argv[1:]:
82
 
    pofile = os.path.basename(pofile_full)
83
 
    if pofile[-3:] != '.po':
84
 
        print 'Not a po file, skipping: %s' % pofile
85
 
        continue
86
 
 
87
 
    try:
88
 
        lang = CODE2LANG[pofile[:-3]]
89
 
    except KeyError:
90
 
        print 'Language for %s not defined!' % pofile
91
 
        continue
92
 
 
93
 
    try:
94
 
        langfile = codecs.open('lang/%s-utf-8.inc.php' % lang, 'r', 'utf-8').readlines()
95
 
    except IOError:
96
 
        print 'Language file %s does not exist!' % lang
97
 
        continue
98
 
 
99
 
    print 'Updating %s from: %s' % (lang, pofile)
100
 
    po = polib.pofile(os.path.join(sys.argv[1], pofile_full))
101
 
 
102
 
    for translation in po.translated_entries():
103
 
        if translation.msgctxt is None:
104
 
            msgid = translation.msgid.replace('\'', '\\\'')
105
 
            msgstr = translation.msgstr.replace('\'', '\\\'')
106
 
            try:
107
 
                key = langmap[msgid]
108
 
                keylen = len(key)
109
 
            except KeyError:
110
 
                continue
111
 
            for i in xrange(len(langfile)):
112
 
                if langfile[i][:2 + keylen] == '$%s ' % key:
113
 
                    langfile[i] = u'$%s = \'%s\';\n' % (key, msgstr)
114
 
 
115
 
    out = codecs.open('lang/%s-utf-8.inc.php' % lang, 'w', 'utf-8')
116
 
    out.writelines(langfile)