[Pkg-mozext-commits] [adblock-plus] 313/464: Made sure Chrome master locale can be uploaded to Crowedin and implemented upload of existing locales (the latter doesn't work, issue appears to be on Crowdin's side)

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:44:28 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 e9a0c52d42ff87582d03d2187ba7f688690bd9b0
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Thu Oct 18 17:09:07 2012 +0200

    Made sure Chrome master locale can be uploaded to Crowedin and implemented upload of existing locales (the latter doesn't work, issue appears to be on Crowdin's side)
---
 build.py          |  52 ++++++++++++++++++++++++---
 localeTools.py    | 106 +++++++++++++++++++++++++++++++++++++++++-------------
 packagerChrome.py |   2 ++
 3 files changed, 130 insertions(+), 30 deletions(-)

diff --git a/build.py b/build.py
index 3603dfd..89ae703 100644
--- a/build.py
+++ b/build.py
@@ -244,12 +244,48 @@ def updateTranslationMaster(baseDir, scriptName, opts, args, type):
 
   key = args[0]
 
-  import buildtools.packagerGecko as packager
-  defaultLocaleDir = os.path.join(packager.getLocalesDir(baseDir), packager.defaultLocale)
-  basename = packager.readMetadata(baseDir).get('general', 'baseName')
+  if type == 'chrome':
+    import buildtools.packagerChrome as packager
+    defaultLocaleDir = os.path.join(baseDir, '_locales', packager.defaultLocale)
+    metadata = packager.readMetadata(baseDir)
+    basename = metadata.get('general', 'baseName')
+  else:
+    import buildtools.packagerGecko as packager
+    defaultLocaleDir = os.path.join(packager.getLocalesDir(baseDir), packager.defaultLocale)
+    metadata = packager.readMetadata(baseDir)
+    basename = metadata.get('general', 'baseName')
 
   import buildtools.localeTools as localeTools
-  localeTools.updateTranslationMaster(defaultLocaleDir, packager.defaultLocale, basename, key)
+  localeTools.updateTranslationMaster(type, metadata, defaultLocaleDir, basename, key)
+
+
+def uploadTranslations(baseDir, scriptName, opts, args, type):
+  if len(args) < 1:
+    print 'Project key is required to upload existing translations.'
+    usage(scriptName, type, 'uploadtrans')
+    return
+
+  key = args[0]
+
+  if type == 'chrome':
+    import buildtools.packagerChrome as packager
+    localesDir = os.path.join(baseDir, '_locales')
+    locales = os.listdir(localesDir)
+    locales = map(lambda locale: (locale.replace('_', '-'), os.path.join(localesDir, locale)), locales)
+    metadata = packager.readMetadata(baseDir)
+    basename = metadata.get('general', 'baseName')
+  else:
+    import buildtools.packagerGecko as packager
+    localesDir = packager.getLocalesDir(baseDir)
+    locales = packager.getLocales(baseDir, True)
+    locales = map(lambda locale: (locale, os.path.join(localesDir, locale)), locales)
+    metadata = packager.readMetadata(baseDir)
+    basename = metadata.get('general', 'baseName')
+
+  import buildtools.localeTools as localeTools
+  for locale, localeDir in locales:
+    if locale != packager.defaultLocale:
+      localeTools.uploadTranslations(type, metadata, localeDir, locale, basename, key)
 
 
 def getTranslations(baseDir, scriptName, opts, args, type):
@@ -403,7 +439,13 @@ with addCommand(updateTranslationMaster, 'translate') as command:
   command.shortDescription = 'Updates translation master files'
   command.description = 'Updates the translation master files in the project on crowdin.net.'
   command.params = '[options] project-key'
-  command.supportedTypes = ('gecko')
+  command.supportedTypes = ('gecko', 'chrome')
+
+with addCommand(uploadTranslations, 'uploadtrans') as command:
+  command.shortDescription = 'Uploads existing translations'
+  command.description = 'Uploads already existing translations to the project on crowdin.net.'
+  command.params = '[options] project-key'
+  command.supportedTypes = ('gecko', 'chrome')
 
 with addCommand(getTranslations, 'gettranslations') as command:
   command.shortDescription = 'Downloads translation updates'
