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: Simplified mapping calculation Created Dec. 17, 2014, 1:20 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 Chrome locales to Gecko locales. Start by mapping Chrome
166 'ar': 'ar', 171 # locales to themselves, merely with the minus sign as separator.
167 'bg': 'bg', 172 locale_mapping = {l.replace('-', '_'): 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.replace('-', '_')] = crowdin_locale
Sebastian Noack 2014/12/17 14:11:46 I'd rather not duplicate the logic replacing the d
Wladimir Palant 2014/12/17 14:59:33 Done.
177
178 # Now convert values to Gecko locales (use Gecko => Crowdin mapping).
179 reverse_mapping = {v: k for k, v in locale_mapping.iteritems()}
180 for gecko_locale, crowdin_locale in localeTools.langMappingGecko.iteritems():
181 if crowdin_locale in reverse_mapping:
182 locale_mapping[reverse_mapping[crowdin_locale]] = gecko_locale
183
184 for target, source in locale_mapping.iteritems():
211 targetFile = '_locales/%s/messages.json' % target 185 targetFile = '_locales/%s/messages.json' % target
186 if not targetFile in files:
187 continue
212 188
213 for item in params['metadata'].items('import_locales'): 189 for item in params['metadata'].items('import_locales'):
214 fileName, keys = item 190 fileName, keys = item
215 parts = map(lambda n: source if n == '*' else n, fileName.split('/')) 191 parts = map(lambda n: source if n == '*' else n, fileName.split('/'))
216 sourceFile = os.path.join(os.path.dirname(item.source), *parts) 192 sourceFile = os.path.join(os.path.dirname(item.source), *parts)
217 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incomplete' ) 193 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incomplete' )
218 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker): 194 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker):
219 continue 195 continue
220 196
221 data = {} 197 data = json.loads(files[targetFile].decode('utf-8'))
222 if targetFile in files:
223 data = json.loads(files[targetFile].decode('utf-8'))
224 198
225 try: 199 try:
226 sourceData = localeTools.readFile(sourceFile) 200 if sourceFile.endswith('.json'):
201 with io.open(sourceFile, 'r', encoding='utf-8') as handle:
202 sourceData = {k: v['message'] for k, v in json.load(handle).iteritem s()}
203 else:
204 sourceData = localeTools.readFile(sourceFile)
227 205
228 # Resolve wildcard imports 206 # Resolve wildcard imports
229 if keys == '*' or keys == '=*': 207 if keys == '*' or keys == '=*':
230 importList = sourceData.keys() 208 importList = sourceData.keys()
231 importList = filter(lambda k: not k.startswith('_'), importList) 209 importList = filter(lambda k: not k.startswith('_'), importList)
232 if keys == '=*': 210 if keys == '=*':
233 importList = map(lambda k: '=' + k, importList) 211 importList = map(lambda k: '=' + k, importList)
234 keys = ' '.join(importList) 212 keys = ' '.join(importList)
235 213
236 for stringID in re.split(r'\s+', keys): 214 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): 368 'lib/info.js' not in files):
391 files['lib/info.js'] = createInfoModule(params) 369 files['lib/info.js'] = createInfoModule(params)
392 370
393 zipdata = files.zipToString() 371 zipdata = files.zipToString()
394 signature = None 372 signature = None
395 pubkey = None 373 pubkey = None
396 if keyFile != None: 374 if keyFile != None:
397 signature = signBinary(zipdata, keyFile) 375 signature = signBinary(zipdata, keyFile)
398 pubkey = getPublicKey(keyFile) 376 pubkey = getPublicKey(keyFile)
399 writePackage(outFile, pubkey, signature, zipdata) 377 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