forked from vanilla-music/vanilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-translations.py
executable file
·59 lines (50 loc) · 1.79 KB
/
update-translations.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
#!/usr/bin/python
# Fetch the translations from http://crowdin.net/project/vanilla-music and save
# them to res/ in the current directory. Removes files that contain no
# translations and removes incomplete plurals (they cause crashes).
#
# This script does not force crowdin to rebuild the translation package. That
# should be done through the website before running this script.
#
# Requires python-lxml.
try:
# python 3
from urllib.request import urlopen
except ImportError:
# python 2
from urllib2 import urlopen
from lxml import etree
from zipfile import ZipFile
import io
import os
data = urlopen('http://crowdin.net/download/project/vanilla-music.zip')
ar = ZipFile(io.BytesIO(data.read()))
parser = etree.XMLParser(remove_blank_text=True)
for name in ar.namelist():
# ignore directories
if not name.endswith('translatable.xml'):
continue
doc = etree.parse(ar.open(name), parser)
# remove plurals without "other" quantity (they cause crashes)
for e in doc.xpath("//plurals[not(item/@quantity='other')]"):
e.getparent().remove(e)
# ignore languages with no translations
if len(doc.getroot()) == 0:
continue
# make some translations more general
lang = name.split('/')[0]
if lang == 'es-ES':
lang = 'es'
# The Android convention seems to be to put pt-PT in values-pt-rPT and
# put pt-BR in values-pt. But since we have no pt-BR translation yet,
# I'm just putting pt-PT in values-pt for now. Hopefully this doesn't
# cause any issues.
elif lang == 'pt-PT':
lang = 'pt'
# create dir if needed (assume res/ exists already)
path = 'res/values-' + lang
if not (os.path.isdir(path)):
os.mkdir(path)
# save result
with io.open(path + "/translatable.xml", "w") as file:
file.write(etree.tostring(doc, encoding='unicode', pretty_print=True, doctype='<?xml version="1.0" encoding="utf-8"?>'))