diff --git a/localeTools.py b/localeTools.py
index 63a380c..184d212 100644
--- a/localeTools.py
+++ b/localeTools.py
@@ -98,6 +98,10 @@ def escapeEntity(value):
 def unescapeEntity(value):
   return value.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"')
 
+def mapLocale(type, locale):
+  mapping = langMappingChrome if type == 'chrome' else langMappingGecko
+  return mapping.get(locale, locale)
+
 def parseDTDString(data, path):
   result = []
   currentComment = [None]
@@ -209,7 +213,7 @@ def toJSON(path):
     else:
       obj['description'] = '%s: %s' % (name, comment)
     result[name] = obj
-  return json.dumps(result, indent=2)
+  return json.dumps(result, ensure_ascii=False, indent=2)
 
 def fromJSON(path, data):
   data = json.loads(data)
@@ -226,6 +230,36 @@ def fromJSON(path, data):
     file.write(generateStringEntry(key, value['message'], path))
   file.close()
 
+def preprocessChromeLocale(path, metadata, isMaster):
+  fileHandle = codecs.open(path, 'rb', encoding='utf-8')
+  data = json.load(fileHandle)
+  fileHandle.close()
+
+  # Remove synced keys, these don't need to be translated
+  if metadata.has_section('locale_sync'):
+    for file, stringIDs in metadata.items('locale_sync'):
+      for stringID in re.split(r'\s+', stringIDs):
+        if file == 'remove':
+          key = stringID
+        else:
+          key = re.sub(r'\..*', '', file) + '_' + re.sub(r'\W', '_', stringID)
+        if key in data:
+          del data[key]
+
+  for key, value in data.iteritems():
+    if isMaster:
+      # Make sure the key name is listed in the description
+      if "description" in value:
+        value["description"] = "%s: %s" % (key, value["description"])
+      else:
+        value["description"] = key
+    else:
+      # Delete description from translations
+      if "description" in value:
+        del value["description"]
+
+  return json.dumps(data, ensure_ascii=False, sort_keys=True, indent=2)
+
 def setupTranslations(type, locales, projectName, key):
   # Copy locales list, we don't want to change the parameter
   locales = set(locales)
@@ -237,17 +271,16 @@ def setupTranslations(type, locales, projectName, key):
   else:
     firefoxLocales = urllib2.urlopen('http://www.mozilla.org/en-US/firefox/all.html').read()
     for match in re.finditer(r'&lang=([\w\-]+)"', firefoxLocales):
-      locales.add(langMappingGecko.get(match.group(1), match.group(1)))
+      locales.add(mapLocale(type, match.group(1)))
     langPacks = urllib2.urlopen('https://addons.mozilla.org/en-US/firefox/language-tools/').read()
     for match in re.finditer(r'<tr>.*?</tr>', langPacks, re.S):
       if match.group(0).find('Install Language Pack') >= 0:
         match2 = re.search(r'lang="([\w\-]+)"', match.group(0))
         if match2:
-          locales.add(langMappingGecko.get(match2.group(1), match2.group(1)))
+          locales.add(mapLocale(type, match2.group(1)))
 
   # Convert locale codes to the ones that Crowdin will understand
-  mapping = langMappingChrome if type == 'chrome' else langMappingGecko
-  locales = set(map(lambda locale: mapping[locale] if locale in mapping else locale, locales))
+  locales = set(map(lambda locale: mapLocale(type, locale), locales))
 
   allowed = set()
   allowedLocales = urllib2.urlopen('http://crowdin.net/page/language-codes').read()
@@ -263,7 +296,25 @@ def setupTranslations(type, locales, projectName, key):
   if result.find('<success') < 0:
     raise Exception('Server indicated that the operation was not successful\n' + result)
 
