[Pkg-mozext-commits] [adblock-plus] 267/464: Added build.py gettranslations command to import translations from Crodwin

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:44:24 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit 2d84349850263bd0c503fe33bcf1bd4ee5a72657
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Fri Jul 6 10:38:22 2012 +0200

    Added build.py gettranslations command to import translations from Crodwin
---
 build.py       | 22 ++++++++++++++++++++++
 localeTools.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/build.py b/build.py
index 4b02831..cde89e7 100644
--- a/build.py
+++ b/build.py
@@ -223,6 +223,22 @@ def updateTranslationMaster(baseDir, scriptName, opts, args, type):
   import buildtools.localeTools as localeTools
   localeTools.updateTranslationMaster(defaultLocaleDir, packager.defaultLocale, basename, key)
 
+
+def getTranslations(baseDir, scriptName, opts, args, type):
+  if len(args) < 1:
+    print 'Project key is required to update translation master files.'
+    usage(scriptName, type, 'translate')
+    return
+
+  key = args[0]
+  import buildtools.packager as packager
+  localesDir = packager.getLocalesDir(baseDir)
+  basename = packager.readMetadata(baseDir).get('general', 'baseName')
+
+  import buildtools.localeTools as localeTools
+  localeTools.getTranslations(localesDir, packager.defaultLocale, basename, key)
+
+
 def showDescriptions(baseDir, scriptName, opts, args, type):
   locales = None
   for option, value in opts:
@@ -346,6 +362,12 @@ with addCommand(updateTranslationMaster, 'translate') as command:
   command.params = '[options] project-key'
   command.supportedTypes = ('gecko')
 
+with addCommand(getTranslations, 'gettranslations') as command:
+  command.shortDescription = 'Downloads translation updates'
+  command.description = 'Downloads updated translations from crowdin.net.'
+  command.params = '[options] project-key'
+  command.supportedTypes = ('gecko')
+
 with addCommand(showDescriptions, 'showdesc') as command:
   command.shortDescription = 'Print description strings for all locales'
   command.description = 'Display description strings for all locales as specified in the corresponding meta.properties files.'
diff --git a/localeTools.py b/localeTools.py
index 220b304..7f95016 100644
--- a/localeTools.py
+++ b/localeTools.py
@@ -7,6 +7,7 @@
 import re, os, sys, codecs, json, urllib, urllib2
 from StringIO import StringIO
 from ConfigParser import SafeConfigParser
+from zipfile import ZipFile
 from xml.parsers.expat import ParserCreate, XML_PARAM_ENTITY_PARSING_ALWAYS
 
 class OrderedDict(dict):
@@ -141,6 +142,21 @@ def toJSON(path):
     result[name] = obj
   return json.dumps(result, indent=2)
 
+def fromJSON(path, data):
+  data = json.loads(data)
+  if not data:
+    if os.path.exists(path):
+      os.remove(path)
+    return
+
+  dir = os.path.dirname(path)
+  if not os.path.exists(dir):
+    os.makedirs(dir)
+  file = codecs.open(path, 'wb', encoding='utf-8')
+  for key, value in data.iteritems():
+    file.write(generateStringEntry(key, value['message'], path))
+  file.close()
+
 def setupTranslations(locales, projectName, key):
   locales = set(locales)
   firefoxLocales = urllib2.urlopen('http://www.mozilla.org/en-US/firefox/all.html').read()
@@ -211,3 +227,35 @@ def updateTranslationMaster(dir, locale, projectName, key):
     if result.find('<success') < 0:
       raise Exception('Server indicated that the operation was not successful\n' + result)
 
+def getTranslations(localesDir, defaultLocale, projectName, key):
+  result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/export?key=%s' % (projectName, key)).read()
+  if result.find('<success') < 0:
+    raise Exception('Server indicated that the operation was not successful\n' + result)
+
+  result = urllib2.urlopen('http://api.crowdin.net/api/project/%s/download/all.zip?key=%s' % (projectName, key)).read()
+  zip = ZipFile(StringIO(result))
+  dirs = {}
+  for info in zip.infolist():
+    if not info.filename.endswith('.dtd.json') and not info.filename.endswith('.properties.json'):
+      continue
+
+    dir, file = os.path.split(info.filename)
+    origFile = re.sub(r'\.json$', '', file)
+    if not re.match(r'^[\w\-]+$', dir) or dir == defaultLocale:
+      continue
+    if not dir in dirs:
+      dirs[dir] = set()
+    dirs[dir].add(origFile)
+
+    data = zip.open(info.filename).read()
+    fromJSON(os.path.join(localesDir, dir, origFile), data)
+
+  # Remove any extra files
+  for dir, files in dirs.iteritems():
+    baseDir = os.path.join(localesDir, dir)
+    if not os.path.exists(baseDir):
+      continue
+    for file in os.listdir(baseDir):
+      path = os.path.join(baseDir, file)
+      if os.path.isfile(path) and (file.endswith('.properties') or file.endswith('.dtd')) and not file in files:
+        os.remove(path)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list