Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: translations.py

Issue 29323730: Issue 2752 - Automate translation download (Closed)
Patch Set: Created Aug. 16, 2015, 9:04 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ensure_dependencies.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: translations.py
===================================================================
new file mode 100755
--- /dev/null
+++ b/translations.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+
+import os
+import re
+import sys
+import urllib2
+from StringIO import StringIO
+from zipfile import ZipFile
+
+import buildtools.localeTools
+
+BASE_PATH = os.path.dirname(os.path.abspath(__file__))
+LOCALES_PATH = os.path.join(BASE_PATH, "mobile", "android", "base", "locales",
+ "adblockbrowser")
+PROJECT_URL = "http://api.crowdin.net/api/project/adblockbrowserandroid"
+
+def print_usage():
+ print >>sys.stderr, "Usage: %s get API_KEY" % os.path.basename(sys.argv[0])
+
+def get_translations(api_key):
+ # We could use buildtools.localeTools.getTranslations here if we were
+ # submitting JSON wrappers for DTDs, the same way ABP for Firefox does it.
+ # See https://issues.adblockplus.org/2911.
+
+ result = urllib2.urlopen("%s/export?key=%s" % (PROJECT_URL, api_key))
+ if result.read().find("<success") < 0:
+ raise Exception("Export failed: " + result)
+
+ result = urllib2.urlopen("%s/download/all.zip?key=%s" % \
+ (PROJECT_URL, api_key))
+ zip = ZipFile(StringIO(result.read()))
+ for info in zip.infolist():
+ if not info.filename.endswith(".dtd"):
+ continue
+
+ dir, file = os.path.split(info.filename)
+ if not re.match(r"^[\w\-]+$", dir) or dir == "en-US":
+ continue
+
+ mapping = buildtools.localeTools.langMappingGecko
+ for key, value in mapping.iteritems():
+ if value == dir:
+ dir = key
+
+ data = zip.open(info.filename).read()
+ if not data:
+ continue
+
+ path = os.path.join(LOCALES_PATH, dir, file)
+ if not os.path.exists(os.path.dirname(path)):
+ os.makedirs(os.path.dirname(path))
+ with open(path, "w") as file:
+ file.write(data)
+
+if __name__ == "__main__":
+ if len(sys.argv) < 3:
+ print_usage()
+ sys.exit(1)
+
+ command, api_key = sys.argv[1:]
+ if command != "get":
+ print_usage()
+ sys.exit(2)
+
+ get_translations(api_key)
« no previous file with comments | « ensure_dependencies.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld