-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
transifex.py
113 lines (80 loc) · 2.62 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
transifex.py - Tool to manage Transifex translations
u, up, update <DIR> Download all translations from Transifex
l, list List all translations available on Transifex
"""
import os
import sys
import requests
API_URL = "https://www.transifex.com/api/2"
API_KEY = os.getenv("TRANSIFEX_API_KEY")
PROJECT_SLUG = "flex-pelican"
RESOURCE_SLUG = "messagespot"
def get_languages():
url = "{api_url}/project/{project_slug}/languages/".format(
**{"api_url": API_URL, "project_slug": PROJECT_SLUG,}
)
req = requests.get(url, auth=("api", API_KEY))
req.raise_for_status()
languages = req.json()
return sorted([lang["language_code"] for lang in languages])
def list_languages():
languages = get_languages()
print("LANG LOCAL")
for lang in languages:
filename1 = os.path.join(".", lang, "LC_MESSAGES", "messages.po")
filename2 = os.path.join(
".", "translations", lang, "LC_MESSAGES", "messages.po"
)
if os.path.exists(filename1) or os.path.exists(filename2):
print("{0} YES".format(lang.ljust(5)))
else:
print("{0} NO".format(lang.ljust(5)))
print("\nTotal: {}".format(len(languages)))
def download(path):
languages = get_languages()
for lang in languages:
print("Downloading {}...".format(lang))
url = "{api_url}/project/{project_slug}/resource/{resource_slug}/translation/{lang_code}/".format(
**{
"api_url": API_URL,
"project_slug": PROJECT_SLUG,
"resource_slug": RESOURCE_SLUG,
"lang_code": lang,
}
)
req = requests.get(url, auth=("api", API_KEY))
req.raise_for_status()
data = req.json()
po_content = data["content"]
filepath = os.path.join(path, lang, "LC_MESSAGES")
os.makedirs(filepath, exist_ok=True)
filename = os.path.join(path, lang, "LC_MESSAGES", "messages.po")
with open(filename, "w") as f:
f.write(po_content)
print("{} updated".format(lang))
def usage():
print(__doc__[1:])
def _pop(l, default=None):
try:
return l.pop(0)
except IndexError:
return default
def run():
args = sys.argv[1:]
cmd = _pop(args, "help")
if cmd in ["u", "up", "update"]:
path = _pop(args)
if path and not args:
download(path)
else:
usage()
elif cmd in ["l", "list"]:
if not args:
list_languages()
else:
usage()
else:
usage()
if __name__ == "__main__":
run()