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

Side by Side Diff: packagerChrome.py

Issue 5307739131609088: Issue 1709 - Allow importing Chrome-style locales in Chrome extensions (Closed)
Patch Set: Created Dec. 16, 2014, 11:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This Source Code Form is subject to the terms of the Mozilla Public 3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this 4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 6
7 import sys, os, re, json, struct 7 import sys
8 import os
9 import re
10 import json
11 import struct
12 import io
8 from StringIO import StringIO 13 from StringIO import StringIO
9 14
10 import packager 15 import packager
11 from packager import readMetadata, getMetadataPath, getDefaultFileName, getBuild Version, getTemplate, Files 16 from packager import readMetadata, getMetadataPath, getDefaultFileName, getBuild Version, getTemplate, Files
12 17
13 defaultLocale = 'en_US' 18 defaultLocale = 'en_US'
14 19
15 def getIgnoredFiles(params): 20 def getIgnoredFiles(params):
16 result = set(('store.description',)) 21 result = set(('store.description',))
17 22
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 160
156 def toJson(data): 161 def toJson(data):
157 return json.dumps( 162 return json.dumps(
158 data, ensure_ascii=False, sort_keys=True, 163 data, ensure_ascii=False, sort_keys=True,
159 indent=2, separators=(',', ': ') 164 indent=2, separators=(',', ': ')
160 ).encode('utf-8') + '\n' 165 ).encode('utf-8') + '\n'
161 166
162 def importGeckoLocales(params, files): 167 def importGeckoLocales(params, files):
163 import localeTools 168 import localeTools
164 169
165 localeCodeMapping = { 170 # We need to map Gecko locales to Chrome locales. Start by mapping Chrome
166 'ar': 'ar', 171 # locales to themselves.
167 'bg': 'bg', 172 locale_mapping = {l: l for l in localeTools.chromeLocales}
168 'ca': 'ca',
169 'cs': 'cs',
170 'da': 'da',
171 'de': 'de',
172 'el': 'el',
173 'en-US': 'en_US',
174 'en-GB': 'en_GB',
175 'es-ES': 'es',
176 'es-AR': 'es_419',
177 'et': 'et',
178 'fi': 'fi',
179 # '': 'fil', ???
180 'fr': 'fr',
181 'he': 'he',
182 'hi-IN': 'hi',
183 'hr': 'hr',
184 'hu': 'hu',
185 'id': 'id',
186 'it': 'it',
187 'ja': 'ja',
188 'ko': 'ko',
189 'lt': 'lt',
190 'lv': 'lv',
191 'nl': 'nl',
192 # 'nb-NO': 'no', ???
193 'pl': 'pl',
194 'pt-BR': 'pt_BR',
195 'pt-PT': 'pt_PT',
196 'ro': 'ro',
197 'ru': 'ru',
198 'sk': 'sk',
199 'sl': 'sl',
200 'sr': 'sr',
201 'sv-SE': 'sv',
202 'th': 'th',
203 'tr': 'tr',
204 'uk': 'uk',
205 'vi': 'vi',
206 'zh-CN': 'zh_CN',
207 'zh-TW': 'zh_TW',
208 }
209 173
210 for source, target in localeCodeMapping.iteritems(): 174 # Convert values to Crowdin locales first (use Chrome => Crowdin mapping).
175 for chrome_locale, crowdin_locale in localeTools.langMappingChrome.iteritems() :
176 locale_mapping[chrome_locale] = crowdin_locale
177
178 # Now convert values to Gecko locales (use Gecko => Crowdin mapping).
179 def get_key(dict, value):
Sebastian Noack 2014/12/17 09:04:06 Maybe I overlook something here? But it seems that
Wladimir Palant 2014/12/17 10:09:54 This will produce lots of duplicates - we need eac
Sebastian Noack 2014/12/17 10:18:36 Well, I were hoping to get rid of get_key() by doi
Wladimir Palant 2014/12/17 10:25:49 I think we can only do this by reversing the mappi
Sebastian Noack 2014/12/17 10:31:49 Both sounds good to me.
Wladimir Palant 2014/12/17 13:21:12 Ok, the current approach removes unnecessary steps
180 for k, v in dict.iteritems():
181 if v == value:
182 return k
183 return None
184
185 for gecko_locale, crowdin_locale in localeTools.langMappingGecko.iteritems():
186 chrome_locale = get_key(locale_mapping, crowdin_locale)
187 if chrome_locale is not None:
188 locale_mapping[chrome_locale] = gecko_locale
189
190 # Reverse mapping and make sure keys use underscores as separator
191 locale_mapping = {v: k.replace('-', '_') for k, v in locale_mapping.iteritems( )}
192
193 for source, target in locale_mapping.iteritems():
211 targetFile = '_locales/%s/messages.json' % target 194 targetFile = '_locales/%s/messages.json' % target
195 if not targetFile in files:
196 continue
212 197
213 for item in params['metadata'].items('import_locales'): 198 for item in params['metadata'].items('import_locales'):
214 fileName, keys = item 199 fileName, keys = item
215 parts = map(lambda n: source if n == '*' else n, fileName.split('/')) 200 parts = map(lambda n: source if n == '*' else n, fileName.split('/'))
216 sourceFile = os.path.join(os.path.dirname(item.source), *parts) 201 sourceFile = os.path.join(os.path.dirname(item.source), *parts)
217 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incomplete' ) 202 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incomplete' )
218 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker): 203 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker):
219 continue 204 continue
220 205
221 data = {} 206 data = json.loads(files[targetFile].decode('utf-8'))
222 if targetFile in files:
223 data = json.loads(files[targetFile].decode('utf-8'))
224 207
225 try: 208 try:
226 sourceData = localeTools.readFile(sourceFile) 209 if sourceFile.endswith('.json'):
210 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
211 sourceData = {k: v['message'] for k, v in json.load(handle).iteritem s()}
212 else:
213 sourceData = localeTools.readFile(sourceFile)
227 214
228 # Resolve wildcard imports 215 # Resolve wildcard imports
229 if keys == '*' or keys == '=*': 216 if keys == '*' or keys == '=*':
230 importList = sourceData.keys() 217 importList = sourceData.keys()
231 importList = filter(lambda k: not k.startswith('_'), importList) 218 importList = filter(lambda k: not k.startswith('_'), importList)
232 if keys == '=*': 219 if keys == '=*':
233 importList = map(lambda k: '=' + k, importList) 220 importList = map(lambda k: '=' + k, importList)
234 keys = ' '.join(importList) 221 keys = ' '.join(importList)
235 222
236 for stringID in re.split(r'\s+', keys): 223 for stringID in re.split(r'\s+', keys):
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 'lib/info.js' not in files): 377 'lib/info.js' not in files):
391 files['lib/info.js'] = createInfoModule(params) 378 files['lib/info.js'] = createInfoModule(params)
392 379
393 zipdata = files.zipToString() 380 zipdata = files.zipToString()
394 signature = None 381 signature = None
395 pubkey = None 382 pubkey = None
396 if keyFile != None: 383 if keyFile != None:
397 signature = signBinary(zipdata, keyFile) 384 signature = signBinary(zipdata, keyFile)
398 pubkey = getPublicKey(keyFile) 385 pubkey = getPublicKey(keyFile)
399 writePackage(outFile, pubkey, signature, zipdata) 386 writePackage(outFile, pubkey, signature, zipdata)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld