-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathtransifex.py
62 lines (52 loc) · 1.7 KB
/
transifex.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
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python2
#vim:set fileencoding=utf-8 sw=2 expandtab
import update
import requests
import json
class Transifex(object):
def __init__(self, user, passwd):
self.auth_ = (user, passwd)
r = requests.get(
'http://www.transifex.com/api/2/project/'
'arduino-ide-15/resource/ide-15/?details',
auth=self.auth_
)
r.raise_for_status()
d = r.json()
self.languages_ = set(lang['code'] for lang in d['available_languages'])
def canonical_lang(self, lang):
lang = lang.lower()
for l in self.languages_:
if l.lower() == lang:
return l
match = []
for l in self.languages_:
if l.split('_', 1)[0].lower() == lang:
match.append(l)
if len(match) > 1:
raise RuntimeError('Two or more candidates for %s: %s' % (lang, ' '.join(match)))
if len(match) == 0:
raise RuntimeError('No language code %s' % lang)
return match[0]
def translation(self, lang):
r = requests.get(
'https://www.transifex.com/api/2/project/arduino-ide-15/resource/ide-15/translation/%s/?file' % lang,
auth=self.auth_
)
r.raise_for_status()
r.encoding = 'utf-8' # workaround for a Transifex issue.
return r.text
def pull(self, lang, fname):
new = self.translation(lang).encode('utf-8')
new = map(lambda a: a + '\n', new.split('\n'))
new = update.read_po(new)
update.dump(new, fname)
def push(self, lang, data):
r = requests.put(
'http://www.transifex.com/api/2/project/'
'arduino-ide-15/resource/ide-15/translation/%s/' % lang,
data=json.dumps({ 'content': data }),
headers={ 'content-type': 'application/json' },
auth=self.auth_
)
r.raise_for_status()