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

Unified Diff: localeTools.py

Issue 29336281: Issue 2109 - Allow for translation of app independent repositories (Closed)
Patch Set: Created Feb. 11, 2016, 9:04 p.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
« build.py ('K') | « build.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: localeTools.py
diff --git a/localeTools.py b/localeTools.py
index 00346ad885f69dc32e58813a187ec9078e66405c..c925f800ceae9561dd9c789d2e2970c4c81d5b84 100644
--- a/localeTools.py
+++ b/localeTools.py
@@ -108,8 +108,10 @@ def unescapeEntity(value):
return value.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '"')
def mapLocale(type, locale):
- mapping = langMappingChrome if type == 'chrome' else langMappingGecko
- return mapping.get(locale, locale)
+ if type == 'chrome' or type == 'ISO-15897':
Sebastian Noack 2016/02/12 16:33:58 Nit: type in {'chrome', 'ISO-15897'} But why do w
Wladimir Palant 2016/02/12 17:13:04 This logic is wrong - you cannot decide on the map
kzar 2016/02/12 17:28:15 Done.
+ return langMappingChrome.get(locale, locale)
+ else:
+ return langMappingGecko.get(locale, locale)
def parseDTDString(data, path):
result = []
@@ -272,27 +274,27 @@ def postprocessChromeLocale(path, data):
json.dump(parsed, file, ensure_ascii=False, sort_keys=True, indent=2, separators=(',', ': '))
file.close()
-def setupTranslations(type, locales, projectName, key):
- # Copy locales list, we don't want to change the parameter
- locales = set(locales)
+def setupTranslations(projectName, localeConfig, key):
+ # Make a new set from the locales list, mapping to Crowdin friendly format
+ locales = set(map(lambda locale: mapLocale(localeConfig['name_format'],
Sebastian Noack 2016/02/12 16:33:58 Why not using a set expression? {mapLocale(loca
kzar 2016/02/12 17:28:15 Good point, Done.
+ locale),
+ localeConfig['locales'].keys()))
# Fill up with locales that we don't have but the browser supports
- if type == 'chrome':
+ if 'chrome' in localeConfig['target_platforms']:
for locale in chromeLocales:
- locales.add(locale)
- else:
+ locales.add(mapLocale('chrome', locale))
Wladimir Palant 2016/02/12 17:13:04 mapLocale('ISO-15897', ...) please, that's the for
kzar 2016/02/12 18:25:26 Yep, already changed those in second patch set. Sa
+
+ if 'gecko' in localeConfig['target_platforms']:
firefoxLocales = urllib2.urlopen('http://www.mozilla.org/en-US/firefox/all.html').read()
for match in re.finditer(r'&amp;lang=([\w\-]+)"', firefoxLocales):
- locales.add(mapLocale(type, match.group(1)))
+ locales.add(mapLocale('gecko', match.group(1)))
Wladimir Palant 2016/02/12 17:13:04 mapLocale('BCP-47', ...) please, that's the format
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(mapLocale(type, match2.group(1)))
-
- # Convert locale codes to the ones that Crowdin will understand
- locales = set(map(lambda locale: mapLocale(type, locale), locales))
+ locales.add(mapLocale('gecko', match2.group(1)))
Wladimir Palant 2016/02/12 17:13:04 mapLocale('BCP-47', ...) please, that's the format
allowed = set()
allowedLocales = urllib2.urlopen('http://crowdin.net/page/language-codes').read()
@@ -327,7 +329,7 @@ def postFiles(files, url):
if result.find('<success') < 0:
raise Exception('Server indicated that the operation was not successful\n' + result)
-def updateTranslationMaster(type, metadata, dir, projectName, key):
+def updateTranslationMaster(metadata, dir, projectName, localeConfig, 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']))
@@ -336,14 +338,15 @@ def updateTranslationMaster(type, metadata, dir, projectName, key):
for file in os.listdir(dir):
path = os.path.join(dir, file)
if os.path.isfile(path):
- if type == 'chrome' and file.endswith('.json'):
- data = preprocessChromeLocale(path, metadata, True)
- newName = file
- elif type == 'chrome':
- fileHandle = codecs.open(path, 'rb', encoding='utf-8')
- data = json.dumps({file: {'message': fileHandle.read()}})
- fileHandle.close()
- newName = file + '.json'
+ if localeConfig['file_format'] == 'chrome-json':
+ if file.endswith('.json'):
+ data = preprocessChromeLocale(path, metadata, True)
+ newName = file
+ else:
+ fileHandle = codecs.open(path, 'rb', encoding='utf-8')
+ data = json.dumps({file: {'message': fileHandle.read()}})
+ fileHandle.close()
+ newName = file + '.json'
else:
data = toJSON(path)
newName = file + '.json'
@@ -365,19 +368,20 @@ def updateTranslationMaster(type, metadata, dir, 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):
+def uploadTranslations(metadata, dir, locale, projectName, localeConfig, key):
files = []
for file in os.listdir(dir):
path = os.path.join(dir, file)
if os.path.isfile(path):
- if type == 'chrome' and file.endswith('.json'):
- data = preprocessChromeLocale(path, metadata, False)
- newName = file
- elif type == 'chrome':
- fileHandle = codecs.open(path, 'rb', encoding='utf-8')
- data = json.dumps({file: {'message': fileHandle.read()}})
- fileHandle.close()
- newName = file + '.json'
+ if localeConfig['file_format'] == 'chrome-json':
+ if file.endswith('.json'):
+ data = preprocessChromeLocale(path, metadata, False)
+ newName = file
+ else:
+ fileHandle = codecs.open(path, 'rb', encoding='utf-8')
Sebastian Noack 2016/02/12 16:33:58 Using the "with" statement?
kzar 2016/02/12 17:28:15 I really want to avoid unrelated changes this time
Sebastian Noack 2016/02/12 17:38:17 Well, apparently you change this code anyway. Not
+ data = json.dumps({file: {'message': fileHandle.read()}})
+ fileHandle.close()
+ newName = file + '.json'
else:
data = toJSON(path)
newName = file + '.json'
@@ -385,9 +389,11 @@ def uploadTranslations(type, metadata, dir, locale, projectName, key):
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)))
+ postFiles(files, 'http://api.crowdin.net/api/project/%s/upload-translation?key=%s&language=%s' % (
+ projectName, key, mapLocale(localeConfig['name_format'], locale))
+ )
-def getTranslations(type, localesDir, defaultLocale, projectName, key):
+def getTranslations(localeConfig, 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)
@@ -400,20 +406,26 @@ def getTranslations(type, localesDir, defaultLocale, projectName, key):
continue
dir, file = os.path.split(info.filename)
- if not re.match(r'^[\w\-]+$', dir) or dir == defaultLocale:
+ if not re.match(r'^[\w\-]+$', dir) or dir == localeConfig['default_locale']:
continue
- if type == 'chrome' and file.count('.') == 1:
+ if localeConfig['file_format'] == 'chrome-json' and file.count('.') == 1:
origFile = file
else:
origFile = re.sub(r'\.json$', '', file)
- if type == 'gecko' and not origFile.endswith('.dtd') and not origFile.endswith('.properties'):
+ if (localeConfig['file_format'] == 'gecko-dtd' and
+ not origFile.endswith('.dtd') and
+ not origFile.endswith('.properties')):
continue
- mapping = langMappingChrome if type == 'chrome' else langMappingGecko
+ if localeConfig['name_format'] == 'ISO-15897':
+ mapping = langMappingChrome
+ else:
+ mapping = langMappingGecko
+
for key, value in mapping.iteritems():
if value == dir:
dir = key
- if type == 'chrome':
+ if localeConfig['name_format'] == 'ISO-15897':
dir = dir.replace('-', '_')
data = zip.open(info.filename).read()
@@ -424,23 +436,24 @@ def getTranslations(type, localesDir, defaultLocale, projectName, key):
dirs[dir] = set()
dirs[dir].add(origFile)
- path = os.path.join(localesDir, dir, origFile)
+ path = os.path.join(localeConfig['base_path'], dir, origFile)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
- if type == 'chrome' and origFile.endswith('.json'):
- postprocessChromeLocale(path, data)
- elif type == 'chrome':
- data = json.loads(data)
- if origFile in data:
- fileHandle = codecs.open(path, 'wb', encoding='utf-8')
- fileHandle.write(data[origFile]['message'])
- fileHandle.close()
+ if localeConfig['file_format'] == 'chrome-json':
+ if origFile.endswith('.json'):
+ postprocessChromeLocale(path, data)
+ else:
+ data = json.loads(data)
+ if origFile in data:
+ fileHandle = codecs.open(path, 'wb', encoding='utf-8')
Sebastian Noack 2016/02/12 16:33:58 As discussed before, please use io.open instead co
kzar 2016/02/12 17:28:15 As above, want to avoid unrelated changes and this
Sebastian Noack 2016/02/12 17:38:17 Also here, as you change that code anyway, it's go
+ fileHandle.write(data[origFile]['message'])
+ fileHandle.close()
else:
fromJSON(path, data)
# Remove any extra files
for dir, files in dirs.iteritems():
- baseDir = os.path.join(localesDir, dir)
+ baseDir = os.path.join(localeConfig['base_path'], dir)
if not os.path.exists(baseDir):
continue
for file in os.listdir(baseDir):
« build.py ('K') | « build.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld