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

Side by Side Diff: localeSyncChrome.py

Issue 8627097: Moved Chrome extension scripts to buildtools repository (Closed)
Patch Set: Added build.py gettranslations support for Chrome Created Oct. 22, 2012, 11:25 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 # This Source Code is subject to the terms of the Mozilla Public License
5 # version 2.0 (the "License"). You can obtain a copy of the License at
6 # http://mozilla.org/MPL/2.0/.
7
8 import sys, os, json, re, codecs
9 import buildtools.localeTools as localeTools
10
11 firefoxToChrome = {
12 'ar': 'ar',
13 'bg': 'bg',
14 'ca': 'ca',
15 'cs': 'cs',
16 'da': 'da',
17 'de': 'de',
18 'el': 'el',
19 'en-US': 'en_US',
20 'en-GB': 'en_GB',
21 'es-ES': 'es',
22 'es-AR': 'es_419',
23 'et': 'et',
24 'fi': 'fi',
25 # '': 'fil', ???
26 'fr': 'fr',
27 'he': 'he',
28 'hi-IN': 'hi',
29 'hr': 'hr',
30 'hu': 'hu',
31 'id': 'id',
32 'it': 'it',
33 'ja': 'ja',
34 'ko': 'ko',
35 'lt': 'lt',
36 'lv': 'lv',
37 'nl': 'nl',
38 # 'nb-NO': 'no', ???
39 'pl': 'pl',
40 'pt-BR': 'pt_BR',
41 'pt-PT': 'pt_PT',
42 'ro': 'ro',
43 'ru': 'ru',
44 'sk': 'sk',
45 'sl': 'sl',
46 'sr': 'sr',
47 'sv-SE': 'sv',
48 'th': 'th',
49 'tr': 'tr',
50 'uk': 'uk',
51 'vi': 'vi',
52 'zh-CN': 'zh_CN',
53 'zh-TW': 'zh_TW',
54 }
55
56 def syncLocales(sourceLocales, targetLocales, removed, imported):
57 for source, target in firefoxToChrome.iteritems():
58 targetFile = os.path.join(targetLocales, target, 'messages.json')
59 hasSource = os.path.exists(os.path.join(sourceLocales, source))
60 if hasSource and os.path.exists(os.path.join(sourceLocales, source, '.incomp lete')):
61 hasSource = False
62 if not hasSource and not os.path.exists(targetFile):
63 continue
64
65 data = {}
66 if os.path.exists(targetFile):
67 file = codecs.open(targetFile, 'rb', encoding='utf-8')
68 data = json.load(file)
69 file.close()
70
71 for entry in removed:
72 if entry in data:
73 del data[entry]
74
75 if hasSource:
76 for fileName, stringIDs in imported:
77 sourceFile = os.path.join(sourceLocales, source, fileName)
78 try:
79 sourceData = localeTools.readFile(sourceFile)
80 for stringID in stringIDs:
81 if stringID in sourceData:
82 key = re.sub(r'\..*', '', fileName) + '_' + re.sub(r'\W', '_', str ingID)
83 data[key] = {'message': sourceData[stringID]}
84 except:
85 pass
86
87 sourceFile = os.path.join(sourceLocales, source, 'meta.properties')
88 try:
89 sourceData = localeTools.readFile(sourceFile)
90 if 'name' in sourceData:
91 data['name'] = {'message': sourceData['name']}
92 except:
93 pass
94
95 try:
96 os.makedirs(os.path.dirname(targetFile))
97 except:
98 pass
99 file = codecs.open(targetFile, 'wb', encoding='utf-8')
100 json.dump(data, file, ensure_ascii=False, sort_keys=True, indent=2, separato rs=(',', ': '))
101 print >>file
102 file.close()
103
104 def run(baseDir, sourceDir):
105 import buildtools.packagerGecko as packagerGecko
106 import buildtools.packagerChrome as packagerChrome
107
108 sourceLocales = packagerGecko.getLocalesDir(sourceDir)
109 if not os.path.isdir(sourceLocales):
110 raise IOError('Directory %s not found' % sourceLocales)
111 targetLocales = os.path.join(baseDir, '_locales')
112
113 metadata = packagerChrome.readMetadata(baseDir)
114 removed = []
115 if metadata.has_option('locale_sync', 'remove'):
116 for key in re.split(r'\s+', metadata.get('locale_sync', 'remove')):
117 removed.append(key)
118
119 imported = []
120 for file, keys in metadata.items('locale_sync'):
121 if file == 'remove':
122 continue
123 imported.append((file, re.split(r'\s+', keys)))
124 syncLocales(sourceLocales, targetLocales, removed, imported)
OLDNEW

Powered by Google App Engine
This is Rietveld