-def updateTranslationMaster(dir, locale, projectName, key):
+def postFiles(files, url):
+  boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
+  body = ''
+  for file, data in files:
+    body +=  '--%s\r\n' % boundary
+    body += 'Content-Disposition: form-data; name="files[%s]"; filename="%s"\r\n' % (file, file)
+    body += 'Content-Type: application/octet-stream\r\n'
+    body += 'Content-Transfer-Encoding: binary\r\n'
+    body += '\r\n' + data + '\r\n'
+    body += '--%s--\r\n' % boundary
+
+  request = urllib2.Request(url, StringIO(body.encode('utf-8')))
+  request.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
+  request.add_header('Content-Length', len(body))
+  result = urllib2.urlopen(request).read()
+  if result.find('<success') < 0:
+    raise Exception('Server indicated that the operation was not successful\n' + result)
+
+def updateTranslationMaster(type, metadata, dir, projectName, key):
   result = json.load(urllib2.urlopen('http://api.crowdin.net/api/project/%s/info?key=%s&json=1' % (projectName, key)))
 
   existing = set(map(lambda f: f['name'], result['files']))
@@ -272,32 +323,20 @@ def updateTranslationMaster(dir, locale, projectName, key):
   for file in os.listdir(dir):
     path = os.path.join(dir, file)
     if os.path.isfile(path):
-      data = toJSON(path)
-      if data:
+      if type == 'chrome':
+        data = preprocessChromeLocale(path, metadata, True)
+        newName = file
+      else:
+        data = toJSON(path)
         newName = file + '.json'
+
+      if data:
         if newName in existing:
           update.append((newName, data))
           existing.remove(newName)
         else:
           add.append((newName, data))
 
-  def postFiles(files, url):
-    boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
-    body = ''
-    for file, data in files:
-      body +=  '--%s\r\n' % boundary
-      body += 'Content-Disposition: form-data; name="files[%s]"; filename="%s"\r\n' % (file, file)
-      body += 'Content-Type: application/octet-stream\r\n'
-      body += '\r\n' + data.encode('utf-8') + '\r\n'
-      body += '--%s--\r\n' % boundary
-
-    request = urllib2.Request(url, body)
-    request.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
-    request.add_header('Content-Length', len(body))
-    result = urllib2.urlopen(request).read()
-    if result.find('<success') < 0:
-      raise Exception('Server indicated that the operation was not successful\n' + result)
-
   if len(add):
     titles = urllib.urlencode([('titles[%s]' % name, re.sub(r'\.json', '', name)) for name, data in add])
     postFiles(add, 'http://api.crowdin.net/api/project/%s/add-file?key=%s&type=chrome&%s' % (projectName, key, titles))
@@ -308,6 +347,23 @@ def updateTranslationMaster(dir, locale, projectName, key):
     if result.find('<success') < 0:
       raise Exception('Server indicated that the operation was not successful\n' + result)
 
+def uploadTranslations(type, metadata, dir, locale, projectName, key):
+  files = []
+  for file in os.listdir(dir):
+    path = os.path.join(dir, file)
+    if os.path.isfile(path):
+      if type == 'chrome':
+        data = preprocessChromeLocale(path, metadata, False)
+        newName = file
+      else:
+        data = toJSON(path)
+        newName = file + '.json'
+
+      if data:
+        files.append((newName, data))
+  if len(files):
+    postFiles(files, 'http://api.crowdin.net/api/project/%s/upload-translation?key=%s&language=%s' % (projectName, key, mapLocale(type, locale)))
+
 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:
diff --git a/packagerChrome.py b/packagerChrome.py
index c1f62c4..264c301 100644
--- a/packagerChrome.py
+++ b/packagerChrome.py
@@ -9,6 +9,8 @@ from ConfigParser import SafeConfigParser
 from StringIO import StringIO
 from zipfile import ZipFile, ZIP_DEFLATED
 
+defaultLocale = 'en_US'
+
 def getDefaultFileName(baseDir, metadata, version, ext):
   return os.path.join(baseDir, '%s-%s.%s' % (metadata.get('general', 'baseName'), version, ext))
 

-- 
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